Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Language! You will create a two person Tic-Tac-Toe game using a two dimensi

ID: 3866441 • Letter: J

Question

Java Language!

You will create a two person Tic-Tac-Toe game using a two dimensional array to store the data. The program will prompt the X and O players to enter what square they want by typing in the horizontal and vertical position of the square. For example:

What square player X? 0 2

| |
-----
| |
-----
X| |

What square player O? 1 1

| |
-----
|O|
-----
X| |

And so on.

If the player inputs incorrect data then the program should prompt them to re-enter the data.

EXTRA CREDIT (10 points):

Make it so your program knows when a player has won (and ends the game with an announcement).

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;
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote