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

How do I convert the code below to meet these requirements? // Tic-Tac-Toe #incl

ID: 3868480 • Letter: H

Question

How do I convert the code below to meet these requirements?


// Tic-Tac-Toe
#include
#include
#include
#include
using namespace std;
// global constants
const char X = 'X';
const char O = 'O';
const char EMPTY = ' ';
const char TIE = 'T';
const char NO_ONE = 'N';
// function prototypes
void instructions();
char askYesNo(string question);
int askNumber(string question, int high, int low = 0);
char humanPiece();
char opponent(char piece);
void displayBoard(const vector& board);
char winner(const vector& board);
bool isLegal(const vector& board, int move);
int humanMove(const vector& board, char human);
int computerMove(vector board, char computer);
void announceWinner(char winner, char computer, char human);
// main function
int main()
{
int move;
const int NUM_SQUARES = 9;
vector board(NUM_SQUARES, EMPTY);
instructions();
const char human = humanPiece();
const char computer = opponent(human);
char turn = X;
displayBoard(board);
while (winner(board) == NO_ONE)
{
if (turn == human)
{
move = humanMove(board, human);
board[move] = human;
}
else
{
move = computerMove(board, computer);
board[move] = computer;
}
displayBoard(board);
turn = opponent(turn);
}
announceWinner(winner(board), computer, human);
return 0;
}
// functions
void instructions()
{
cout << "Welcome to Tic-Tac-Toe. ";
cout << "--where human brain is pit against silicon processor ";
cout << "Type the number 0-8 to select the position you want to choose. ";
cout << "It will be faithfully reflected on the following board: ";
cout << " 0 | 1 | 2 ";
cout << " --------- ";
cout << " 3 | 4 | 5 ";
cout << " --------- ";
cout << " 6 | 7 | 8 ";
cout << "Ready? The war will begin soon! ";
}
char askYesNo(string question)
{
char response;
do
{
cout << question << " (y/n): ";
cin >> response;
} while (response != 'y' && response != 'n');
return response;
}
int askNumber(string question, int high, int low)
{
int number;
do
{
cout << question << " (" << low << " - " << high << "): ";
cin >> number;
} while (number > high || number < low);
return number;
}
char humanPiece()
{
char go_first = askYesNo("Are you sure you want to move first?");
if (go_first == 'y')
{
cout << " Human move first. ";
return X;
}
else
{
cout << " YComputer move first. ";
return O;
}
}
char opponent(char piece)
{
if (piece == X)
{
return O;
}
else
{
return X;
}
}
void displayBoard(const vector& board)
cout << " " << board[0] << " | " << board[1] << " | " << board[2];
cout << " " << "---------";
cout << " " << board[3] << " | " << board[4] << " | " << board[5];
cout << " " << "---------";
cout << " " << board[6] << " | " << board[7] << " | " << board[8];
cout << " ";
}
char winner(const vector& board)
{
const int WINNING_ROWS[8][3] = { {0, 1, 2},
{3, 4, 5},
{6, 7, 8},
{0, 3, 6},
{1, 4, 7},
{2, 5, 8},
{0, 4, 8},
{2, 4, 6} };
const int TOTAL_ROWS = 8;
for(int row = 0; row < TOTAL_ROWS; ++row)
{
if ( (board[WINNING_ROWS[row][0]] != EMPTY) &&
(board[WINNING_ROWS[row][0]] == board[WINNING_ROWS[row][1]]) &&
(board[WINNING_ROWS[row][1]] == board[WINNING_ROWS[row][2]]) )
{
return board[WINNING_ROWS[row][0]];
}
}
if (count(board.begin(), board.end(), EMPTY) == 0)
return TIE;
return NO_ONE;
}
inline bool isLegal(int move, const vector& board)
{
return (board[move] == EMPTY);
}
int humanMove(const vector& board, char human)
{
int move = askNumber("Choose your position.", (board.size() - 1));
while (!isLegal(move, board))
{
cout << " This position is already occupied by a pawn, choose another position. ";
move = askNumber("Choose your position.", (board.size() - 1));
}
cout << "Well... ";
return move;
}
int computerMove(vector board, char computer)
{
unsigned int move = 0;
bool found = false;
while (!found && move < board.size())
{
if (isLegal(move, board))
{
board[move] = computer;
found = winner(board) == computer;
board[move] = EMPTY;
}
if (!found)
{
++move;
}
}
if (!found)
{
move = 0;
char human = opponent(computer);
while (!found && move < board.size())
{
if (isLegal(move, board))
{
board[move] = human;
found = winner(board) == human;
board[move] = EMPTY;
}
if (!found)
{
++move;
}
}
}
if (!found)
{
move = 0;
unsigned int i = 0;
const int BEST_MOVES[] = {4, 0, 2, 6, 8, 1, 3, 5, 7};
while (!found && i < board.size())
{
move = BEST_MOVES[i];
if (isLegal(move, board))
{
found = true;
}
++i;
}
}
cout << "The position computer chose is: " << move << endl;
return move;
}
void announceWinner(char winner, char computer, char human)
{
if (winner == computer)
{
cout << winner << " Computer Win! ";
}
else if (winner == human)
{
cout << winner << "'Human Win! ";
}
else
{
cout << "Draw. ";
}
}

