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 'T' if there is a tie. For now it just returns a space no matter what.
return ' ';
}
}
this is the second file
import java.util.Random;
public class TicTacToeplayer {
public void getMove(char[][] theBoard, char myPiece) {
// this method should update the game board to place 'myPiece' (X or O)
// in a space on the board.
// As-is, this method just places an X or O randomly, without even considering
// if the square is already taken.
Random myRand = new Random();
int myRow = myRand.nextInt(3);
int myCol = myRand.nextInt(3);
theBoard[myRow][myCol] = myPiece;
}
}
i have created a project but they wont run together.