# minesweeper game creation problem

• June 17th, 2009, 12:11 AM
kaylors
minesweeper game creation problem
okay so the last problem was solved. thanks to freakychris for that(although i did manage to figure it out before he replied, but still. so now i got a new problem. and if this one comes through, then finally my game will be complete and you guys can play it if you want. mind u its supposed to work exactly like windows minesweeper. only its not gui.

Code :

```import java.io.*; class minesweeper { BufferedReader br=new BufferedReader(new InputStreamReader(System.in));   char Mine[][]=new char[20][20]; char Field[][]=new char[20][20];   boolean cond=true;   int x,y,r1,r2; char cho;   void Miner() { for(int i=0;i<20;i++) { for(int j=0;j<20;j++) { Field[i][j]='#'; double rand=Math.random()*100; if(rand>r1 && rand<r2) Mine[i][j]='@'; else Mine[i][j]='#'; } } }   void ValFiller() { for(int i=0;i<20;i++) { for(int j=0;j<20;j++) { if(Mine[i][j]!='@') Mine[i][j]=Char(Seeker(i,j)); } } }   char Char(int g) { switch(g) { case 1: return '1'; case 2: return '2'; case 3: return '3'; case 4: return '4'; case 5: return '5'; case 6: return '6'; case 7: return '7'; case 8: return '8'; } return '0'; }   int Seeker(int i,int j) { int MineVal=0; for(int k=i-1;k<=i+1;k++) { for(int l=j-1;l<=j+1;l++) { if(k>=0 && l>=0&& k<20 && l<20) { if(Mine[k][l]=='@') MineVal++; } } } return MineVal; }   void Exhibit() { System.out.println(); for(int i=0;i<20;i++) { for(int j=0;j<20;j++) { System.out.print(Field[i][j]+" "); } System.out.println(); } }   void ExhibitM() { System.out.println(); for(int i=0;i<20;i++) { for(int j=0;j<20;j++) { System.out.print(Mine[i][j]+" "); } System.out.println(); } }   void Coords()throws IOException { System.out.println("Enter Co-ordinates:"); System.out.print("X:");   x=Integer.parseInt(br.readLine()); System.out.print("Y:");   y=Integer.parseInt(br.readLine()); }   void Choice(char c)throws IOException { System.out.println("What would you like to do?"); System.out.println("R.Reveal the Cell."); System.out.println("F.Flag the Cell as Mine."); System.out.print("Choice:"); cho=(char)br.read();   }   void Action() { if(cho=='F') Field[x][y]='F'; else if(cho=='R') { if(Mine[x][y]=='@') { Detonate(); cond=false; } else { Field[x][y]=Mine[x][y]; Reveal(); } } }   boolean Adja(int i,int j) {   if(i+1>=0 && i+1<20) { if(Field[i+1][j]!='#') return true; } if(i-1>=0 && i-1<20) { if(Field[i-1][j]!='#') return true; } if(j+1>=0 && j+1<20) { if(Field[i][j+1]!='#') return true; } if(j-1>=0 && j-1<20) { if(Field[i][j-1]!='#') return true; } return false; }   void Reveal() { for(int i=0;i<20;i++) { for(int j=0;j<20;j++) { if(Mine[i][j]!='@' && Adja(i,j)) Field[i][j]=Mine[i][j]; } }   for(int i=19;i>=0;i--) { for(int j=19;j>=0;j--) { if(Mine[i][j]!='@' && Adja(i,j)) Field[i][j]=Mine[i][j]; } } }   void DiffLvl()throws IOException { System.out.println("E.Easy"); System.out.println("M.Medium"); System.out.println("H.Hard"); System.out.print("Enter your choice:"); char ch=(char)(br.read()); if(ch=='E') r1=50; r2=60; if(ch=='M') r1=35; r2=70; if(ch=='H') r1=30; r2=70; }   void Play()throws IOException { System.out.println("Welcome to ShaunSweeper"); System.out.println("Choose a difficulty level:"); DiffLvl(); Miner(); ValFiller();   while(cond) { Exhibit(); System.out.println(); Coords(); System.out.println(); Choice(); Action();   } }   void Activate()throws IOException { Play(); }   void Detonate() { for(int i=0;i<20;i++) { for(int j=0;j<20;j++) { if(Mine[i][j]=='@') Field[i][j]='@'; } } Exhibit(); cond=false; } }```

theres no use my babbling too much. the program works flawlessly...almost. there seems to be something working with the entry method. try it tout and see the error i get. and please see if u can solve it. i've used blue j for this and hope u can use it too.
• June 19th, 2009, 10:34 AM
JavaPF
Re: minesweeper game creation problem

Code :

``` public static void main(String[] args){   minesweeper m = new minesweeper(); m.Miner(); m.Exhibit(); m.ValFiller();   }```

To get your code to run in Eclipse. It seems to run OK for me.. I'm not really sure what the problem is?
• June 19th, 2009, 05:36 PM
Freaky Chris
Re: minesweeper game creation problem
I think the phrase oops, springs to mind.

Code :

` for(int l=j-1;j<j+1;j++)`

See what you have done wrong :P
...
I'll give you a clue, J will always be less than J+1 xD

Regards,
Chris
• June 24th, 2009, 06:47 AM
kaylors
Re: minesweeper game creation problem
lol, i see i see, yes that is what i have done. and now i have corected it. thanks anyway guys.
• June 24th, 2009, 08:14 AM
JavaPF
Re: minesweeper game creation problem
Quote:

Originally Posted by Freaky Chris
I think the phrase oops, springs to mind.

Code :

` for(int l=j-1;j<j+1;j++)`
See what you have done wrong :P
...
I'll give you a clue, J will always be less than J+1 xD

Regards,
Chris

Ah good work Chris. I overlooked that :-t
• June 27th, 2009, 04:06 PM
Freaky Chris
Re: minesweeper game creation problem
p.s can you please mark this as solved :)

Thanks,
Chris