# MineSweeper!

• 02-20-2010, 02:34 PM
CrushOverwrite
MineSweeper!
can someone help me on doing this program....

my teacher gave me this .txt file (notepad)

it contains

Code:

```XXX...XX... XX..XXX.... XX..XX..XX```
something like that.. the X represents bombs and the dots represent the number of how many bombs there are around it.. for short..its the mine sweeper game..
someone please help me do this.. i need to read the .txt file as the input.. all i need is to make the dots to numbers.. my teacher said i must use Two Dimensional array.. someone please help :) thank you
• 02-20-2010, 02:51 PM
Fubarable
Quote:

Originally Posted by CrushOverwrite
can someone help me on doing this program....

my teacher gave me this .txt file (notepad)

it contains
\
XXX...XX...
XX..XXX....
XX..XX..XX

something like that.. the X represents bombs and the dots represent the number of how many bombs there are around it.. for short..its the mine sweeper game..
someone please help me do this.. i need to read the .txt file as the input.. all i need is to make the dots to numbers.. my teacher said i must use Two Dimensional array.. someone please help :) thank you

Hello CrushOverwrite and welcome to the forum.

So please, tell us what you've done so far, and what specific steps you're having problems with. So far all you've done is post your assignment and in this situation, usually the best we can do is point you to the tutorials. If you can give us a more specific question, we'll likely be able to help you out better with a more specific answer.

Much luck!
• 02-20-2010, 03:59 PM
CrushOverwrite
ohhh i see.. im sorry just new to java and so with the forums :)

lets see.. i would like to ask how do i put the input (which is in the notepad which contains
XXX..XX..
...XX..XX
..XX...XX.)

i just need to know how to put it in an array.. and i know ill try to do the rest..

(im sorry i suck at explaining.. heres an exmaple of what i need to know)
.XXX.
X...X
.X.X.

int board[][]=new int[3][5];

how do i put the first "." to [0][0]
• 02-20-2010, 04:01 PM
CrushOverwrite
ugh! hahah..im really noob sorry.. its like i really need to start from scratch.. im the only one in class who doesnt know how to program and it irritates me.. thats why i think forums would help me :-p
• 02-20-2010, 07:58 PM
twiggy62
If you are hard coding it, wouldn't it be:

Code:

`int[][] board = new int[3][5];`
Which creates 3 rows, and 5 columns.

Then to fill:

Code:

```board[0][0] = "."; board[0][1] = "X"; board[0][2] = "X";```
etc.
etc.
etc.

If you are not hard coding it, and have to read it from the file, then the procedure will be a bit different.
• 02-20-2010, 09:15 PM
Fubarable
What are you supposed to use to read in the lines? A Scanner object or a BufferedReader?

