Results 1 to 18 of 18

Thread: MineSweeper!

  1. #1
    Join Date
    Feb 2010
    Posts
    9
    Rep Power
    0

    Default MineSweeper!

    can someone help me on doing this program....

    my teacher gave me this .txt file (notepad)

    it contains

    Java 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
    Last edited by Fubarable; 02-20-2010 at 08:08 PM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by CrushOverwrite View Post
    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!

  3. #3
    Join Date
    Feb 2010
    Posts
    9
    Rep Power
    0

    Default

    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]

  4. #4
    Join Date
    Feb 2010
    Posts
    9
    Rep Power
    0

    Default

    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

  5. #5
    twiggy62's Avatar
    twiggy62 is offline Member
    Join Date
    Feb 2010
    Posts
    26
    Rep Power
    0

    Default

    If you are hard coding it, wouldn't it be:

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

    Then to fill:

    Java 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.
    Last edited by twiggy62; 02-20-2010 at 11:35 PM.
    The biggest room in the world, is room for improvement.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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].

  7. #7
    Join Date
    Feb 2010
    Posts
    9
    Rep Power
    0

    Default

    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..

  8. #8
    Join Date
    Feb 2010
    Posts
    9
    Rep Power
    0

    Default

    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..

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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?

  10. #10
    Join Date
    Feb 2010
    Posts
    9
    Rep Power
    0

    Default

    hmm.. the note pad says
    15 20(15 down,20 horizon)
    its like
    X...X...X..X..X..X..
    X
    X
    .
    .
    .
    .
    .
    X
    X
    X
    X
    .
    .
    X

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  12. #12
    Join Date
    Feb 2010
    Posts
    9
    Rep Power
    0

    Default 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.




    Java 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
    instead of
    XXXXXXXXXX....XXXX...XXXXXX...X(<<<<<mine looks like this haha)

    Moderator edit: code tags added to help improve readability
    Last edited by Fubarable; 02-23-2010 at 05:56 PM. Reason: Code tags added

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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.

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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!

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Your output is incorrect as it truncates the last 5 characters from each line.

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    A solution that worked for me was to iterate through the board[][] array using nested for loops (as you're already doing:

    Java 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.

  17. #17
    Join Date
    Feb 2010
    Posts
    9
    Rep Power
    0

    Default 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)

    haha man your good..

    dude. theres an upcoming project our teacher is about to give on friday.. and i dont know what is wrong with him that he chose me to be the leader of one group.. if i need help can i ask you for things? hahaha

    BTw.. THANKS ALOT!!

  18. #18
    Join Date
    Feb 2010
    Posts
    9
    Rep Power
    0

    Default

    and i forgot.. yah i dont know any other way to do it without using try catch.. and i dont know.. our teacher said were suppose to use try catch ArrayIndexOutOfBoundsE

    and again thanks duude!

    the code that you have shown, many did it dat way.. haha lol you guys are pro

Similar Threads

  1. Replies: 2
    Last Post: 08-05-2010, 04:49 AM
  2. Minesweeper problem! need help!
    By jeffrey in forum New To Java
    Replies: 1
    Last Post: 10-02-2009, 04:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •