public class TicTacToeGame {
public static final int board_size = 3; // number of rows or columns in the board
public static void main(String[] args) {
char board[][] = new char[board_size][board_size]; // the game board
TicTacToeGame p1 = new TicTacToeGame(); // create the players
TicTacToeGame p2 = new TicTacToeGame();
initBoard (board); // initialize the board to spaces and print it out
displayBoard (board);
char winner = findWinner(board);
while (winner == ' ') {
p1.getMove(board, 'X'); // player one will be 'X', and will always go first
displayBoard (board);
winner = findWinner(board);
if (winner != ' ') {
if (winner == 'T') { System.out.println("Tie Game!"); }
else { System.out.println("The Winner is: " + winner); }
break;
}
p2.getMove(board, 'O'); // player two will be 'O', and always go second
displayBoard (board);
winner = findWinner(board);
if (winner != ' ') {
if (winner == 'T') { System.out.println("Tie Game!"); }
else { System.out.println("The Winner is: " + winner); }
}
}
}
public static void initBoard(char[][] theBoard) {
// since this is pass by reference, initializing the Board here resets it in main
for (int row = 0; row < board_size; row++) {
for (int col = 0; col < board_size; col++) {
theBoard[row][col] = ' '; // row always comes first in a 2d array
}
}
}
public static void displayBoard(final char[][] theBoard) {
// note the final parameter - we won't change the board, just print it.
// Need to format the board display to look like this:
// | |
// -----
// | |
// -----
// | |
for (int row = 0; row < board_size; row++) {
for (int col = 0; col < board_size; col++) {
System.out.print(theBoard[row][col]); // row always comes first in a 2d array
}
System.out.println();
}
}
public static char findWinner(final char[][] theBoard){
// this method should return 'X' if X is the winner
// 'O' if O is the winner, a space (' ') if there is no winner,
// or
need help please thanks