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

The code shown below is for a tic tac toe game where one user plays against anot

ID: 3737879 • Letter: T

Question

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 <iostream>
#include <fstream>
#include <cstdlib>
#include<string>
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<boardSize;i++)
    {
        for (int j=0; j<boardSize;j++)
        {
            board[i][j] = 0;
        }
    }
}

void ticTacToe::printBoard()
{
        for (int i=0; i<boardSize;i++)
        {
            for (int j=0; j<boardSize; j++)
            {
                cout << board[i][j];
            }
            cout << endl;
        }
}

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<boardSize;i++)
    {
        for (int j=0; j<boardSize;j++)
        {
            int n = 0;
            if (board[i][j]==ch)
            {
                n++;
            }
            else
            {
                break;
            }
        }
    }

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

    return false;

}

bool ticTacToe::checkEveryColumn()
{
    for (int i=0;i<boardSize;i++)
    {
        for (int j=0; j<boardSize;j++)
        {
            if (board[j][i]==ch)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}

bool ticTacToe::checkDiagonal1()
{
    for (int i=0;i<boardSize;i++)
    {
        if (board[i][j]==ch)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

bool ticTacToe::checkDiagonal2()
{
    for (int i=0;i<boardSize;i++)
    {
       int j = boardSize-1;
       if (board[i][j]==ch)
       {
           return true;
       }
       else
       {
           return false;
       }
       j--;
    }
}
*/

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;
}

Explanation / Answer

#include <iostream>

#include <fstream>

#include <iomanip>

#include <cstdlib>

#include <string>

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 < boardSize; i++)

{

for (int j = 0; j < boardSize; j++)

{

board[i][j] = 0;

}

}

}

void ticTacToe::printBoard()

{

for (int i = 0; i < boardSize; i++)

{

for (int j = 0; j < boardSize; j++)

{

cout << setw(3) << board[i][j];

}

cout << endl;

}

}

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<boardSize;i++)

{

for (int j=0; j<boardSize;j++)

{

int n = 0;

if (board[i][j]==ch)

{

n++;

}

else

{

break;

}

}

}

if (n==boardSize)

{

return true;

}

return false;

}

bool ticTacToe::checkEveryColumn()

{

for (int i=0;i<boardSize;i++)

{

for (int j=0; j<boardSize;j++)

{

if (board[j][i]==ch)

{

return true;

}

else

{

return false;

}

}

}

}

bool ticTacToe::checkDiagonal1()

{

for (int i=0;i<boardSize;i++)

{

if (board[i][j]==ch)

{

return true;

}

else

{

return false;

}

}

}

bool ticTacToe::checkDiagonal2()

{

for (int i=0;i<boardSize;i++)

{

int j = boardSize-1;

if (board[i][j]==ch)

{

return true;

}

else

{

return false;

}

j--;

}

}

*/

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++;

}

int i, j, c;

for (i = 0; i < boardSize; i++)

{

c = 0;

for (j = 0; j < boardSize; j++)

{

//check every row

if (board[i][j] == ch)

c++;

}

if (c == boardSize)

return true;

}

for (i = 0; i < boardSize; i++)

{

c = 0;

for (j = 0; j < boardSize; j++)

{

//check every column

if (board[j][i] == ch)

c++;

}

if (c == boardSize)

return true;

}

c = 0;

for (i = 0; i < boardSize; i++)

{

for (j = 0; j < boardSize; j++)

{

// check diagnonal 1

if (i == j && board[i][j] == ch)

c++;

}

}

if (c == boardSize)

return true;

c = 0;

for (i = 0; i < boardSize; i++)

{

for (j = 0; j < boardSize; j++)

{

// check diagnonal 2

if (i + j == boardSize - 1 && board[i][j] == ch)

c++;

}

}

if (c == boardSize)

return true;

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;

}