Skip to content

Instantly share code, notes, and snippets.

@yao2030
Created October 28, 2012 08:59
Show Gist options
  • Save yao2030/3968101 to your computer and use it in GitHub Desktop.
Save yao2030/3968101 to your computer and use it in GitHub Desktop.
mine sweeper
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