Write a TicTacToe.java program that let 1 or 2 players to play tic-tac-toe game.
ID: 3886862 • Letter: W
Question
Write a TicTacToe.java program that let 1 or 2 players to play tic-tac-toe game. In one player mode, user plays against computer. In two players mode, 2 users play against each other. In either mode, players can decide to play 'X' or 'O'. After each player's turn, program must output the current status of the board. Game can stop either a winner is detected or a draw is happened. Either way, program must print a message notifying the result of the game and print who is a a winner if any.
NOTE: 'X' always play first.
NOTE: Detect invalid inputs and notify players.
NOTE: Prompt the players to Game Menu when the current game is finished.
NOTE: Do not use the ZIP format from syllabus and submit ONLY Java and/or Text file (Text file contains output of your program).
Explanation / Answer
Code:-
import java.util.Scanner;
/*
* TIC TAC TOE GAME
*/
class TICTACTOE {
public static final int EMPTY = 0;
public static final int CROSS = 1;
public static final int ROUND = 2;
public static final int RUNNING = 0;
public static final int DRAW = 1;
public static final int CROSS_WON = 2;
public static final int ROUND_WON = 3;
public static final int ROWS = 3, COLS = 3; // ROWS AND COLUMNS
public static int[][] board = new int[ROWS][COLS]; // Board in form of 2D array
public static int currentState; // the current state of the game
public static int currentPlayer; // the current player (CROSS or ROUND)
public static int currntRow, currentCol; // current seed's row and column
public static Scanner in = new Scanner(System.in); // the input Scanner
public static void main(String[] args) {
//INIT FUNCTION
initGame();
// Play the game once
do {
playerMove(currentPlayer); // update currentRow and currentCol
updateGame(currentPlayer, currntRow, currentCol); // update currentState
printBoard();
// Print message if game-over
if (currentState == CROSS_WON) {
System.out.println("WON and the winner is 'X'");
} else if (currentState == ROUND_WON) {
System.out.println("WON and the winner is '0'");
} else if (currentState == DRAW) {
System.out.println("DRAW........Try again later");
}
// Switch player
currentPlayer = (currentPlayer == CROSS) ? ROUND : CROSS;
} while (currentState == RUNNING); //
}
public static void initGame() {
for (int row = 0; row < ROWS; ++row) {
for (int col = 0; col < COLS; ++col) {
board[row][col] = EMPTY; // all cells empty
}
}
currentState = RUNNING; // ready to play
currentPlayer = CROSS; // cross plays first
}
public static void playerMove(int theSeed) {
boolean validInput = false; // for input validation
do {
if (theSeed == CROSS) {
System.out.print("Player 'X',Your move enter accordingly (row[1-3] column[1-3]): ");
} else {
System.out.print("Player 'O', Your move enter accordingly (row[1-3] column[1-3]): ");
}
int row = in.nextInt() - 1; // array index starts at 0 instead of 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; // update game-board content
validInput = true; // input okay, exit loop
} else {
System.out.println("Previous move for(" + (row + 1) + "," + (col + 1)
+ ") is invalid. Try again..");
}
} while (!validInput); //loop continues untill valid input is provided
}
public static void updateGame(int theSeed, int currentRow, int currentCol) {
if (hasWon(theSeed, currentRow, currentCol)) { // check if the move results a winner
currentState = (theSeed == CROSS) ? CROSS_WON : ROUND_WON;
} else if (isDraw()) { // condition for DRAW
currentState = DRAW;
}
// GAME IS STILL GOING TO CONTINUE
}
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; // an empty cell found, game continue
}
}
}
return true; //no empty cell,game finished and its a draw
}
public static boolean hasWon(int theSeed, int currentRow, int currentCol) {
return (board[currentRow][0] == theSeed // 3-in-the-row
&& board[currentRow][1] == theSeed
&& board[currentRow][2] == theSeed
|| board[0][currentCol] == theSeed // 3-in-the-column
&& board[1][currentCol] == theSeed
&& board[2][currentCol] == theSeed
|| currentRow == currentCol // 3-in-the-diagonal
&& board[0][0] == theSeed
&& board[1][1] == theSeed
&& board[2][2] == theSeed
|| currentRow + currentCol == 2 // 3-in-the-opposite-diagonal
&& 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]); // print each of the cells
if (col != COLS - 1) {
System.out.print("|"); // print vertical partition
}
}
System.out.println();
if (row != ROWS - 1) {
System.out.println("-----------"); // print horizontal partition
}
}
System.out.println();
}
public static void printCell(int content) {
switch (content) {
case EMPTY: System.out.print(" "); break;
case ROUND: 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.