Following code is for 4×4 tic toc toe  game covert this code in to 3×3 players. Code

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Following code is for 4×4 tic toc toe  game covert this code in to 3×3 players.

Code

#include <iostream>

 

using namespace std;

 

 

 

class Board

{

char board[4][4];

public:

Board()

{

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

    {

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

        {

            board[i][j]='_';

        }

    }

}

 

 

void printBoard()

{

 

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

    {

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

        {

            cout<<board[i][j]<<'|';

        }

    cout<<endl;

    }

cout<<endl;

}

int setPosition(char choice)

{

while(1)

{

int row,column;

cout<<"Please enter the row and column"<<endl;

cin>>row>>column;

if(board[row][column]=='_')

{

board[row][column]=choice;

break;

 

}

else

{

cout<<"Position is taken"<<endl;

 

}

}

}

char getPosition()

{

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

{

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

    {

        return board[i][j];

    }

    }

 

}

int checkHorizontal(char choice)

{

 

 

int count;

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

{

    count=0;

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

    {

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

        {

            count++;

        }

        if(count==4)

        {

 

            return 1;

        }

    }

 

}

return 0;

}

int checkVertical(char choice)

{

int count;

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

{

    count=0;

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

    {

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

        {

            count++;

        }

        if(count==4)

        {

 

            return 1;

        }

    }

 

}

return 0;

}

int checkprincipalDiagonal(char choice)

{

int count=0;

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

{

    if (board[i][i]==choice)

    {

        count++;

    }

 

    }

if(count==4)

{

 

  return 1;

}

 

 

return 0;

}

int checkotherDiagonal(char choice)

{

int count=0;

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

{

 

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

    {

 

        if((i+j)%3==0 && i!=j)

        {

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

        {

           count++;

        }

        }

 

 

    }

}

 

if(count==4)

{

 

    return 1;

}

    else

    {

        return 0;

    }

}

int checkDraw()

{

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

{

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

    {

        if(board[i][j]=='_')

        {

            return 1;

            break;

        }

 

    }

 }

return 0;

}

 

};

class Player

{

string Name;

char choice;

public:

void setName(string NameIn)

{

 Name=NameIn;

}

void setChoice(char choiceIn)

{

    choice=choiceIn;

}

char getChoice()

{

    return choice;

    cout<<endl<<endl;

}

 

string getName()

{

    return Name;

    cout<<endl<<endl;

}

};

int main()

{

char choice1,choice2;

string Name1,Name2;

Board b1;

cout<<"The board is printed below:"<<endl;

b1.printBoard();

cout<<endl;

Player p1,p2;

cout<<"Player 1,Please Enter your name"<<endl<<endl;

cin>>Name1;

cout<<endl<<endl<<"Player 2,Please Enter your name"<<endl<<endl;

cin>>Name2;

p1.setName(Name1);

p2.setName(Name2);

cout<<endl<<endl<<"Player 1,Please Enter a character to use"<<endl<<endl;

cin>>choice1;

cout<<endl<<endl<<"Player 2,Please Enter a character to use"<<endl<<endl;

cin>>choice2;

p1.setChoice(choice1);

p2.setChoice(choice2);

while(1)

{

cout<<endl<<endl<<"Your turn,"<<Name1<<endl<<endl;

b1.setPosition(choice1);

b1.getPosition();

b1.printBoard();

if(b1.checkHorizontal(choice1)==1 || b1.checkVertical(choice1)==1|| 

b1.checkprincipalDiagonal(choice1)==1 || b1.checkotherDiagonal(choice1)==1)

{

    cout<<endl<<endl<<"Congrats"<<"\t"<<Name1<<"\t"<<"You have won"<<endl;

    break;

}

cout<<endl<<endl<<"Your turn,"<<Name2<<endl<<endl;

b1.setPosition(choice2);

b1.getPosition();

b1.printBoard();

 

if(b1.checkHorizontal(choice2)==1 || b1.checkVertical(choice2)==1|| 

b1.checkprincipalDiagonal(choice2)==1 || b1.checkotherDiagonal(choice2)==1)

{

    cout<<endl<<endl<<"Congrats "<<Name2<<" You Have won!"<<endl;

    break;

}

if(b1.checkDraw()==0)

{

    cout<<endl<<endl<<"The match is a draw!"<<endl;

    break;

}

 

 

}

 

 

 

}

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Matrix multiplication
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education