Actually I try to solve this problem wich is MineSweeper with Java but it doesn't work !!
I consider there are several versions of this problem and the version I try to solve is here :
http://acm.pku.edu.cn/JudgeOnline/problem?id=2612
Therefore I found a C program which solve the problem and I take it as a model to write correctly my program in Java.
Here it's the program in C :
Code: Select all
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a[11][15];
char b[11][15];
int m,i,j,c,d,flag,count;
while (scanf("%d",&m)!=EOF)
{
for (i=0;i<m;i++)
{
scanf("%s",a[i]);
}
for (i=0;i<m;i++)
{
scanf("%s",b[i]);
}
flag=0;
for (i=0;i<m;i++)
{
for (j=0;j<m;j++)
{
if (b[i][j]=='x'&&a[i][j]=='*')
{
flag=1;
goto exit;
}
}
}
exit:
;
if (flag==1)
{
for (i=0;i<m;i++)
{
for (j=0;j<m;j++)
{
if (b[i][j]=='x'&&a[i][j]!='*')
{
count=0;
if (c==0)
{
for (c=i;c<=i+1;c++)
{
if (j==0)
{
for (d=j;j<=j+1;j++)
{
if (a[c][d]=='*')
{
count++;
}
}
}
else
{
for (d=j-1;j<=j+1;j++)
{
if (a[c][d]=='*')
{
count++;
}
}
}
}
}
else
{
for (c=i-1;c<=i+1;c++)
{
if (j==0)
{
for (d=j;d<=j+1;d++)
{
if (a[c][d]=='*')
{
count++;
}
}
}
else
{
for (d=j-1;d<=j+1;d++)
{
if (a[c][d]=='*')
{
count++;
}
}
}
}
}
printf("%d",count);
}
if (b[i][j]=='x'&&a[i][j]=='*')
{
printf("*");
}
if (b[i][j]=='.')
{
if (a[i][j]=='*')
{
printf("*");
}
else
{
printf(".");
}
}
}
printf("\n");
}
printf("\n");
}
else
{
for (i=0;i<m;i++)
{
for (j=0;j<m;j++)
{
if (b[i][j]=='x')
{
count=0;
if (c==0)
{
for (c=i;c<=i+1;c++)
{
if (j==0)
{
for (d=j;j<=j+1;j++)
{
if (a[c][d]=='*')
{
count++;
}
}
}
else
{
for (d=j-1;j<=j+1;j++)
{
if (a[c][d]=='*')
{
count++;
}
}
}
}
}
else
{
for (c=i-1;c<=i+1;c++)
{
if (j==0)
{
for (d=j;d<=j+1;d++)
{
if (a[c][d]=='*')
{
count++;
}
}
}
else
{
for (d=j-1;d<=j+1;d++)
{
if (a[c][d]=='*')
{
count++;
}
}
}
}
}
printf("%d",count);
}
if (b[i][j]=='.')
{
printf(".");
}
}
printf("\n");
}
printf("\n");
}
}
return 0;
}
And here my code in Java
Code: Select all
import java.io.*;
public class Main
{
static int ScanAroundCase(int Taille, char[][] Plateau, int y, int x)
{
int CaseValue = 0;
if (y - 1 >= 0 && x - 1 >= 0)
{
if (Plateau[y - 1][x - 1] == '*')
{
CaseValue++;
}
}
if (y - 1 >= 0)
{
if (Plateau[y - 1][x] == '*')
{
CaseValue++;
}
}
if (y - 1 >= 0 && x + 1 < Taille)
{
if (Plateau[y - 1][x + 1] == '*')
{
CaseValue++;
}
}
if (x - 1 >= 0)
{
if (Plateau[y][x - 1] == '*')
{
CaseValue++;
}
}
if (x + 1 < Taille)
{
if (Plateau[y][x + 1] == '*')
{
CaseValue++;
}
}
if (y + 1 < Taille && x - 1 >= 0)
{
if (Plateau[y + 1][x - 1] == '*')
{
CaseValue++;
}
}
if (y + 1 < Taille)
{
if (Plateau[y + 1][x] == '*')
{
CaseValue++;
}
}
if (y + 1 < Taille && x + 1 < Taille)
{
if (Plateau[y + 1][x + 1] == '*')
{
CaseValue++;
}
}
return CaseValue;
}
static String[][] BalayagePlateau(int Taille, char[][] Plateau, char[][] TabEntrees)
{
int i = 0, j = 0, flag = 0;
String MinesScan[][] = new String[Taille][Taille];
while (i < Taille)
{
while (j < Taille)
{
if (TabEntrees[i][j] == 'x')
{
if (Plateau[i][j] == '*')
{
flag = 1;
}
else
{
MinesScan[i][j] = Integer.toString(ScanAroundCase(Taille, Plateau, i, j));
}
}
if (Plateau[i][j] == '*')
{
if (flag == 1)
{
MinesScan[i][j] = "*";
}
else
{
MinesScan[i][j] = ".";
}
}
else if (TabEntrees[i][j] != 'x')
{
MinesScan[i][j] = ".";
}
j++;
}
j = 0;
i++;
}
i = 0;
j = 0;
if (flag == 1)
{
while (i < Taille)
{
while (j < Taille)
{
if (Plateau[i][j] == '*')
{
MinesScan[i][j] = "*";
}
j++;
}
i++;
j = 0;
}
}
return MinesScan;
}
static char[][] CreerTabEntrees(int Taille)
{
BufferedReader saisie = new BufferedReader(new InputStreamReader(System.in));
char[][] Entrees = new char[Taille][Taille];
String chaine = "";
int i = 0, j = 0;
try
{
while ( i < Taille)
{
chaine = saisie.readLine();
while (j < Taille)
{
Entrees[i][j] = chaine.charAt(j);
j++;
}
j = 0;
i++;
}
}
catch(Exception excp)
{
}
return Entrees;
}
static char[][] CreerPlateau(int Taille)
{
BufferedReader saisie = new BufferedReader(new InputStreamReader(System.in));
char[][] Plateau = new char[Taille][Taille];
String chaine = "";
int i = 0, j = 0;
try
{
while (i < Taille)
{
chaine = saisie.readLine();
while (j < Taille)
{
Plateau[i][j] = chaine.charAt(j);
j++;
}
j = 0;
i++;
}
}
catch(Exception excp)
{
}
return Plateau;
}
public static void main(String[] args)
{
BufferedReader saisie = new BufferedReader(new InputStreamReader(System.in));
String[][] Plateau, TabEntrees;
String chaine = "";
int Taille = 0, i = 0, j = 0;
try
{
while(true)
{
Taille = Integer.parseInt(saisie.readLine());
Plateau = new String[Taille][Taille];
Plateau = BalayagePlateau(Taille, CreerPlateau(Taille), CreerTabEntrees(Taille));
if (Taille >= 0)
{
while (i < Taille)
{
while (j < Taille)
{
System.out.print(Plateau[i][j]);
j++;
}
System.out.println();
j = 0;
i++;
}
System.out.println();
i = 0;
j = 0;
}
else
{
System.out.println();
}
}
}
catch(Exception excp)
{
}
}
}
I try a lot of possibilities and my program respect the problem's rules but it doesn't work !!
I don't understand why !!
Could you help me to found my errors ?
Thanks for your help !!
Plz give me hints at least !!