Write a class called Board that represents a tic-tac-toe board. It should have a 3x3 array as a data member, which will store the locations of the players' moves. It should have a default constructor that initializes the 3x3 array to being empty. It should have a method called makeMove that takes the x and y coordinates of the move (see the example below) and which player's turn it is as parameters. If that location is unoccupied, makeMove should record the move and return true. If that location is already occupied, makeMove should just return false. There should be a method called gameState that takes no parameters and returns one of the four following values: X_WON, O_WON DRAW, or UNFINISHED-use an enum for this, not strings (the enum definition should go in Board.hpp, before the Board class definition). There should also be a method called print, which just prints out the current board to the screen Write a class called TicTacToe that allows two people to play a game. This class will have a field for a Board object and a field to keep track of which player's turn it is. It should have a constructor that takes a char parameter that specifies whether 'x' or 'o' should have the first move. It should have a method called play that starts the game. The play method should keep looping, asking the correct player for their move and sending it to the board (with makeMove) until someone has won or it's a draw (as indicated by gameState), and then declare what the outcome was Write a main method (in TicTacToe.cpp) that asks the user which player should go first, creates a new TicTacToe object and starts the game. For this assignment only, you will not comment out your main method. Input validation: If someone tries to take an occupied square, tell them that square is already occupied and ask for a different move. Here's an example portion of a game (already in progress): 012 0 x 2 Player O: please enter your move 1 2

Explanation / Answer

TicTacToe.h

#include<iostream>
#include <string>
using std::cout;
using std::cin;
using std::endl;
using std::string;
class TTT
{
public:
void dispBoard();
bool getPlayaMv(int & io, int & qq);
bool chckForValidMv(int io, int qq);
char winner();
bool isWinner(char ch);
bool checkDraw();

TTT();

private:
char Board[3][3];
char nxtSym;
};

TicTacToe.cpp

#include "TicTacToe.h"

void TTT::dispBoard()
{
cout << "Board:" << endl;

cout << "Play " << nxtSym << ": make a selection based on the row and column:" << endl;

cout << " 0 1 2" << endl;

for(int uu=0; uu<3; ++uu)
{
cout << " |-----|-----|-----|" << endl;
cout << " | | | |" << endl;
cout << uu<< " | " << Board[uu][0] << " | " << Board[uu][1] << " | " << Board[uu][2] << " |" << endl;
cout << " | | | |" << endl;
}
cout << " |-----|-----|-----|" << endl;
}

bool TTT::getPlayaMv(int & io, int & qq)
{
cout << "Enter your move by selecting row and column numbers: ";
cin >> io;
cin >> qq;
while( !chckForValidMv(io,qq) )
{
cout << "Incorrect move; Try again." << endl;
cout << "Enter your move by selecting row and column numbers: ";
cin >> io;
cin >> qq;
}
Board[io][qq] = nxtSym;
if (nxtSym == 'X' )
nxtSym = 'O';
else
nxtSym = 'X';
return true;
}
bool TTT::chckForValidMv(int io, int qq)
{
if (((io >=0 && io <= 2) && (qq >=0 && qq <= 2) && (Board[io][qq] == ' ') ))
return true;
else
return false;
}
bool TTT::checkDraw()
{
for (int uu=0; uu<3; uu++)
for (int tt=0; tt<3; tt++)
if (Board[uu][tt] == ' ') return false;
return true;
}
char TTT::winner()
{
char ch = 'D';
if (isWinner('X'))
return 'X';
if (isWinner('O'))
return 'O';
if (checkDraw())
return 'D';
return ' ';
}
bool TTT::isWinner(char ch)
{
for (int uu=0; uu<3; uu++)
{
if (Board[uu][0] == ch && Board[uu][1] == ch && Board[uu][2] == ch)
return true;
if (Board[0][uu] == ch && Board[1][uu] == ch && Board[2][uu] == ch)
return true;
}
if (Board[0][0] == ch && Board[1][1] == ch && Board[2][2] == ch)
return true;
if (Board[2][0] == ch && Board[1][1] == ch && Board[0][2] == ch)
return true;
return false;
}

TTT::TTT()
{
for (int uu=0; uu<3; ++uu)
for (int tt=0; tt< 3; ++tt)
Board[uu][tt]= ' ';
nxtSym = 'X';
}

Testing Method

int main()
{
int io, qq;
char ch=' ';
TTT myTicTacTo;

myTicTacTo.dispBoard();

do
{
myTicTacTo.getPlayaMv(io, qq);
myTicTacTo.dispBoard();
ch= myTicTacTo.winner();
if (ch != ' ')
{
if (ch == 'D')
cout << "It is a draw!" << endl;
else
cout << "Player " << ch << " is the winner. " << endl;
break;
}

} while(true);


return 0;
}

Rate the answer an upvote.......Thankyou

Hope this helps.......

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