Java Practice (simulation minesweeper game)

Source: Internet
Author: User

To lay mines for minesweeper games, the minesweeper panel for minesweeper can be represented by a two-dimensional int array. If a location is a mine, the position is represented by a number-1,
If the location is not a mine, it is temporarily represented by the number 0.
The program completes the operation of random mines in the two-dimensional array, and the program reads 3 parameters: Number of rows (r) of the Bray panel, number of columns (c), number of mines laid out (N),
And to meet the 0<n<r*c*0.75 (that is, the maximum density of the laying of mines is 75%), the program after the operation of the N mines randomly placed in a two-dimensional array of r*c, after the completion of the game of minesweeper.

ImportJava.util.*; Public classMinesweeper { Public Static voidMain (string[] args) {Scanner SCN=NewScanner (system.in); //Enter rows and columns        intR =Integer.parseint (Scn.nextline ()); intc =Integer.parseint (Scn.nextline ()); if(R<2 | | c<2) {System.out.println ("The rows entered are not valid. "); return; }        //define a two-dimensional array to Bray        int[] A =New int[R][c]; Random Rnd=NewRandom (); //give the number of thunder randomly        intn = Rnd.nextint ((int) (r*c*0.75)); System.out.println ("Number of Thunder:" +N);  while(n>0)        {            //random mines, randomly generated mines in rows and columns            intRR =Rnd.nextint (R); intCC =Rnd.nextint (c); if(A[rr][cc]!=-1) {A[RR][CC]=-1; N--; }        }        //Demining         for(inti=0;i<r;i++)        {             for(intj=0;j<c;j++)            {                intLei=0; if(A[i][j] = = 0)                {                    if(i-1>=0 && j-1>=0 && a[i-1][j-1]==-1) Lei++; if(I-1>=0 && A[i-1][j]==-1) Lei++; if(j<c-1)                    {                        if(I-1>=0 && A[i-1][j+1]==-1) Lei++; if(A[i][j+1]==-1) Lei++; }                    if(J-1>=0 && A[i][j-1]==-1) Lei++; if(i<r-1)                    {                        if(J-1>=0 && A[i+1][j-1]==-1) Lei++; if(A[i+1][j]==-1) Lei++; }                    if(I<r-1 && j<c-1)                    {                        if(i+1>=1 && j+1>=1 && a[i+1][j+1]==-1) Lei++; } A[i][j]=Lei; }            }        }        //Output         for(inti=0;i<r;i++)        {             for(intj=0;j<c;j++) {System.out.print (A[i][j]+ "\ T");        } System.out.println (); }    }}

Java Practice (simulation minesweeper game)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.