Question: Note: Although Tic - Tac - Toe and Mastermind allow only two players, Connect Four can be played with up to four players. For simplicity,

Note: Although Tic-Tac-Toe and Mastermind allow only two players, Connect Four can be
played with up to four players. For simplicity, your code will handle only two players.
4. Select File > New > Enum to create an enumeration named Outcome in the
games.boards package defined as follows:
public enum Outcome {PLAYER1_WIN, PLAYER2_WIN, CONTINUE, TIE}
This enumeration represents the result when the turn is completed.
5. Select File > New > Enum to create an enumeration named Mark in the games.boards
package defined as follows:
public enum Mark {EMPTY, NOUGHT, CROSS, YELLOW, RED,
BLUE, GREEN, MAGENTA, ORANGE}
This enumeration represents the result when the game is completed.
Note: Only yellow and red are used in Connect Four, while Mastermind uses all six colors.
6. Add the Cell class to the games.boards package. It should have the private variables
content, row, and column, and the public methods getContent, setContent, getRow, and
getColumn. Use the following code as a guide:
public class Cell {
private Mark content;
private int row, column;
public Cell(int row, int column){
this.row = row;
this.column = column;
content = Mark.EMPTY;
}
public Mark getContent(){ return content; }
public void setContent(Mark content){
this.content = content;
}
public int getRow(){ return row; }
public int getColumn(){ return column; }
}
Take note that all classes that support direct instantiation should have a constructor. In this
case, the constructor will be used by the Board class to create each of its cells.
7. Add the Board class to the games.boards package. It should have a two-dimensional array
of Cell objects. The Board class should initialize a board with a specified number of columns
and rows, provide access to Cell objects, and display all of its cells correctly. Use the
following code as a guide:
public class Board {
private Cell[][] cells;
public Board(int rows, int columns){
cells = new Cell[rows][columns];
for( int r =0; r < cells.length; r++){
for (int c =0; c < cells[r].length; c++){
cells[r][c]= new Cell(r,c);
}
}
}
public void setCell(Mark mark, int row, int column)
throws IllegalArgumentException {
if (cells[row][column].getContent()== Mark.EMPTY)
cells[row][column].setContent(mark);
else throw new IllegalArgumentException
("Player already there!" );
}
public Cell getCell(int row, int column){
return cells[row][column];
}
public String toString(){
StringBuilder str = new StringBuilder();
for( int r =0; r < cells.length; r++){
str.append("|");
for (int c =0; c < cells[r].length; c++){
switch(cells[r][c].getContent())
{
case NOUGHT:
str.append(O);
break;
case CROSS:
str.append(X);
break;
case YELLOW:
str.append(Y);
break;
case RED:
str.append(R);
break;
case BLUE:
str.append(B);
break;
case GREEN:
str.append(G);
break;
case MAGENTA:
str.append(M);
break;
case ORANGE:
str.append(M);
break;
default: //Empty
str.append();
}
str.append(|);
}
str.append(
);
}
return str.toString();
}
}
This code should seem familiar to you. The methods in theTicTacToeGame class are similar
to those in the Board class. The StringBuilder class was used instead of theString class for
better performance.
8. Add the following import statement to theBoardGameTester class:
import games.boards.*;
9. In the main() method of BoardGameTester, perform the following actions:
a. Create a 3\times 3 board for a Tic-Tac-Toe game.
b. Create a 6\times 7 board for a Connect Four game.
c. Create a 5\times 8 board for a game of Mastermind.
d. Set a cell to a nought or cross on the Tic-Tac-Toe board.
e. Set a cell to yellow or red on the Connect Four board.
f. Set a cell to yellow, red, green, blue, magenta, or orange on the Mastermind board.
g. Display the boards for Tic-Tac-Toe, Connect Four, and Mastermind.
10. Run the project to ensure it works as you expect it to.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!