Question

In: Computer Science

Write a class (and a client class to test it) that encapsulates a tic-tac-toe board. A...

Write a class (and a client class to test it) that encapsulates a tic-tac-toe board. A tic-tac-toe board looks like a table of three rows and three columns partially or completely filled with the characters X and O. At any point, a cell of that table could be empty or could contain an X or an O. You should have one instance variable, a two-dimensional array of values representing the tic-tac-toe board.

This game should involve one human player vs. the computer (aka, it should be a one player game). At the start of each game, randomly select if the computer will play X or O and who (i.e. human or computer) will make the first move.

Your default constructor should instantiate the array so that it represents an empty board.

You should include the following methods:

a method that generates a valid play by the computer and displays the board after each play.

a method that requests a valid play from the human and displays the board after each play.

a method to display the tic-tac-toe board.

a method checking if a player has won based on the contents of the board; this method takes no parameter. It returns X if the "X player" has won, O if the "O player" has won, T if the game was a tie. A player wins if he or she has placed an X (or an O) in all cells in a row, all cells in a column, or all cells in one of the diagonals.

NOTE: Be sure to display the board after each move. You must provide clear prompts for the human player to select a space on the tic-tac-toe board.

Input Validation: Verify that all moves by the human player are to a valid space on the tic-tac-toe board. An incorrect choice should not halt or terminate the game. Also, no human vs. human mode should be included in the program. Just Human vs. the Computer is what the game should only involve.

Solutions

Expert Solution


// tictactoe.cpp
#include <vector>
#include <iostream>

using namespace std;

const bool CLEAR_SCREEN = true;

void clearScreen()
{
cout << endl;

if (CLEAR_SCREEN)
{
cout << "\033c";
}

cout << endl;
}

void drawBoard(const vector <char> &board)
{
clearScreen();
for (int i = 0; i < 9; i += 3)
{
cout << " " << board.at(i) << " | " << board.at(i + 1) << " | "
<< board.at(i + 2) << " " << endl;
if (i < 6)
cout << "-----|-----|-----" << endl;
}
cout << endl;
}

void initVector(vector <char> &v)
{
if (v.size() <= 26)
{
for (int idx = 0; idx < v.size(); idx++)
{
v.at(idx) = 'a' + idx;
}
}
  
return;
}


int convertPosition(char position)
{

vector <char> newVector(9);
  
  
initVector(newVector);

for (int idx = 0; idx < newVector.size(); idx++)
{
if (position == newVector.at(idx))
return idx;
}
  
return 10;
}


bool validPlacement(const vector <char> &board, int position)
{
  
if (position < board.size())
{
if ((board.at(position) != 'o' && board.at(position) != 'x'))
return true;
}
else
return false;
}

int getPlay(const vector <char> &board)
{
int idx;
char userPosition;

do
{
cout << "Please choose a position: ";
cin >> userPosition;
cout << endl;
  
idx = convertPosition (userPosition);
} while ((!validPlacement(board, idx)));
return idx;
}


bool gameWon(const vector <char> &board)
{
for (int idx = 0; idx < 3; idx++)
{
if (board.at(3*idx) == board.at(3*idx+1) && board.at(3*idx+1) == board.at(3*idx+2))
return true;
else if (board.at(idx) == board.at(idx+3) && board.at(idx+3) == board.at(idx+6))
return true;
}
  
if (board.at(2) == board.at(4) && board.at(4) == board.at(6))
return true;
else if (board.at(0) == board.at(4) && board.at(4) == board.at(8))
return true;
  
  
return false;
}


bool boardFull(const vector <char> &board)
{
int idxJ = 0;
for (int idx = 0; idx < board.size(); idx++)
{
if (board.at(idx) == 'o' || board.at(idx) == 'x')
idxJ++;
}
  
if(idxJ == 9)
return true;
else
return false;
}

const int PLAYER1 = 0;
const int PLAYER2 = 1;

int main()
{
  
vector <char> board(9);
int curPlay;
int turn = PLAYER1;


initVector(board);


drawBoard(board);

while (!boardFull(board) && !gameWon(board))
{
curPlay = getPlay(board);
  
board.at(curPlay) = (turn == PLAYER1) ? 'x' : 'o';
  
if (!boardFull(board) && !gameWon(board))
{
if (turn == PLAYER1)
turn = PLAYER2;
else if (turn == PLAYER2)
turn = PLAYER1;
}
  
drawBoard(board);
}

if (turn == PLAYER1 && gameWon(board))
cout << "Player (x's) wins!" << endl << endl;
else if (turn == PLAYER2 && gameWon(board))
cout << "Computer (o's) wins!" << endl << endl;
else
cout << "No one wins" << endl << endl;


return 0;
}


