Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello. I am working on C++ in my CSIT class and I have coded a Tic Tac Toe game. For extra credit I want to

Hello. I am working on C++ in my CSIT class and I have coded a Tic Tac Toe game. For extra credit I want to develop an AI player to play against but I do not know how. Would love some help, thanks!

testingTTT.cpp

#include

#include "ticTacToe.h"

using namespace std;

int main()

{

ticTacToe g1;

g1.play();

system("pause");

return 0;

}

ticTacToe.h

class ticTacToe

{

public:

ticTacToe(); //default constructor

void play(); // it allows you and a friend to play tic tac toe

private:

void print() const; //print the status of the board

void takeTurn(); //will ask the player whoes turn it is to enter a play

int winning(); //returns 1 if there is a winning move by player 1, -1 if there is a winning move by player 2; 0 otherwise

int board[3][3]; //the board for the game; 0- unoccupied; 1- occupied by player 1; -1 - occupied by player 2

bool playerTurn; //true if the turn of player 1; false otherwise

int turn; //0..9 indicates the overall turn

};

ticTacToeImp.cpp

#include

#include "ticTacToe.h"

using namespace std;

ticTacToe::ticTacToe() //default constructor

{

playerTurn = true; //by default, 1st player goes first

turn = 0; //no one has played yet

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

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

board[i][j] = 0;

}

void ticTacToe::print() const //print the status of the board

{

char A[3][3];

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

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

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

A[i][j] = ' ';

else if (board[i][j] == 1)

A[i][j] = 'o';

else

A[i][j] = 'x';

char v = 179, h = 196, c = 197;

cout << " 1 2 3" << endl;

cout << "1 " << A[0][0] << " " << v

<< " " << A[0][1] << " " << v << " " << A[0][2] << " " << endl;

cout << " " << h << h << h << c << h << h << h << c << h << h << h << endl;

cout << "2 " << A[1][0] << " " << v

<< " " << A[1][1] << " " << v << " " << A[1][2] << " " << endl;

cout << " " << h << h << h << c << h << h << h << c << h << h << h << endl;

cout << "3 " << A[2][0] << " " << v

<< " " << A[2][1] << " " << v << " " << A[2][2] << " " << endl;

}

void ticTacToe::takeTurn()

{

int x, y;

if (playerTurn) //player 1's turn

{

cout << "Player 1 enter your move" << endl;

cin >> x >> y;

board[x-1][y-1] = 1;

turn++;

playerTurn = false;

}

else

{

cout << "Player 2 enter your move" << endl;

cin >> x >> y;

board[x - 1][y - 1] = -1;

turn++;

playerTurn = true;

}

}

int ticTacToe::winning()

{

for (int r = 0; r < 3; r++)

{

int s = 0;

for (int c = 0; c < 3; c++)

s += board[r][c];

if (s == 3)

return 1;

else if (s == -3)

return -1;

}

for (int c = 0; c < 3; c++)

{

int s = 0;

for (int r = 0; r < 3; r++)

s += board[r][c];

if (s == 3)

return 1;

else if (s == -3)

return -1;

}

int s1 = 0, s2=0;

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

{

s1 += board[i][i];

s2 += board[2 - i][i];

}

if (s1 == 3 || s2 == 3)

return 1;

else if (s1 == -3 || s2 == -3)

return -1;

return 0;

}

void ticTacToe::play()

{

int w = winning();

while (w == 0 && turn < 9)

{

print();

takeTurn();

w = winning();

}

print();

if (w == 0)

cout << "draw!" << endl;

else if (w == 1)

cout << "Player 1 wins!" << endl;

else

cout << "Player 2 wins!" << endl;

}

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

Database Systems Introduction To Databases And Data Warehouses

Authors: Nenad Jukic, Susan Vrbsky, Svetlozar Nestorov

1st Edition

1943153191, 978-1943153190

More Books

Students also viewed these Databases questions

Question

What is an interface? What keyword is used to define one?

Answered: 1 week ago

Question

KEY QUESTION Refer to columns 1 and 6 in the table for question

Answered: 1 week ago