4 x 4 Tic Tac Toe Java Program Write a Java windows program ( not an applet ) th
ID: 655252 • Letter: 4
Question
4 x 4 Tic Tac Toe Java Program
Write a Java windows program (not an applet) that allows two players to play 4x4 Tic-Tac-Toe.
The program starts by displaying a blank board and randomly decides whether X or O goes first. A color message is displayed at the top indicating who gets the next move. When a square on the board is clicked, either an X or an O appears depending on whose turn it is. The turns alternate so that a symbol never appears twice in a row.
Once you complete this assignment, extend it so that the program determines when either X or O has won by having four in a row, horizontally, vertically, or diagonally. It displays a message when either X or O wins, or when the board is full with no winner. This can be done adding a method to the board class that checks to see if either X or O has won
Requirements
1. The Java program must use the Swing graphics methods and overwrite the JFrame paint method.
2. It must keep track of whose turn it is and use different colors for X and O.
3. It must draw Xs and Os (not some other symbol) in the squares.
4. It must implement the MouseListener interface.
5. To keep track of what positions in the board are occupied. Create a Board class whose fields include:
- A 4x4 array representing the board. Considering using an Enum class to represent X, O, or none.
- A Boolean indicating whose turn is next.
- Appropriate methods to manipulate the board.
6. Use the Random class to decide whether X or O goes first.
Suggestions
Here is a suggested order:
a. Draw an empty board
b. Add a red X in the upper left corner.
c. Add a blue O in the lower right corner.
d. Implement the Board class described above.
e. Implement the mouse listener so that an X (or O depending on turn) is set in the board array according to which square is clicked in
f. Make the paint method draw Xs and Os in the window according to what elements are set in the Board array.
g. Draw a string to indicate whose turn it is.
Explanation / Answer
import java.util.Scanner;
public class TTTConsoleNonOO2P {
public static final int EMPTY = 0;
public static final int CROSS = 1;
public static final int NOUGHT = 2;
public static final int PLAYING = 0;
public static final int DRAW = 1;
public static final int CROSS_WON = 2;
public static final int NOUGHT_WON = 3;
public static final int ROWS = 3, COLS = 3;
public static int[][] board = new int[ROWS][COLS];
public static int currentState;
public static int currentPlayer;
public static int currntRow, currentCol;
public static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
initGame();
do {
playerMove(currentPlayer);
updateGame(currentPlayer, currntRow, currentCol);
printBoard();
if (currentState == CROSS_WON) {
System.out.println("'X' won! Bye!");
} else if (currentState == NOUGHT_WON) {
System.out.println("'O' won! Bye!");
} else if (currentState == DRAW) {
System.out.println("It's a Draw! Bye!");
}
currentPlayer = (currentPlayer == CROSS) ? NOUGHT : CROSS;
} while (currentState == PLAYING);
}
public static void initGame() {
for (int row = 0; row < ROWS; ++row) {
for (int col = 0; col < COLS; ++col) {
board[row][col] = EMPTY;
}
}
currentState = PLAYING;
currentPlayer = CROSS;
}
public static void playerMove(int theSeed) {
boolean validInput = false; do {
if (theSeed == CROSS) {
System.out.print("Player 'X', enter your move (row[1-3] column[1-3]): ");
} else {
System.out.print("Player 'O', enter your move (row[1-3] column[1-3]): ");
}
int row = in.nextInt() - 1;
int col = in.nextInt() - 1;
if (row >= 0 && row < ROWS && col >= 0 && col < COLS && board[row][col] == EMPTY) {
currntRow = row;
currentCol = col;
board[currntRow][currentCol] = theSeed;
validInput = true;
} else {
System.out.println("This move at (" + (row + 1) + "," + (col + 1)
+ ") is not valid. Try again...");
}
} while (!validInput); // repeat until input is valid
}
public static void updateGame(int theSeed, int currentRow, int currentCol) {
if (hasWon(theSeed, currentRow, currentCol))
{
currentState = (theSeed == CROSS) ? CROSS_WON : NOUGHT_WON;
} else if (isDraw()) { currentState = DRAW;
}
}
public static boolean isDraw() {
for (int row = 0; row < ROWS; ++row) {
for (int col = 0; col < COLS; ++col) {
if (board[row][col] == EMPTY) {
return false;
}
}
}
return true;
}
public static boolean hasWon(int theSeed, int currentRow, int currentCol) {
return (board[currentRow][0] == theSeed
&& board[currentRow][1] == theSeed
&& board[currentRow][2] == theSeed
|| board[0][currentCol] == theSeed
&& board[1][currentCol] == theSeed
&& board[2][currentCol] == theSeed
|| currentRow == currentCol
&& board[0][0] == theSeed
&& board[1][1] == theSeed
&& board[2][2] == theSeed
|| currentRow + currentCol == 2
&& board[0][2] == theSeed
&& board[1][1] == theSeed
&& board[2][0] == theSeed);
}
public static void printBoard() {
for (int row = 0; row < ROWS; ++row) {
for (int col = 0; col < COLS; ++col) {
printCell(board[row][col]);
if (col != COLS - 1) {
System.out.print("|");
}
}
System.out.println();
if (row != ROWS - 1) {
System.out.println("-----------");
}
}
System.out.println();
}
public static void printCell(int content) {
switch (content) {
case EMPTY: System.out.print(" "); break;
case NOUGHT: System.out.print(" O "); break;
case CROSS: System.out.print(" X "); break;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.