⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 square.java

📁 a very good example of how to use java for a minesweeper game that is much like the windows one. Th
💻 JAVA
字号:
import javax.swing.*;import java.awt.*;import java.awt.event.*;/** *designed & build by Grahame batte email: g.batte@lancaster.ac.uk *last modified on 22-05-03  *used in my minesweeper aplication */public class Square extends JButton implements MouseListener{/** *mine indicates the state of a square, the square behaves appropriatly if it is a mine */	public boolean mine = false;	private boolean flaged = false;/** *GameOver indicates the state of uncovering a mine, if == true a nw game needs to be started. */	public static boolean GameOver = false;/** *GameWon indicates the state of a game being won.that is all the mines have been flagedright and all other squares uncovered safly */		public static boolean GameWon = false;/** *sUncovered indicates where a square has been uncovered or not.  */	public boolean sUncovered = false;/** *count is the number of mines around this square, incremented by game class when a mine is placed */	public int count = 0;/** *flagnum is the number of flas that can be displayed equal to number of mine */	public static int flagnum;/** *flagused is the number of flags used so far */	public static int flagused = 0;/** *flagleft is the number of flags that have not been placed on the game equal to flagnum - flags used */	public static int flagleft; /** *indicates when a flaged is places where it is in the wright place.that is if it has been placed on the location of a mine */	public boolean flagedright=false;/** *T100 the int representing the hundrads in the flags left *//** *T10 the int representing the tens in the flags left *//** *T1 the int representing the units in the flags left */	public static int T100,T10,T;/** *numclick indicates the number of times the mouse is click during a game,  * its main use is the start the timer thread when numclicks == 1 and to stop the thread if ==0 */	public static int numclicks = 0;/** *isblank indicates whether a square that has been clicked on is blank or not */	public boolean isblank = false;	int row,column;	static int nrow,ncolumn;/** *Square creats a square for display on the game. at location x y. it is called be the game class. also adds functons to the square *@param x the row number where this square will be displayed *@param y the column number where this square will be displayed *@param z the number of mines on the game, used for the number of mines left display */		public Square(int x, int y)		   {			super();			row=x;			column=y;			flagnum = game.mines;		    ImageIcon unclicked = new ImageIcon("images/unclicked.jpg"); //blank square			this.setIcon(unclicked);			this.setEnabled(true);		    this.addMouseListener(this);		    		    flagleft = flagnum-flagused;	 		T100= flagleft/100;				// finds the hundreds uleft	 		T10=(flagleft-T100*100)/10;		//find the tens left	 		T=flagleft%10;						//find the units	 		MineSweeper.one.setIcon(new ImageIcon("digits/"+T+".gif"));	//sets button to display wright image	 		MineSweeper.ten.setIcon(new ImageIcon("digits/"+T10+".gif"));	//	 		MineSweeper.hund.setIcon(new ImageIcon("digits/"+T100+".gif"));//		   }	//close constuctor		//------------------------MouseListener-----------------------------------------/** *mousedReleased adds the funtions to the square, increments the numclicked, starts the timer, changes the state of the smile and of the game, *places and removes flags, explodes mines(makes then red), updates the flags left display and uncovers the square. */		public void mouseReleased(MouseEvent e)			{				 numclicks++;			 if (numclicks==1)			 	{	 		 	 	 timer one = new timer ();			//start the timer 			 	 one.start();	 		 	}	 			 ImageIcon smiley20 = new ImageIcon("images/smiley2.jpg"); //change the state of the smiling face	  			 MineSweeper.face.setIcon(smiley20);	  		 	//		 if (GameOver==false)				//cheecks for game status				{			 ImageIcon smiley1 = new ImageIcon("images/smiley1.jpg"); //change the state of the smiling face	  		 MineSweeper.face.setIcon(smiley1);			 	if (flaged == true)				//removes a flag if the is righr click, left click does nothing						 		{				 		if (e.getButton()==3)					 	 		{			 	 		 sUncovered=false;			 	 		 flagedright=false;			 	 		 ImageIcon unclicked = new ImageIcon("images/unclicked.jpg");						 this.setIcon(unclicked);							 flaged = false;						 flagused--;			//reduses flags used //System.out.println(flagused);						 flagleft = flagnum-flagused;						 T100= flagleft/100;				// finds the hundreds uleft						 T10=(flagleft-T100*100)/10;		//find the tens left						 T=flagleft%10;						//find the units						 MineSweeper.one.setIcon(new ImageIcon("digits/"+T+".gif"));	//sets button to display wright image						 MineSweeper.ten.setIcon(new ImageIcon("digits/"+T10+".gif"));	//						 MineSweeper.hund.setIcon(new ImageIcon("digits/"+T100+".gif"));//			 	  		}  // end if(flaged == true)			 		} // end if(e.getButton()==3)			 					 	else if (e.getButton()==3)		// adds a flag ir right click that there is not one one the square			 	 	{			 	 		sUncovered=true;			 	 	 if (flagused<flagnum)		//limits the number of flags used			 	 	 	{			 	 		 ImageIcon marked = new ImageIcon("images/marked.jpg"); 						 this.setIcon(marked);							 flaged = true;						 flagused++;				//increases flags used//System.out.println(flagused);						 flagleft = flagnum-flagused;						 T100= flagleft/100;				// finds the hundreds uleft						 T10=(flagleft-T100*100)/10;		//find the tens left						 T=flagleft%10;						//find the units						 MineSweeper.one.setIcon(new ImageIcon("digits/"+T+".gif"));	//sets button to display wright image						 MineSweeper.ten.setIcon(new ImageIcon("digits/"+T10+".gif"));	//						 MineSweeper.hund.setIcon(new ImageIcon("digits/"+T100+".gif"));//						 						 if(flaged==true&&mine==true)						 	{						 	 flagedright=true;						 	}						}//end if(flagused<flagnum)			 	  	}  // end if			 	  				 	else if (e.getButton()==1)			 		{			 		 setEnabled(false);			//reveals what is behind the square			 		 sUncovered=true;			 			if(mine == true)		//if it is a mine			 				{			 				 ImageIcon mine2 = new ImageIcon("images/mine2.jpg");//set mine to red			 				 this.setDisabledIcon(mine2);			 				 GameOver = true;							//sets state of game						 	 ImageIcon smiley2 = new ImageIcon("images/smiley2.jpg"); //change the state of the smiling face	  					 	 MineSweeper.face.setIcon(smiley2);	  					 	 	  					 	 for ( int i=0; i<build.s.length;i++)		// displays mines										        						{        						 for (int j=0; j<build.s.length;j++)									{									 if(build.s[i][j].mine==true&&build.s[i][j].flagedright==false)									 	{									  	 build.s[i][j].setEnabled(false);									 	}									}								}			 				}//end if			 		 	if(isblank=true)			 		 		{			 		 		 sUncovered=true;			 		 		 iteration1(row,column,game.row,game.column);			 		 		 iteration2(row,column,game.row,game.column);			 		 		 iteration3(row,column,game.row,game.column);			 		 		 iteration4(row,column,game.row,game.column);			 		 		 iteration5(row,column,game.row,game.column);			 		 		 iteration6(row,column,game.row,game.column);			 		 		 iteration7(row,column,game.row,game.column);			 		 		 iteration8(row,column,game.row,game.column);			 		 		}										 		}// end else if			 				 	} // end if game over == true				  				}  // end mouse released                     			/** *mousedPresed changes the state of the simle when a button is clicked */					public void mousePressed(MouseEvent e)			{ 			 ImageIcon smiley4 = new ImageIcon("images/smiley4.jpg"); //change the state of the smiling face	  		 MineSweeper.face.setIcon(smiley4);			}/** *not used in this program */		public void mouseClicked(MouseEvent e)			{   //not used in this program			}/** *not used in this program */		public void mouseEntered(MouseEvent e)			{  //not used in this program			}/** *not used in this program */		public void mouseExited(MouseEvent e)			{  //not used in this program			}  /** *iteration1 calls remove and iterates with(x+1,y,row,column) *@param x  the row number of the square clicked on *@param y  the column number of the square clicked on *@param column the number of columns asked for be the minesweeper class *@param row the number of rows asked for be the minesweeper class */		public static void iteration1(int x, int y,int row,int column)			{				try{			nrow=x;			ncolumn=y;			if (build.s[nrow][ncolumn].count!=0)				{				}			else if (build.s[nrow][ncolumn].sUncovered=true)				{     				remove dr = new remove(nrow,ncolumn,row,column);		 			}	 		iteration1(x+1,y,row,column);											}catch (Exception t){}			}/** *iteration2 calls remove and iterates with(x-1,y,row,column) *@param x  the row number of the square clicked on *@param y  the column number of the square clicked on *@param column the number of columns asked for be the minesweeper class *@param row the number of rows asked for be the minesweeper class */		public static void iteration2(int x, int y,int row,int column)			{				try{			nrow=x;			ncolumn=y;			if (build.s[nrow][ncolumn].count!=0)				{				}			else if (build.s[nrow][ncolumn].sUncovered=true)				{     				remove dr = new remove(nrow,ncolumn,row,column);		 			}			iteration2(x-1,y,row,column);								}catch (Exception t){}			}/** *iteration3 calls remove and iterates with(x,y+1,row,column) *@param x  the row number of the square clicked on *@param y  the column number of the square clicked on *@param column the number of columns asked for be the minesweeper class *@param row the number of rows asked for be the minesweeper class */		public static void iteration3(int x, int y,int row,int column)			{				try{			nrow=x;			ncolumn=y;			if (build.s[nrow][ncolumn].count!=0)				{				}			else if (build.s[nrow][ncolumn].sUncovered=true)				{     				remove dr = new remove(nrow,ncolumn,row,column);		 			}			iteration3(x,y+1,row,column);								}catch (Exception t){}			}/** *iteration4 calls remove and iterates with(x+1,y+1,row,column) *@param x  the row number of the square clicked on *@param y  the column number of the square clicked on *@param column the number of columns asked for be the minesweeper class *@param row the number of rows asked for be the minesweeper class */		public static void iteration4(int x, int y,int row,int column)			{				try{			nrow=x;			ncolumn=y;			if (build.s[nrow][ncolumn].count!=0)				{				}			else if (build.s[nrow][ncolumn].sUncovered=true)				{     				remove dr = new remove(nrow,ncolumn,row,column);		 			}			    iteration4(x+1,y+1,row,column);;					}catch (Exception t){}			}/** *iteration5 calls remove and iterates with(x,y-1,row,column) *@param x  the row number of the square clicked on *@param y  the column number of the square clicked on *@param column the number of columns asked for be the minesweeper class *@param row the number of rows asked for be the minesweeper class */		public static void iteration5(int x, int y,int row,int column)			{				try{			nrow=x;			ncolumn=y;			if (build.s[nrow][ncolumn].count!=0)				{				}			else if (build.s[nrow][ncolumn].sUncovered=true)				{     				remove dr = new remove(nrow,ncolumn,row,column);		 			}	 		iteration5(x,y-1,row,column);				 		}catch (Exception t){}	 		}/** *iteration6 calls remove and iterates with(x-1,y-1,row,column) *@param x  the row number of the square clicked on *@param y  the column number of the square clicked on *@param column the number of columns asked for be the minesweeper class *@param row the number of rows asked for be the minesweeper class */	 	public static void iteration6(int x, int y,int row,int column)			{				try{			nrow=x;			ncolumn=y;			if (build.s[nrow][ncolumn].count!=0)				{				}			else if (build.s[nrow][ncolumn].sUncovered=true)				{     				remove dr = new remove(nrow,ncolumn,row,column);		 			}	 			iteration6(x-1,y-1,row,column);	 		}catch (Exception t){}	 		}/** *iteration7 calls remove and iterates with(x+1,y-1,row,column) *@param x  the row number of the square clicked on *@param y  the column number of the square clicked on *@param column the number of columns asked for be the minesweeper class *@param row the number of rows asked for be the minesweeper class */	 	public static void iteration7(int x, int y,int row,int column)			{				try{			nrow=x;			ncolumn=y;			if (build.s[nrow][ncolumn].count!=0)				{				}			else if (build.s[nrow][ncolumn].sUncovered=true)				{     				remove dr = new remove(nrow,ncolumn,row,column);		 			}	 		iteration7(x+1,y-1,row,column);	 		}catch (Exception t){}	 		}/** *iteration8 calls remove and iterates with(x-1,y+1,row,column) *@param x  the row number of the square clicked on *@param y  the column number of the square clicked on *@param column the number of columns asked for be the minesweeper class *@param row the number of rows asked for be the minesweeper class */	 	public static void iteration8(int x, int y,int row,int column)			{				try{			nrow=x;			ncolumn=y;			if (build.s[nrow][ncolumn].count!=0)				{				}			else if (build.s[nrow][ncolumn].sUncovered=true)				{     				remove dr = new remove(nrow,ncolumn,row,column);		 			}	 		iteration8(x-1,y+1,row,column);	 		}catch (Exception t){}	 		}}	// end Square class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -