Reputation: 63
I'm having a hard time figuring out how to print out who the winner is. Should I just create a separate method to decide whether someone won or a draw occurred? Here's the code I have so far. Any help is greatly appreciated.
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[][] board = {
{" ", " ", " "},
{" ", " ", " "},
{" ", " ", " "}
};
boolean done = false;
int player = 1;
int row = 0;
int col = 0;
while (done != true) {
System.out.println("Enter the row and column for your next move");
row = in.nextInt();
col = in.nextInt();
if (player == 1) {
board[row][col] = "X";
player = 2;
} else {
board[row][col] = "O";
player = 1;
}
printBoard(board);
}
}
public static void printBoard(String[][] boardValues) {
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
System.out.print("|" + boardValues[row][col] + "|");
}
System.out.println();
System.out.println("-------");
}
}
Upvotes: 0
Views: 202
Reputation: 37
As mentioned in the comments you are missing all the logic to determine if someone has won. Personally I think of the main in this case as:
print the blank board
while (done is false)
player makes move
check for win if true set done to true
print board
Then basically you need a function to make moves, someway in there to determine if it is X or O. Then a method to check for a win, this need only check based on last move since it was previously checked, or it probably easier to check all options. Finally print the updated board and upon exit of the loop you could print a win message.
I find this breaks the program into a nice simple structure. This link has some code you could probably learn from.
http://www3.ntu.edu.sg/home/ehchua/programming/java/JavaGame_TicTacToe.html
Good luck
Upvotes: 2