Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The attached code includes the ITicTacToe interface. You are NOT allowed to modify the interface. Instructions: Finish the implementation of the class TicTacToe. Use the

The attached code includes the ITicTacToe interface. You are NOT allowed to modify the interface. Instructions: Finish the implementation of the class TicTacToe. Use the two-dimensional board array to represent the game board. Focus on the AI or computer move in this version. Use the TicTacToeConsoleVersion class to run the game.

Please help with the TicTacToe class

TicTacToe Console

import java.util.Scanner; /** * Tic-Tac-Toe: Two-player console, non-graphic */ public class TTTConsole { public static Scanner in = new Scanner(System.in); // the input Scanner public static TicTacToe TTTboard = new TicTacToe(); /** The entry main method (the program starts here) */ public static void main(String[] args) { int currentState = TicTacToe.PLAYING; String userInput; //game loop do { TTTboard.printBoard(); // Print message if game-over currentState = TTTboard.checkForWinner(); /** * get player input here and call setMove(). user should input a number between 0-8 */ if (currentState == ITicTacToe.CROSS_WON) { System.out.println("'X' won! Bye!"); } else if (currentState == ITicTacToe.NOUGHT_WON) { System.out.println("'O' won! Bye!"); } else if (currentState == ITicTacToe.TIE) { System.out.println("It's a TIE! Bye!"); } //user can break the loop. remove this line when you finish implementation. userInput = in.next(); } while ((currentState == ITicTacToe.PLAYING) && (!userInput.equals("q"))); // repeat if not game-over } }

TicTacToe interface(Don't edit)

/** * ITicTacToe interface. */ public interface ITicTacToe { // Name-constants to represent the seeds and cell contents public static final int EMPTY = 0; public static final int CROSS = 1; public static final int NOUGHT = 2;

// Name-constants to represent the various states of the game public static final int PLAYING = 0; public static final int TIE = 1; public static final int CROSS_WON = 2; public static final int NOUGHT_WON = 3;

/** * clear the board of all X's and Y's by setting all spots to EMPTY */ public void clearBoard(); /** Sets the given player at the given location on the game board. * The location must be available, or the board will not be changed * @param player - HUMAN_PLAYER or COMPUTER_PLAYER * @param location - The location (0-8) to place the move */ public void setMove(int player, int location); /** Returns the best move for the computer to make. You must call setMove() * to actually make the computer move to the location * @return the best move for the computer to make (0-8) */ public int getComputerMove(); /** * Check for a winner and return a status value indicating who has won. * @return PLAYING if still playing, TIE if its a tie, CROSS_WON if X won, or NOUGH_WON if 0 won */ public int checkForWinner(); }

TicTacToe class (Needs edit)

/** * TicTacToe class implements the interface */ public class TicTacToe implements ITicTacToe { // The game board and the game status private static final int ROWS = 3, COLS = 3; // number of rows and columns private int[][] board = new int[ROWS][COLS]; // game board in 2D array /** * clear board and set current player */ public TicTacToe(){ } @Override public void clearBoard() { // TODO Auto-generated method stub

}

@Override public void setMove(int player, int location) { // TODO Auto-generated method stub

}

@Override public int getComputerMove() { // TODO Auto-generated method stub return 0; }

@Override public int checkForWinner() { // TODO Auto-generated method stub return 0; } /** * Print the game board */ public 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(); } /** * Print a cell with the specified "content" * @param content either CROSS, NOUGHT or EMPTY */ public 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; } }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions