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

# Thread: Problem in my 2-Dimensional Array Board Game

1. ## 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;
Scanner input = new Scanner(System.in);

// Make a constructor with no arguments that sets the board
public Board() {
Row = 10;
Column = 10;
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
if (checkStartPoint(currentRow, currentColumn) == false)
return 0;
else
}

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

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