I'd read one line into a String and then use a for loop to go from 0 to the String's length (which you get via the String's length() method), and then use the String method charAt(int index) to extract the character at that spot in the line. Then using that information fill in your array at location [line number][charAt number].
• 02-21-2010, 04:54 AM
CrushOverwrite
lol hehe..thay would be easy dude.. but what if the content of the input file is changed (input.txt) then i have to manually change the arrays too right? hehe.. i need to read the input.txt file then put it in an array..
• 02-21-2010, 04:56 AM
CrushOverwrite
Fubarable..hey thanks!.. hmm .. i dont know.. classmates are using scanner.. friends say i must use bufferedreader.. ill try to make something.. cuz my idea is that.. im gonna call this method to check if it is a bomb or not by using if statement..i dont know haha..
• 02-21-2010, 05:01 AM
Fubarable
Scanner may be perhaps a little easier to use, though it has its trips and traps. Check out the Sun/Oracle tutorial on this. Scanning

Will the text file tell you how many lines are present, or will you just have to read it and find out?
• 02-22-2010, 10:40 AM
CrushOverwrite
15 20(15 down,20 horizon)
its like
X...X...X..X..X..X..
X
X
.
.
.
.
.
X
X
X
X
.
.
X
• 02-22-2010, 02:44 PM
Fubarable
... and your latest code attempt?
• 02-23-2010, 06:45 PM
CrushOverwrite
im sorry im noob at java haha.here i made it...
//btw you could try it to help me =P
//create the txt file. name it as minesweeper-input1.txt then it contains as follows
15 20
XXX.....X.X.X....X.X
X.....XX....X....X..
X..XX.XXXX..X.XX.X..
X...X..XXX.X.X......
X...X.X....X...X..X.
......X...XXXXXXX...
......X...X.X...X.X.
..XXX.X...X..X.....X
XX.X.X..X.X..X...X..
X......X.X..........
.....XXX..X...X..X..
.X......X...X..X....
...X..X.....X...XX..
.X..X.XX.X......XXXX
XX..X.X.....X..X.XX.

Code:

```import java.io.*; public class bobo {     public static void main(String[]args)throws IOException {                         BufferedReader br = new BufferedReader(new FileReader("minesweeper-input1.txt"));             String [] dimention = br.readLine().split(" ");               int row = Integer.parseInt(dimention[0]);             int column = Integer.parseInt(dimention[1]);             char [][] board = new char[row][column];             String [] tempBoard = new String[row];                         for(int i = 0;i<row;i++){                         tempBoard [i] = br.readLine();             }                         for(int i = 0;i<row;i++){                     board[i]=tempBoard[i].toCharArray();             }             for(int i = 0;i<row;i++){                     System.out.println(board[i]);             }                                     for(int i=0;i<row;i++){                     for(int ii=0;ii<row;ii++){                                   if(board[i][ii]=='X'){                                         System.out.print("X");                                                               }else{                                     int bomb=0;                                                                                 try{                                         if(board[i][ii+1]=='X'){                                                 bomb++;                                         }}catch(ArrayIndexOutOfBoundsException e){                                                                        }                                                                                 try{if(board[i-1][ii]=='X'){                                         bomb++;                                                                         }}catch(ArrayIndexOutOfBoundsException e){                                                                        }                                                                                 try{if(board[i][ii-1]=='X'){                                         bomb++;                                                                                         }}catch(ArrayIndexOutOfBoundsException e){                                                                        }                                                                                                                         try{if(board[i+1][ii]=='X'){                                         bomb++;                                                 }}catch(ArrayIndexOutOfBoundsException e){                                                                        }                                                                                 try{if(board[i+1][ii-1]=='X'){                                         bomb++;                                                 }}catch(ArrayIndexOutOfBoundsException e){                                                                        }                                                                                                                         try{if(board[i+1][ii+1]=='X'){                                         bomb++;                                         }}catch(ArrayIndexOutOfBoundsException e){                                                                        }                                                                                 try{if(board[i-1][ii-1]=='X'){                                         bomb++;                                         }}catch(ArrayIndexOutOfBoundsException e){                                                                        }                                                                                 try{if(board[i-1][ii+1]=='X'){                                         bomb++;                                         }}catch(ArrayIndexOutOfBoundsException e){                                                                        }                                                 System.out.print(bomb);                                                     }                     }             }                             }         }```
//i made it with the help of a patient friend,my only problem is that.. i dont know how to print it by 15's by that i mean
it will print from left to right 15 chars or ints then it will go down the next line to print another 15 chars or ints..

its like

XXXXXXXXXX....X
then next line
XXX...XXXXXX...X

so that the output would look something like
XXXXXXXXXX....X
XXX...XXXXXX...X
XXXXXXXXXX....XXXX...XXXXXX...X(<<<<<mine looks like this haha)

• 02-23-2010, 07:02 PM
Fubarable
Several issues here:
1) What's with catching array index out of bounds? That's no good, so get rid of those try/catch blocks but instead write code that doesn't cause this exception.
2) I've added code tags to help make your code readable, but even so, it's hard to read due to it's non-standard formatting. Many here will not try to read code if it's hard to read.

Hopefully other suggestions will be forthcoming by me or others, but until then, you may wish to work on the suggestions above. Much luck.
• 02-23-2010, 07:17 PM
Fubarable
To solve your printing problem, all you have to do is add System.out.println(); (to print a new line) at the bottom of the outer of the nested for loops. I'll let you experiment to see exactly where to place it.

Much luck!
• 02-23-2010, 07:28 PM
Fubarable
Your output is incorrect as it truncates the last 5 characters from each line.
• 02-23-2010, 08:10 PM
Fubarable
A solution that worked for me was to iterate through the board[][] array using nested for loops (as you're already doing:

Code:

```    for (int i = 0; i < board.length; i++) {       for (int j = 0; j < board[i].length; j++) {         if (board[i][j] == 'X') {           System.out.print('X'); // same as you         } else {           // (1) more code goes in here         }       }       System.out.println();     }```
and then at the spot marked (1), I use another nested pair of for loops going from i - 1 to i + 1, the inner loop going from j - 1 to j + 1, but being careful not to go below 0 or above the maximum row or column limit. To avoid going over or under, I use the Math.max and Math.min methods. Do this and there is no need for those kludgy try/catch blocks.
• 02-24-2010, 03:31 PM
CrushOverwrite
Thanks!!! =)
hahaha lol weve submitted the minesweeper.java a while ago..
im happy i made it lol.. i change the "row" in the for loop to "column" i almost didnt see that... and yah i just aded the println();
and another thing i didnt know that when you run the program an outpul file must be produced.. so a while ago they taught me about the PrintWriter thingy..

hmm yahh i suck at coding its so hard to read..

and yes again.. many made their java with the one you suggested (on how you made the for loop)