'the code works all well until the last else if statement (with the != 'R' etc.) when i run the code to test that statement, the code fails to function [closed]
making a connect4 game, so this is just some part of the logic - no floating pieces, no invalid letters (pieces - only R and Y acceptable) '.' represents an empty gap.
boolean arrayCheck = false;
do {
for (int j = 0; j < connect4Board[0].length; j++) {
for (int i = 0; i < connect4Board.length - 1; i++) {
if ((connect4Board[i][j] == 'R') || (connect4Board[i][j] == 'Y')) {
if ((connect4Board[i + 1][j] == '.') || (connect4Board[i + 1][j] == 0)) {
return arrayCheck; //makes sure a gap underneath is not present and seen as a floating piece
}
else if ((connect4Board[i + 1][j] == 'R') || (connect4Board[i + 1][j] == 'Y')) {
i++;
}
}
else if ((connect4Board[i][j] == '.') || (connect4Board[i][j] == 0)) {
if ((connect4Board[i + 1][j] == 'R') || (connect4Board[i + 1][j] == 'Y') || connect4Board[i + 1][j] == '.' || (connect4Board[i + 1][j] == 0)) {
i++;
} else {
return arrayCheck;
}
}
else if((connect4Board[i][j] != 'R') && (connect4Board[i][j] != 'Y') && (connect4Board[i][j] != '.')) {
return arrayCheck; //where it all goes wrong. code bugs out and doesn't output anything (should output true or false) but the console just stays empty.
}
i--;
}
j++;
}
} while (arrayCheck);
I don't know whether the problem is because of the do-while loop, or the if statement itself. I've tried to put the if statement at the beginning of the loop, outside the loop, and at the end but the problem seems to be the same regardless of where I put it. the console just remains blank (stays running without continuing with any other actions). What should I do?
Solution 1:[1]
1. Check if valid space
The first if checks if it is a valid space, if not it will return false and as a result ending the loop.
2. Check for floating piece
You can then check if they are valid
at the end you have i--;
Do you need to do this?
i++
Or
i--
Or nether of them? Because you already increment for every nested if.
Debuging
To help debug your code try printing i with System.out.println(i);Notice what happens to it.
Is the loop creating an infinite loop?
Example
I created the array as:
char[][] connect4Board = {{'R','.','.','.','.'},{'T','.','.','.','.'},{'R','.','.','.','.'},{'R','.','.','.','.'}};
Could it possibly be how the counter i is incremented? This works for me..
boolean arrayCheck = false;
do {
for (int j = 0; j < connect4Board.length; j++) {
for (int i = 0; i < connect4Board.length - 1; i++) {
/*1. Check if valid space*/
if ((connect4Board[i][j] != 'R') && (connect4Board[i][j] != 'Y') && (connect4Board[i][j] != '.'))
return arrayCheck;
/*2. Check for floating piece*/
if ((connect4Board[i][j] == 'R') || (connect4Board[i][j] == 'Y'))
if ((connect4Board[i + 1][j] == '.') || (connect4Board[i + 1][j] == 0))
return arrayCheck;
else if ((connect4Board[i + 1][j] == 'R') || (connect4Board[i + 1][j] == 'Y'))
i++;
if ((connect4Board[i][j] == '.') || (connect4Board[i][j] == 0))
if ((connect4Board[i + 1][j] == 'R') || (connect4Board[i + 1][j] == 'Y') || connect4Board[i + 1][j] == '.' || (connect4Board[i + 1][j] == 0))
i++;
else
return arrayCheck;
//Do you need to increment?
// i++; Or i--;
}
j++;
}
} while (arrayCheck);
return true;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
