Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Problem in my 2-Dimensional Array Board Game

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    1
    My Mood
    Amused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem in my 2-Dimensional Array Board Game

    I'm suppose to create a 10X10 board, using a 2-dimensional array with 35 random blocked squares ('b') implemented. The goal of the program is, by using recursion, I'm suppose to try to find a path from the bottom left square of the board ([9][0]) to any of the squares at the top of the board ([0][0-9]) leaving a trail of 'x's throughout my movements. Once I find a path all the way to the top, if there is an empty space in one of the top squares, then I mark the square as 'x' and return 1(True) as a result. Else, if there is a 'b' char already in the square, then I return 0(False) as a result.

    At the start of the path find game, I'm suppose to check to see if the starting square ([9][0]) has a 'b' or not. If it has a 'b' char, then it automatically returns a 0 and terminates. But if it has en empty space, I mark an 'x' and continue on. Using recursion, I am suppose to look for a path UP, DOWN, LEFT, and RIGHT. If the square already is blocked 'b', then I can't go that way. If it has an empty space, then I mark an 'x' and continue trying to find a path towards the top.

    I am suppose to display the board before I try to find a path, then after I attempt to find a path up the board, I have to display the result I got and what the board looks like after I found my result -- with a path of 'x's included throughout the board until the last spot where I was terminated.

    Here's my Class code...

    import java.util.Random;
    import java.util.Scanner;
     
    public class Board {
    	private int Row; // Row of the board game
    	private int Column; // Column of the board game
    	private char[][] boardInterface;
    	private boolean checkedStartAlready;
    	Scanner input = new Scanner(System.in);
     
    	// Make a constructor with no arguments that sets the board
    	public Board() {
    		Row = 10;
    		Column = 10;
    		checkedStartAlready = false;
    		boardInterface = new char[Row][Column]; // Initialize board
    		getBoard(); // Generate board with 35 random 'b's
    	}
     
    	// Make a method to get the board
    	public void getBoard() {
    		int placedACharB = 0;
     
    		// Make generator for random numbers
    		Random random = new Random();
    		int generator;
     
    		// Create a loop that sets the character b 35 times in random places
    		for (Row = 0; Row < 10; Row++) {
    			for (Column = 0; Column < 10; Column++) {
    				generator = random.nextInt(3);
    				if ((generator == 0) && (placedACharB < 35)) {
    					boardInterface[Row][Column] = 'b';
    					placedACharB++;
    				} else
    					boardInterface[Row][Column] = ' ';
    			}
    		}
     
    	}
     
    	public int findPath(int currentRow, int currentColumn) {
     
    		// check starting point
    		while (checkedStartAlready == false) {
    			if (checkStartPoint(currentRow, currentColumn) == false)
    				return 0;
    			else
    				checkedStartAlready = true;
    		}
     
    		// check to see if row and column are out of bounds
    		if ((currentRow < 0) || (currentRow > 9))
    			return 0;
    		else if ((currentColumn < 0) || (currentColumn > 9))
    			return 0;
     
    		// check to see if made it to the top
    		while (currentRow == 0) {
    			if (boardInterface[currentRow][currentColumn] == 'b') {
    				return 0;
    			} else {
    				boardInterface[currentRow][currentColumn] = 'x';
    				return 1;
    			}
    		}
     
    		// check to see if space has b or already has x -- if not then mark an x
    		// and continue
    		if (boardInterface[currentRow][currentColumn] == 'b')
    			return 0;
    		else if (boardInterface[currentRow][currentColumn] == 'x')
    			return 0;
    		else {
     
    			// Place new 'x' in empty space of array and continue path
    			boardInterface[currentRow][currentColumn] = 'x';
     
    			// Find next open path -- Look UP, DOWN, LEFT, RIGHT
    			return findPath(currentRow--, currentColumn)
    					+ findPath(currentRow++, currentColumn)
    					+ findPath(currentRow, currentColumn--)
    					+ findPath(currentRow, currentColumn++);
     
    		}
     
    	}
     
    	// Method to check the starting point
    	public boolean checkStartPoint(int startRow, int startColumn) {
    		if (boardInterface[startRow][startColumn] == 'b')
    			return false;
    		else {
    			boardInterface[startRow][startColumn] = 'x';
    			return true;
    		}
    	}
     
    	// Method to display the board
    	public void displayBoard() {
    		for (Row = 0; Row < 10; Row++) {
    			for (Column = 0; Column < 10; Column++) {
    				// print out character
    				System.out.print(boardInterface[Row][Column]);
    			}
    			// if column reaches the end, go to next row
    			System.out.println();
    		}
    		System.out.println();
     
    	}
     
    }

    Here's my Main code...

    public class BoardTester {
     
    	public static void main(String[] args) {
    		Board myBoard = new Board();
    		int startRow = 9;
    		int startColumn = 0;
    		int pathResult;
     
    		// Display the board
    		myBoard.displayBoard();
     
    		// Find path in the board and return result (True == 1, False == 0)
    		pathResult = myBoard.findPath(startRow, startColumn);
     
    		if (pathResult == 0)
    			System.out.println("False");
    		else
    			System.out.println("True");
     
    		// Display board after path results with marked x's INCLUDED
    		myBoard.displayBoard();
     
    	}
     
    }

    Here's an example of what my output SHOULD look like...

    bb b b
    b b  bb
     b       b
     bbb b bbb
     b bbbb
     bbbb    b
         b b
     b b b  b
     
         bb
     
    false
    bb b b
    b b  bb
    xb       b
    xbbb b bbb
    xb bbbbxxx
    xbbbbxxxxb
    xxxxxbxbxx
    xbxbxbxxbx
    xxxxxxxxxx
    xxxxxbbxxx

    But here's the type of output that I keep getting...

    b   b b   
    b b  b    
    b     bbbb
       b   bb 
    b b b  bb 
     bb  b b  
       b b   b
      b  bb   
     bb       
         b  bb
     
    false
    b   b b   
    b b  b    
    b     bbbb
       b   bb 
    b b b  bb 
     bb  b b  
       b b   b
      b  bb   
     bb       
    x    b  bb

    Can somebody explain to me what it is I'm doing wrong? Also, why aren't my 'x's being stored along the way? Also, I'm somewhat of a beginner when it comes to recursion, so please let me know if the recursion could be the problem. Thank you.


  2. #2
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: Problem in my 2-Dimensional Array Board Game

    return findPath(currentRow--, currentColumn)
    + findPath(currentRow++, currentColumn)
    + findPath(currentRow, currentColumn--)
    + findPath(currentRow, currentColumn++);

    You know, that's changing the value of currentRow and currentColumn. Did you mean it to do that?



    Also, there may be a way to simplify the code, probably won't fix the problem, but when you're checking to see if it's not a b or an x and if so to put an x, if it's not a b and not an x then wouldn't it be a ' ' then? Why not just check to see if it's a ' ' or not instead of checking to see if it's neither a b nor an x?

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,504
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: Problem in my 2-Dimensional Array Board Game

    I thought that might be an issue but for all other rows it should skip this loop and enter the if statement below.

    The issue is that the first thing that happens is to check the starting position. If it is blank it places an X in that location. Then the if statement (mentioned above) checks if there is an X at the same location and returns 0 which ends the path search and no more X's are inserted.
    Improving the world one idiot at a time!

  4. #4
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: Problem in my 2-Dimensional Array Board Game

    Well, there's also

    while(currentRow ==0)

    and it seems to be only putting an x in row 0.

  5. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,504
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: Problem in my 2-Dimensional Array Board Game

    Yeah that whole loop is a bit whiffy. It will execute once or not at all. Not much of a loop.
    Improving the world one idiot at a time!

Similar Threads

  1. Java (2 dimensional array problem)
    By JoshuAtenista in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 21st, 2013, 05:38 AM
  2. Help with assignment about a board game (enum and array question)
    By Kranti1992 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 12th, 2012, 07:47 AM
  3. Problem with Two-Dimensional Array Program
    By soradogoof in forum Collections and Generics
    Replies: 26
    Last Post: February 20th, 2012, 07:17 PM
  4. help with board game assignment
    By pjay in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 19th, 2011, 12:58 PM
  5. Problem printing a two dimensional boolean array
    By sallyB in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 23rd, 2011, 03:56 PM

Tags for this Thread