Question
Description For this exercise, you will be create a Tic-Tac-Toe game in Java. Your program should apply inheritance. To do this, I have provided some
Description For this exercise, you will be create a Tic-Tac-Toe game in Java. Your program should apply inheritance. To do this, I have provided some classes and javadoc files that describe the functions of these classes and their public interfaces. In addition, the Javadoc files also contain description of some classes that you need to implement. Study the Javadoc carefully to complete the other classes. Board.java -- The Board class models the Tic-Tac-Toe game board (a 3x3 grid). Cell.java Cell class abstracts the individual cells in a Board. Symbol.java and GameState.java These two classes contain some constants that you should use for the symbols chosen by the players and managing the game state respectively. Do not modify any of these classes. Your code should work with these classes. Now, you need to implement the following classes to complete the game.
Board.java
/** * The Board class models the Tic-Tac-Toe game-board, which is a 3x3 grid. */ public class Board { // save as Board.java // Named-constants for the dimensions private static final int ROWS = 3; private static final int COLS = 3; private Cell[][] cells; // a board composes of ROWS-by-COLS Cell instances /** * Constructor to initialize the game board. */ public Board() { cells = new Cell[ROWS][COLS]; // allocate the array for (int row = 0; row < ROWS; ++row) { for (int col = 0; col < COLS; ++col) { cells[row][col] = new Cell(row, col); // allocate element of the array } } } /** Clears the board. Same as init() method. */ public void clear(){ init(); } /** * The number of rows in the board. * * @return number of rows in the board. */ public int getRows() { return ROWS; } /** * The number of columns in the board. * @return number of columns in the board. */ public int getCols() { return COLS; } /** * Determines if the specified cell position is valid or not. A cell position * is invalid, if it is out of the boundary of the board and the cell is NOT empty. * @param row int value for the row of the board. * @param col int value for the col of the board. * @return true if the supplied position is within valid range and the cell * is empty. */ public boolean isValidCell(int row, int col){ return row >= 0 && row < ROWS && col >= 0 && col < COLS && cells[row][col].getContent() == Symbols.EMPTY; }
/** * Inserts/sets a symbol in the specified location of the board. * @param theSymbol symbol to be set. * @param row valid row location in the board. * @param col valid col location in the board. * @return true, if the insertion is successful, false otherwise. */ public boolean setSymbolAt(char theSymbol, int row, int col){ if(isValidCell(row, col)){ cells[row][col].setContent(theSymbol); return true; } // More approprite would be to throw an exception wehen unable to set // a symbol at a specified location. // But at this time it's OK. return false; } public char getSymbolAt(int r, int c){ return cells[r][c].getContent(); } /** * Initialize (or re-initialize) the contents of the game board. */ public void init() { for (int row = 0; row < ROWS; ++row) { for (int col = 0; col < COLS; ++col) { cells[row][col].clear(); // clear the cell content } } } /** * This method can be used for checking if the game is draw or not! * @return true if there is not more empty cells on the board. */ public boolean hasEmpyCells(){ for (int row = 0; row < ROWS; ++row) { for (int col = 0; col < COLS; ++col) { if (cells[row][col].getContent() == Symbols.EMPTY) { return false; // an empty seed found, not a draw, exit } } } return true; // no empty cell, it's a draw }
/** * Paint the Tic-Tac-Toe board. Currently, in the console. */ public void paintTicTacToeBoard() { for (int row = 0; row < ROWS; ++row) { for (int col = 0; col < COLS; ++col) { cells[row][col].paint(); // each cell paints itself if (col < COLS - 1) System.out.print("|"); } System.out.println(); if (row < ROWS - 1) { System.out.println("-----------"); } } } }
Cell.java
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */
/** * The Cell class models each individual cell of the game board. */ public class Cell { // save as Cell.java
// Private private char content; // content of this cell: Could be any of the three // EMPTY, CROSS, or NOUGHT. private int row, col; // row and column of this cell, not used in this program /** * Constructor to initialize this cell * @param row row position for this cell. * @param col column position for this cell. */ public Cell(int row, int col) { this.row = row; this.col = col; clear(); // clear content } /** * Clear the cell content to EMPTY */ public void clear() { content = Symbols.EMPTY; } /** * Mutator for Content. * @param v Set the cell content to an appropriate value supplied in v. */ public void setContent(char v){ /*switch (v) { case Symbols.CROSS: content = Symbols.CROSS; break; case Symbols.NOUGHT: content = Symbols.NOUGHT; break; case Symbols.EMPTY: content = Symbols.EMPTY; break; default : content = Symbols.EMPTY; // at this moment EMPTY; but, more // appropriate through an exception or enum. }*/ content = v; }
/** Accessor method for content * @return the content of the cell */ public char getContent(){ return content; }
/** * Paint the cell */ public void paint() { /* switch (content) { case Symbols.CROSS: System.out.print(" X "); break; case Symbols.NOUGHT: System.out.print(" O "); break; case Symbols.EMPTY: System.out.print(" "); break; default: ; // should throw an exception, because this should not // have happend. Cell content should not have any values // other than those three; (integrity of the object is // compromised). }*/ if(content == ' ') System.out.print(" "); else System.out.print(" "+content+" "); } }
/** * Enumerations for the various states of the game */
GameState.java
/** * Enumerations for the various states of the game */
public class GameState { // to save as "GameState.java" 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; }
/** * Enumerations for the various states of the game */
/* public enum GameState { // to save as "GameState.java" PLAYING, DRAW, CROSS_WON, NOUGHT_WON; } */
Symbols.java
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */
/** * * @author mrahman */ public class Symbols { // Define different values for different symbols. public static final char EMPTY = ' '; public static final char CROSS = 'X'; public static final char NOUGHT = 'O'; }
/** * Enumerations for the symbols and cell contents */
/* public enum Symbols { // to save as "Symbols.java" EMPTY, CROSS, NOUGHT } */
The Player Class This is a generic Tic-Tac-Toe player class that encapsulates common behavior of a Tic-Tac-Toe player. It should be an abstract class because different players may adopt different strategies for selecting a valid position on the board. For example, a human player may take input from the user for selecting a position, whereas a computer may use artificial intelligence (AI) to select an appropriate position on the board. However, every Tic-Tac-Toe player has common behavior that is they make a move. A move composed of the following operations: 1) Select a valid position from the board. 2) Place the players own symbol on that position. 3) Print a message that the move is complete. The Derived Classes: HumanPlayer and ComputerPlayer The human player and computer player are two derived classes from the Player class that implements an abstract method from the player class. The human player should ask the user to enter a valid position on the board and then it should place a symbol (pre-selected for the player) in that position. However, if the user enters an invalid location (not empty or out of range), then it should show a message and continue until the user enters a valid location. The computer player, on the other hand, should randomly generate a valid location on the board and place its symbol on that location. Note: for the Tic-Tac-Toe game, it is possible to come up with an intelligent strategy that the computer will never lose! If you can find and implement that strategy for your code, you will get 5 bonus marks. Please examine the Javadoc files to find out more on the structure of the APIs for these classes. You must provide all the APIs for these classes as described in the Javadoc files. The TicTacToeGame Class This class act as a game controller, and provides mechanisms for initializing, staring, and managing the state of the game. It also determines if the game is a draw or who is the winner of the game and prints the appropriate message. Please refer to the Javadoc files to find out more on the structure of the APIs for this class. You must provide all the APIs for this class as described in the Javadoc files. Testing A sample output (see below) is provided for you to test your class implementations. You must add additional tests to more thoroughly test your code. Sample Test Run Output from TicTacToeGame.java file: Welcome to the Tic-Tac-Toe Game! There are two payers: X (human) and O (computer). The system will randomly decide which player will go first. Player 'O' has made a move. | | ----------- | | ----------- O | | Player X, enter your move (row[1-3] column[1-3]): 1 1 Player 'X' has made a move. X | | ----------- | | ----------- O | | Player 'O' has made a move. X | | ----------- O | | ----------- O | | Player X, enter your move (row[1-3] column[1-3]): 2 2 Player 'X' has made a move. X | | ----------- O | X | ----------- O | | Player 'O' has made a move. X | | O ----------- O | X | ----------- O | | Player X, enter your move (row[1-3] column[1-3]): 1 5 Invalid choice. Try again. Player X, enter your move (row[1-3] column[1-3]): 0 3 Invalid choice. Try again. Player X, enter your move (row[1-3] column[1-3]): 3 3 Player 'X' has made a move. X | | O ----------- O | X | ----------- O | | X X won! Bye! Note that for this exercise, you need to submit 8 source files, Board.java, Cell.java, ComputerPlayer.java, GameState.java, HumanPlayer.java, Player.java, Symbol.java, and TicTacToeGame.java.
(I need code for this question,please help)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started