Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The code shown below is for a tic tac toe game where one user plays against another on a 3x3 board. How would I modify

The code shown below is for a tic tac toe game where one user plays against another on a 3x3 board. How would I modify the code so that if I wanted to increase the board size from 3by3 to a bigger size (E.G 6by6) the condition for winning would be 5 in a row of either player X or player O, horizontally, vertically or diagonally? Please edit the bold part of the code where I brute forced the winning condition instead of varying it according to board size. Please replace with correct code. As soon as 5 in a row is achieved either horizontally, vertically or diagonally the game should end.

#include #include #include #include using namespace std;

class ticTacToe { public:

ticTacToe(int); void setBoard(); void printBoard(); // bool checkEveryRow(); //bool checkEveryColumn(); // bool checkDiagonal1(); // bool checkDiagonal2(); bool determineWinner(); void moves(); bool validateMove(); void play();

private:

char **board; int counter; int row; int column; char ch; string algorithm; int boardSize;

};

//constructor ticTacToe::ticTacToe(int boardSize) { // create board of size nxn this->board = (char **)malloc(boardSize * sizeof(char *)); int i;

for( i = 0 ; i < boardSize ; i++ ) this->board[i] = (char *)malloc(boardSize * sizeof(char));

this->boardSize = boardSize;

setBoard(); }

void ticTacToe::setBoard() { counter=0;

for (int i=0; i

void ticTacToe::printBoard() { for (int i=0; i

void ticTacToe::moves() { cout << "Player " << counter%2+1 << endl; cout << "Enter row:" << endl; cin >> row; cout << "Enter column:" << endl; cin >> column; }

bool ticTacToe::validateMove() { if ((row<1||row>boardSize)||(column<1||column>boardSize)||(board[row-1][column-1]!=0)) { cout << "Invalid entry" << endl; return false; } else { return true; } }

/* bool ticTacToe::checkEveryRow() { for (int i=0;i

if (n==boardSize) { return true; }

return false;

}

bool ticTacToe::checkEveryColumn() { for (int i=0;i

bool ticTacToe::checkDiagonal1() { for (int i=0;i

bool ticTacToe::checkDiagonal2() { for (int i=0;i

bool ticTacToe::determineWinner() {

if (counter%2==0) { ch = 'X'; } else { ch = 'O'; }

if (validateMove()) { board[row-1][column-1] = ch; if (counter%2==0) { algorithm = "alg1"; } else { algorithm = "alg2"; } cout << "r" << row << "c" << column << " " << algorithm << endl; counter ++;

}

if (((board[0][0]==ch)&&(board[0][1]==ch)&&(board[0][2]==ch))|| //check every row ((board[1][0]==ch)&&(board[1][1]==ch)&&(board[1][2]==ch))|| ((board[2][0]==ch)&&(board[2][1]==ch)&&(board[2][2]==ch))|| ((board[0][0]==ch)&&(board[1][0]==ch)&&(board[2][0]==ch))|| //check every column ((board[0][1]==ch)&&(board[1][1]==ch)&&(board[2][1]==ch))|| ((board[0][2]==ch)&&(board[1][2]==ch)&&(board[2][2]==ch))|| ((board[0][0]==ch)&&(board[1][1]==ch)&&(board[2][2]==ch))|| //check diagonal one ((board[0][2]==ch)&&(board[1][1]==ch)&&(board[2][0]==ch))) //check diagonal two { return true; }

else if (counter==boardSize*boardSize) { cout << "Draw."<< endl; printBoard(); setBoard(); }

return false; }

void ticTacToe::play() { setBoard(); do { moves();

if (determineWinner()) { if (counter%2==0) { cout << "win=alg2" << endl; cout << endl; printBoard(); } else { cout << "win=alg1" << endl; cout << endl; printBoard(); } break; } else { printBoard(); } } while (true); }

int main() { int boardSize = 0; ifstream inData; inData.open("input.txt"); inData >> boardSize; cout << "size=" << boardSize << endl; ticTacToe t(boardSize); cout << "Tic Tac Toe:" << endl; cout << endl; t.play(); return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Sql All In One For Dummies 7 Books In One

Authors: Allen G Taylor ,Richard Blum

4th Edition

1394242298, 978-1394242290

More Books

Students also viewed these Databases questions