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

Write a Java program to play a tictactoe game. Your program needs to have method

ID: 3696179 • Letter: W

Question

Write a Java program to play a tictactoe
game. Your program needs to have methods
and use an array for the board. This program will be turned into a JavaFX project at the end
of the quarter.
WHAT YOU'RE PROGRAM SHOULD DO:
You'll code the playing of one whole round (several turns until someone wins or ties) of a
tictactoe
game. If you don't know the rules of tictactoe,
go look it up on Google. The user
will be playing against a computer. The computer can either play any valid square randomly
or if you want to, you can add some strategy to the computer's move (or maybe a
combination of random play and strategy because using strategy all the time causes a good
many ties to happen). You'll start by asking the user for a name, whether the user wants to
go first and what token the user wants to play ('X' or 'O').
The game should draw a board to the terminal window. Either it plays the computer's move
or it asks the user for a row and column to play. After each move, the game should determine
whether there is a winner or a draw (ie. all squares are filled). To do this, you need to check
all rows, all columns and the two diagonals for the same three nonblank
values. You also
need to determine a tie by checking to see if there are no blank squares left. When there is a
win or a tie, the program should announce the winner (or a tie) and exit the game.
Remember that you need check whether the user or computer has a 'valid' move. In other
words, if the row and column is already taken you need to reprompt
the user for a new row
and column.
For determining a winner, you will need to check all rows, all columns and the two
diagonals. The checking of rows and columns can be done in a for loop; the two diagonals
will probably need to be done separately. Remember to check both tokens, not just one set
of tokens.
BOARD
You need a board array variable to keep track of what token is on what square on the tictactoe
board. You can use a char or string array (either will work) and use a blank character
to indicate the square has not been taken. You can also either use a twodimensional
array or
a onedimensional
array. The onedimensional
array requires you to translate a row and
column into an index. You can do this by using the formula: index = (row1)*
3 + (column1)
where rows and columns are numbered from 1 to 3. Keep in mind that even for the twodimensional
array, you should subtract one from the row and column before using it as an
index.
REQUIREMENTS:
You are to use methods in your program. No method, including the main method,
should be more than 30 code lines long (this doesn't include blank lines, lines with only
one brace ('{' or '}') on it and any comment lines). So if your method is getting long, try finding
ways to break it up into two or more methods.
You are to use an array for the board. Use either a one dimensional or two dimensional
array. See BOARD for details.
You need to check for user inputs that are valid. In other words, the row and column
numbers should be in the right range (1 to 3) and the square cannot be already taken (check
to see if it is blank). Also, check for 'y' or 'n' for answering questions about whether to take
the first turn or what symbol to use (an alternative to asking whether they want to use 'X'
might be to let the user type in any letter and use that to mark squares with).
If you are giving the computer a strategy (this is optional, you can just pick squares at
random), you can allow the user to choose whether they want to play a random computer or a
computer using some sort of strategy. This helps in testing the program as it's almost
impossible to get a winner for the user when the computer plays to win (either that or I'm a
terrible tictactoe
player :)
).
HINTS FOR PRINTING THE BOARD
To print out the board nicely, use the vertical bar '|' and dashes ''
to make the squares of
the board. I used an outer for loop with two inner loops, top loop to print the top of the square
and the bottom loop to print the side of the squares plus the character for that square.
Remember to add another for loop below the outer loop to print the bottom of the board.

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!");
} 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);
}

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

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