Use a two-dimensional array to implement a Tic-Tac-Toe game. Use an object-orien
ID: 3848533 • Letter: U
Question
Use a two-dimensional array to implement a Tic-Tac-Toe game. Use an object-oriented approach. Suggestions: Define a TicTacToe class. The data structure will be a 3x3 array (the board) to hold the current state of the game Define functions to Update the board with 'X' or 'O' Determine if there is a win Print the board on the screen Implement the interface with the user and logic of the game in main. The board should be printed on the screen after each update. Generate one file with the TicTacToe class and main at the end. This will be the only file you will turn in.
Explanation / Answer
PROGRAM:- with game ground as 2-D array
import java.util.Scanner;
public class goforit {
public static final int NOTHING = 0;
public static final int CROSS = 1;
public static final int ROUND = 2;
// Name-constants to represent the various states of the game
public static final int GAMEON = 0;
public static final int MAKE = 1;
public static final int CROSS_WON = 2;
public static final int ROUND_WON = 3;
// The game ground and the game status
public static final int ROWS = 3, COLS = 3; // number of rows and columns
public static int[][] ground = new int[ROWS][COLS]; // game ground in 2D array
public static int curState;
public static int curPlayer;
public static int curRow, curCol;
public static Scanner scn = new Scanner(System.in); // the input Scanner
public static void main(String[] args) {
initialize();
do {
playerMove(curPlayer);
updateGame(curPlayer, curRow, curCol); // update curState
printBoard();
if (curState == CROSS_WON) {
System.out.println("'X' won! Bye!");
} else if (curState == ROUND_WON) {
System.out.println("'O' won! Bye!");
} else if (curState == MAKE) {
System.out.println("It's a Draw! Bye!");
}
curPlayer = (curPlayer == CROSS) ? ROUND : CROSS;
} while (curState == GAMEON); // repeat if not game-over
}
public static void initialize() {
for (int row = 0; row < ROWS; ++row) {
for (int col = 0; col < COLS; ++col) {
ground[row][col] = NOTHING; // all cells empty
}
}
curState = GAMEON; // ready to play
curPlayer = CROSS; // cross plays first
}
public static void playerMove(int loc) {
boolean validInput = false;
do {
if (loc == 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 = scn.nextInt() - 1;
int col = scn.nextInt() - 1;
if (row >= 0 && row < ROWS && col >= 0 && col < COLS && ground[row][col] == NOTHING) {
curRow = row;
curCol = col;
ground[curRow][curCol] = loc;
validInput = true;
} else {
System.out.println("Your move at [" + (row + 1) + "," + (col + 1)
+ "] is not valid. Try agascn...");
}
} while (!validInput);
}
public static void updateGame(int loc, int currentRow, int curCol) {
if (hasWon(loc, currentRow, curCol)) { // check if winning move
curState = (loc == CROSS) ? CROSS_WON : ROUND_WON;
} else if (canDraw()) { // check for draw
curState = MAKE;
}
}
public static boolean canDraw() {
for (int row = 0; row < ROWS; ++row) {
for (int col = 0; col < COLS; ++col) {
if (ground[row][col] == NOTHING) {
return false; // an empty cell found, not draw, exit
}
}
}
return true; // no empty cell, it's a draw
}
public static boolean hasWon(int loc, int currentRow, int curCol) {
return (ground[currentRow][0] == loc // 3-in-the-row
&& ground[currentRow][1] == loc
&& ground[currentRow][2] == loc
|| ground[0][curCol] == loc // 3-in-the-column
&& ground[1][curCol] == loc
&& ground[2][curCol] == loc
|| currentRow == curCol // 3-in-the-diagonal
&& ground[0][0] == loc
&& ground[1][1] == loc
&& ground[2][2] == loc
|| currentRow + curCol == 2 // 3-in-the-opposite-diagonal
&& ground[0][2] == loc
&& ground[1][1] == loc
&& ground[2][0] == loc);
}
public static void printBoard() {
for (int row = 0; row < ROWS; ++row) {
for (int col = 0; col < COLS; ++col) {
showCell(ground[row][col]); // print each of the cells
if (col != COLS - 1) {
System.out.print("|");
}
}
System.out.println();
if (row != ROWS - 1) {
System.out.println("-----------"); // print horizontal partition
}
}
System.out.println();
}
public static void showCell(int content) {
switch (content) {
case NOTHING: System.out.print(" "); break;
case ROUND: System.out.print(" O "); break;
case CROSS: System.out.print(" X "); break;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.