Created
October 28, 2012 08:59
-
-
Save yao2030/3968101 to your computer and use it in GitHub Desktop.
mine sweeper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Mine | |
{ | |
public static void main(String[] args) | |
{ | |
int M = Integer.parseInt(args[0]); | |
int N = Integer.parseInt(args[1]); | |
double p = Double.parseDouble(args[2]); | |
boolean[][] mine = new boolean[M+2][M+2]; | |
for(int i = 1; i <= M; i++) | |
{ | |
for(int j = 1; j <= N; j++) | |
{ | |
if(Math.random() < p) mine[i][j] = true; | |
} | |
} | |
for(int i = 1; i <= M; i++) | |
{ | |
for(int j = 1; j <= N; j++) | |
{ | |
if(mine[i][j]) System.out.print("* "); | |
else System.out.print(". "); | |
} | |
System.out.println(); | |
} | |
System.out.println(); | |
for(int i = 1; i <= M; i++) | |
{ | |
for(int j = 1; j <= N; j++) | |
{ | |
int count = 0;tory | |
if(mine[i-1][j]) count++; | |
if(mine[i+1][j]) count++; | |
if(mine[i][j+1]) count++; | |
if(mine[i][j-1]) count++; | |
if(mine[i-1][j-1]) count++; | |
if(mine[i+1][j-1]) count++; | |
if(mine[i+1][j+1]) count++; | |
if(mine[i-1][j+1]) count++; | |
if(!mine[i][j])System.out.print(count + " "); | |
else System.out.print("* "); | |
} | |
System.out.println(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment