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

📄 game.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 19-05-03 
 *used in my minesweeper aplication
 */
public class game extends JPanel
{
	int minesPlaced = 0; 
/**
 *numUncovered is the number of squares on the game that have been uncovered. used to determine when the game is over.
 */
	public static int numUncovered = 0;
/**
 *row,column,and mines anr the number of rows,columns and mines. used to pass the int around the program.
 */
	public static int row,column,mines;
/**
 *game sets s = new Square [row][column];
 *randomly generats int mines number of mines, places them on the game
 *displays the numbers indicating the number of mines next to a square
 *@param row  the number of rows asked for by the minesweeper clas
 *@param column the number of columns asked for be the minesweeper class
 *@param mines the number of columns asked for be the minesweeper class
 */
public game(int x, int y,int z)
	{ 	
	super();
	row=x;
	column=y;
	mines=z;			 
	 setLayout(new GridLayout(row,column));	
	 build a = new build(x,y);				 
    	for ( int i=0; i<x;i++)															
        	{
        	 for (int j=0; j<y;j++)
				{
				 add(build.s[i][j]);					// displays that square
				} //end for
			}  //end for
		 while (minesPlaced < mines) 			// places z random mines
			{
	    	  int nrow = randomInt(row);		//randomInt is a method i wrote at the end of this class
	    	  int ncolumn = randomInt(column);	//it generates a random number less than the number given(ie row or column)

             build.s [nrow][ncolumn].mine = true;		// sets square nrow,ncolumn to a mine
             ImageIcon mine = new ImageIcon("images/mine.jpg");
		     build.s[nrow][ncolumn].setDisabledIcon(mine);
			 minesPlaced++;						//increments the mines place to keep track of the number used
//System.out.println("location " + nrow + ncolumn);
//--------------------displays the number of mines next to a square code cheeks if square is on the edge
//--------------------then increments the squares next to mine		
		try{
			if(ncolumn==(column-1))
				{
				 build.s [nrow+1][ncolumn].count++;	
				build.s [nrow+1][ncolumn-1].count++;
				 build.s [nrow][ncolumn-1].count++;
				 build.s [nrow-1][ncolumn-1].count++;
				 build.s [nrow-1][ncolumn].count++;
				}//end if(nrow==0)
			else if((nrow)==(row-1)&&ncolumn==(column-1))
				{
				 build.s [nrow][ncolumn-1].count++;
				 build.s [nrow-1][ncolumn-1].count++;
				 build.s [nrow-1][ncolumn].count++;
				}//end 
			else if((nrow)==(row-1))
				{
				 build.s [nrow][ncolumn+1].count++;	//
				 build.s [nrow][ncolumn-1].count++;
				 build.s [nrow-1][ncolumn-1].count++;
				 build.s [nrow-1][ncolumn].count++;
				 build.s [nrow-1][ncolumn+1].count++;
				}// end 
			else if((nrow)==(row-1)&&ncolumn==0)
				{
				 build.s [nrow][ncolumn+1].count++;	//
				 build.s [nrow-1][ncolumn].count++;
				 build.s [nrow-1][ncolumn+1].count++;
				}// end if
			else if((ncolumn)==0)
				{
				 build.s [nrow][ncolumn+1].count++;	//
		 		 build.s [nrow+1][ncolumn+1].count++;	 
				 build.s [nrow+1][ncolumn].count++;	
				 build.s [nrow-1][ncolumn].count++;
				 build.s [nrow-1][ncolumn+1].count++;
				}//end if(nrow==0)
			else if((nrow)==0&&ncolumn==0)
				{
				 build.s [nrow][ncolumn+1].count++;	//
		 		 build.s [nrow+1][ncolumn+1].count++;	 
				 build.s [nrow+1][ncolumn].count++;	
				}//end if(nrow==0&&ncolumn==0)
			else if((nrow)==0)
				{
				 build.s [nrow][ncolumn+1].count++;	//
		 		 build.s [nrow+1][ncolumn+1].count++;	
				 build.s [nrow+1][ncolumn].count++;	//
				 build.s [nrow+1][ncolumn-1].count++;
				 build.s [nrow][ncolumn-1].count++;
				}//end if(nrow==0)
			else if((nrow)==0&&ncolumn==(column-1))
				{
				 build.s [nrow+1][ncolumn].count++;	
				 build.s [nrow+1][ncolumn-1].count++;
				 build.s [nrow][ncolumn-1].count++;
	
				}//end if(nrow==0&&ncolumn==0)
			else 
				{
				 build.s [nrow][ncolumn+1].count++;	//
		 		 build.s [nrow+1][ncolumn+1].count++;	//  
				 build.s [nrow+1][ncolumn].count++;	
				 build.s [nrow+1][ncolumn-1].count++;
				 build.s [nrow][ncolumn-1].count++;
				 build.s [nrow-1][ncolumn-1].count++;
				 build.s [nrow-1][ncolumn].count++;
				 build.s [nrow-1][ncolumn+1].count++;
				}
			} catch(Exception ex){/*ex.printStackTrace();*/}	 
		//	}//end if	*/
			}//end while
		
		for ( int i=0; i<build.s.length;i++)		// displays the numbers											
        	{
        	 for (int j=0; j<build.s.length;j++)
				{
				 if(build.s[i][j].mine == false)
				 	{
		
			 	 	 if (build.s[i][j].count==0)
			 			{
			 			 ImageIcon blank = new ImageIcon("images/blank.jpg");
		    			 build.s[i][j].setDisabledIcon(blank);
		    			 build.s[i][j].isblank=true;
			 			}//end if
				 	if (build.s[i][j].count==1)
				 		{
				 	 	 ImageIcon mine1 = new ImageIcon("images/1mine.jpg");
		    			 build.s[i][j].setDisabledIcon(mine1);
				 		}//end if
				    if (build.s[i][j].count==2)
				 		{
				 		 ImageIcon mine2 = new ImageIcon("images/2mine.jpg");
		    			 build.s[i][j].setDisabledIcon(mine2);
				 		} //end if
				 	if (build.s[i][j].count==3)
				 		{
				 		 ImageIcon mine3 = new ImageIcon("images/3mine.jpg");
		    			 build.s[i][j].setDisabledIcon(mine3);
				 		}//end if
				 	if (build.s[i][j].count==4)
				 		{
				 		 ImageIcon mine4 = new ImageIcon("images/4mine.jpg");
		    			 build.s[i][j].setDisabledIcon(mine4);
				 		}//end if
				 	if (build.s[i][j].count==5)
				 		{
				 		 ImageIcon mine5 = new ImageIcon("images/5mine.jpg");
		    			 build.s[i][j].setDisabledIcon(mine5);
				 		}//end if
				 	if (build.s[i][j].count==6)
				 		{
				 		 ImageIcon mine6 = new ImageIcon("images/6mine.jpg");
		    			 build.s[i][j].setDisabledIcon(mine6);
				 		} //end if
				 	if (build.s[i][j].count==7)
				 		{
				 		 ImageIcon mine7 = new ImageIcon("images/7mine.jpg");
		    			 build.s[i][j].setDisabledIcon(mine7);
				 		}//end if
				 	if (build.s[i][j].count==8)
				 		{
				 		 ImageIcon mine8 = new ImageIcon("images/8mine.jpg");
		    			 build.s[i][j].setDisabledIcon(mine8);
				 		}//end if
				 	}//end if(s[i][j].mine == false)
					} //end for(int j=0; j<s.length;j++)
				}  //end for(int i=0; i<s.length;i++)
			 			 	 						
	}// end constructor
/**
 *randomInt chenerates a random number between 0 and n-1
 *@param n a random number less than n
 */	
//----------Return random integer in range 0 to n-1. -------------------------
    	private static int randomInt (int n) 
    		{
         	 return (int)(n * Math.random());
    		}
   	
}//end class

⌨️ 快捷键说明

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