Related Solutions

Write a class (and a client class to test it) that encapsulates a tic-tac-toe board. A...
Write a class (and a client class to test it) that encapsulates a tic-tac-toe board. A tic-tac-toe board looks like a table of three rows and three columns partially or completely filled with the characters X and O. At any point, a cell of that table could be empty or could contain an X or an O. You should have one instance variable, a two-dimensional array of values representing the tic-tac-toe board. This game should involve one human player vs....
Write a class (and a client class to test it) that encapsulates a tic-tac-toe board. A...
Write a class (and a client class to test it) that encapsulates a tic-tac-toe board. A tic-tac-toe board looks like a table of three rows and three columns partially or completely filled with the characters X and O. At any point, a cell of that table could be empty or could contain an X or an O. You should have one instance variable, a two-dimensional array of values representing the tic-tac-toe board. This game should involve one human player vs....
Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3...
Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3 grid as shown below: The game is played by two players, who take turns. The first player marks moves with a circle, the second with a cross. The player who has formed a horizontal, vertical, or diagonal sequence of three marks wins. Your program should draw the game board, ask the user for the coordinates of the next mark (their move), change the players...
PYTHON (Game: Tic-tac-toe): Write a program that plays the tic-tac-toe game. Two players take turns clicking...
PYTHON (Game: Tic-tac-toe): Write a program that plays the tic-tac-toe game. Two players take turns clicking an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A draw (no winner) occurs when all the cells in the grid have been filled with tokens and neither player has...
Write a LISP program to play the game Tic-Tac-Toe on a size 4x4 game board. Your...
Write a LISP program to play the game Tic-Tac-Toe on a size 4x4 game board. Your program must use min-max search and should be invoked by the function call: > (Tic-Tac-Toe) The game is single player, human vs the computer AI.
C# (Tic-Tac-Toe) Create class TicTacToe that will enable you to write a complete app to play...
C# (Tic-Tac-Toe) Create class TicTacToe that will enable you to write a complete app to play the game of Tic-Tac-Toe. The class contains a private 3-by-3 rectangular array of integers. The constructor should initialize the empty board to all 0s. Allow two human players. Wherever the first player moves, place a 1 in the specified square, and place a 2 wherever the second player moves. Each move must be to an empty square. After each move, determine whether the game...
Python Code Needed Two-Player, Two-Dimensional Tic-Tac-Toe Write a script to play two-dimensional Tic-Tac-Toe between two human...
Python Code Needed Two-Player, Two-Dimensional Tic-Tac-Toe Write a script to play two-dimensional Tic-Tac-Toe between two human players who alternate entering their moves on the same computer. Create a 3-by-3 two-dimensional array. Each player indicates their moves by entering a pair of numbers representing the row and column indices of the square in which they want to place their mark, either an 'X' or an 'O'. When the first player moves, place an 'X' in the specified square. When the second...
Stewart invented “Max” a board game similar to “Tic Tac Toe.” In May 2017, Stewart began...
Stewart invented “Max” a board game similar to “Tic Tac Toe.” In May 2017, Stewart began negotiating with Big Board, Inc., to license “Max” for distribution outside the United States. On June 11, 2017, the parties met and orally discussed terms. As compensation, Big Board promised to pay Stewart the amount due from another board game he had developed for Big Board two years ago. On June 26, 2017, Lisa, a Big Board employee, sent Stewart an email titled “Max...
If you were to write a game of tic-tac-toe, you may store the representation of the...
If you were to write a game of tic-tac-toe, you may store the representation of the game board as a two dimensional list such as   [['X', '', 'X'], ['O', 'X', ''], ['', 'O', 'X']] where each sublist is a row in the board.   Empty strings ('') denote a space that does not yet have a value. Assuming this representation, write functions (contained in the same file) that do the following: a) Create a new empty tic-tac-toe board. This function should...
For this assignment, you will write a tic-tac-toe application in HTML and JavaScript, using an HTML...
For this assignment, you will write a tic-tac-toe application in HTML and JavaScript, using an HTML <canvas> tag. The game will be played "hot seat" where players take turns using the same device. Requirements: The canvas should be 600px tall and wide, with the gameplay area occupying most of the canvas. The X's and O's may be drawn using polygons or large-font text The grid should be drawn using polygons, specifically long, thin rectangles Before & between games, the canvas...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT