'Why isnt my nested loop not looping Diagonal for checking if Tic-tac-toe is win(with java)?
im a student who really needs help(my teacher doesn't even answer when i email her, i am a online student) So, i have a tic-tac-toe program(the program is to long to put all of it here, 10 classes at least 5 of them have 300 lines of code) So in this program there is a method isWin(), it will check the tic-tac-toe if there is a win so it will check Horizontally, Vertically, and diagonally from both sides. So the problem is with diagonally checking if there is a win. Im going to post the full isWin() function and what i tried.
boolean isWin(char mark) {
// If the game has already been recorded as won, return true.
if (gameHasBeenWon) return true;
/**
* Using the incoming mark, implement nested loops to loop through the
* columns and diagonals, checking each character in every column and
* diagonal o see if there are three marks in a line. If any
* winning line is found, complete the following directives:
* 1. Set gameHasBeenWon to true.
* 2. Return the value of gameHasBeenWon.
*
* The check for horizontal wins is provided for you.
*/
// Check for horizontal wins.
for (int row = 0; row < 3; row++) {
boolean win = true;
for (int col = 0; col < 3; col++) {
if (grid[row][col] != mark) win = false;
}
if (win) {
gameHasBeenWon = true;
return gameHasBeenWon;
}
}
for (int col = 0; col < 3; col++) {
boolean win = true;
for (int row = 0; row < 3; row++) {
if (grid[row][col] != mark) win = false;
}
if (win) {
gameHasBeenWon = true;
return gameHasBeenWon;
}
}
for (int col = 0; col < 3; col++) {
boolean win = true;
for (int row = 0; row < 3; row++) {
if (grid[row][col] != mark) win = false;
if(row ==
}
}
What i tried to do is
for (int col = 0; col < 1; col++) {
boolean win = true;
for (int row = 0; row < 3; row++) {
if(row == 2){
if (grid[row][col+2] != mark) win = false;
}else if(row==0){
if (grid[row][col] != mark) win = false;
}else{
if (grid[row][col+1] != mark) win = false;
}
if (win) {
gameHasBeenWon = true;
return gameHasBeenWon;
}
}
}
but when i tried that, the isTied() method doesn't work, i will provide that under.
boolean isTie() {
// If game has already been recorded as won, then it cannot be a tie.
if (gameHasBeenWon) return false;
// If there are any spaces on the board, return false, because it is
// not a tie.
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
if (grid[row][col] == SPACE) return false;
}
}
// If the game was not won, but all the spaces are full, it is a tie.
return true;
}
Edit: this is the Tester class
/**
* The following is a short program that tests the coding of class
* TicTacToeBoard from Lab Exercise 2.
*/
public class TicTacToeBoardTest2 {
/**
* Use the special main method for testing the TicTacToeBoard class only.
*/
public static void main(String[] args) {
System.out.println("\nTicTacToeBoard test 2 results:");
TicTacToeBoard testBoard = new TicTacToeBoard();
try {
testBoard.move(1, 1, 'X');
testBoard.move(2, 2, 'O');
testBoard.move(2, 0, 'X');
System.out.println();
testBoard.display();
printMessage("Not a win", !testBoard.isWin('X'));
} catch (IllegalMoveException ime) {
System.out.println("Error: illegal move not expected: " + ime);
}
testBoard = new TicTacToeBoard();
try {
testBoard.move(2, 0, 'X');
testBoard.move(2, 1, 'X');
testBoard.move(2, 2, 'X');
System.out.println();
testBoard.display();
printMessage("Horizontal win", testBoard.isWin('X'));
} catch (IllegalMoveException ime) {
System.out.println("Error: illegal move not expected: " + ime);
}
testBoard = new TicTacToeBoard();
try {
testBoard.move(0, 1, 'O');
testBoard.move(1, 1, 'O');
testBoard.move(2, 1, 'O');
System.out.println();
testBoard.display();
printMessage("Vertical win", testBoard.isWin('O'));
} catch (IllegalMoveException ime) {
System.out.println("Error: illegal move not expected: " + ime);
}
testBoard = new TicTacToeBoard();
try {
testBoard.move(0, 0, 'X');
testBoard.move(1, 1, 'X');
testBoard.move(2, 2, 'X');
System.out.println();
testBoard.display();
printMessage("Diagonal win 1", testBoard.isWin('X'));
} catch (IllegalMoveException ime) {
System.out.println("Error: illegal move not expected: " + ime);
}
testBoard = new TicTacToeBoard();
try {
testBoard.move(0, 2, 'X');
testBoard.move(1, 1, 'X');
testBoard.move(2, 0, 'X');
System.out.println();
testBoard.display();
printMessage("Diagonal win 2", testBoard.isWin('X'));
} catch (IllegalMoveException ime) {
System.out.println("Error: illegal move not expected: " + ime);
}
testBoard = new TicTacToeBoard();
try {
char[] marks = {'O', 'O', 'X', 'X', 'X', 'O', 'O', 'X', 'X'};
int index = 0;
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
testBoard.move(row, col, marks[index]);
index++;
}
}
System.out.println();
testBoard.display();
printMessage("Tie game not a win", !testBoard.isWin('X'));
printMessage("Tie game", testBoard.isTie());
} catch (IllegalMoveException ime) {
System.out.println("Error: illegal move not expected: " + ime);
}
}
private static void printMessage(String testCase, boolean passed) {
System.out.println(testCase + "? " + passed);
if (passed) {
System.out.println("Good: " + testCase + " handled.");
} else {
System.out.println("Error: " + testCase + " not handled.");
}
}
}
Solution 1:[1]
When you move diagonally you are changing the coordinates for row and for col at the same time. So you can't build loops where one of these coordinates are not changing. Also you have to check both directions, going "down" from [0,0] to [2,2] (assuming [0,0] is the top left corner) and going "up" from [2,0] to [0,2]. For the part going "down" you can write a for loop like this:
win = true;
for (int i=0; i<3; i++) {
if (grid[i][i] != mark) { // this will check [0,0], [1,1], [2,2]
win = false;
break;
}
}
For going "up" you have to decrease the row index like this:
win = true;
for (int i=0; i<3; i++) {
if (grid[2-i][i] != mark) { // this will check [2,0], [1,1], [0,2]
win = false;
break;
}
}
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 | Progman |
