218k views
4 votes
Programming assignment (100 pts): In the C++ programming language write a program capable of playing Tic-Tac-Toe against the user. Your program should use OOP concepts in its design. You can use ASCII art to generate and display the 3x3 playing board. The program should randomly decide who goes first computer or user. Your program should know and inform the user if an illegal move was made (cell already occupied). The program should also announce if one of the players wins or if a draw is achieved. While it is desirable for your program to play a strong game, this is not an Artificial Intelligence course so if your program does not play at a world champion level you will not be penalized for it.

User Jamira
by
3.2k points

1 Answer

5 votes

Answer:

#include <iostream>

#include <ctime>

#include <string> // for string methods

using namespace std;

class Board

{

private:

char squares[3][3]; //two-dimensional arrays

public:

Board() {}

void PrintBoard();

void BeginGame();

void playerTurn (char num, char Player);

bool CheckWin (char Player, bool gameOver);

bool CheckDraw(bool gameOver);

};

//Organize board for the game

void Board::BeginGame()

{

int n = 1;

int i = 0;

int j = 0;

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

{

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

{

squares[i][j] = '0' + n; //Casting n value to a character

n++;

}

}

} //End SetGameBoard

//printing board

void Board::PrintBoard()

{

int i = 0;

int j = 0;

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

{

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

{

if ( j < 2 )

cout << squares[i][j] << "

else

{

cout << squares[i][j] << endl;

}

}

}

} //End

void Board:: playerTurn (char num, char Player)

{

int i = 0;

int j = 0;

bool WrongMove = true; //If the value is true then the player has made a wrong move

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

{

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

{

if(squares[i][j] == num)

{

//Assigns the space with the X or O,

squares[i][j] = Player;

WrongMove = false;

}

}

}

if(WrongMove == true) { cout << "You can only mark the open sets!\\"; }

} //End Player Move

//checking for the winner. If not, cotinue or a draw

bool Board:: CheckWin (char Player, bool GameOver)

{

for(int i = 0; i < 3; i++) //To check rows

{

if(squares[i][0] == squares[i][1] && squares[i][1] ==

squares[i][2]) GameOver = true;

}

for(int i = 0; i < 3; i++) //To check columns

{

if(squares[0][i] == squares[1][i] && squares[1][i] ==

squares[2][i]) GameOver = true;

}

if(squares[0][0] == squares[1][1] && squares[1][1] == squares[2][2]) //To check the Diagonals

{

GameOver = true;

}

if(squares[0][2] == squares[1][1] && squares[1][1] == squares[2][0])

{

GameOver = true;

}

if(GameOver == true)

cout << "Player " << Player << " wins!\\\\";

cout << "-----------------------" << endl;

cout << "

return GameOver;

}

bool Board:: CheckDraw(bool GameOver)

{

int n = 1;

int i = 0;

int j = 0;

int counter = 0;

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

{

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

{

//Check to see if the board if full

if(squares[i][j] == '0' + n)

{

counter++;

}

n++;

}

}

if( counter < 1 )

{

cout << "Game Draw\\\\";

GameOver = true;

}

return GameOver;

}

// Main method starts here

int main()

{

bool finish = false, GameOver = false,isValid;

char Player = 'O', num;

int number;

srand(time(NULL)); //seed random from time

number = rand()%2; //Randomly choose a player to start game

if(number == 0)

Player = 'X';

else

Player = 'O';

cout << "" << endl;

cout << "-=Tic-Tac-Toe=-\\";

cout << "-------------"<< endl;

Board TicTac;

TicTac.BeginGame();

TicTac.PrintBoard(); //Initialize and output board

do

{

if( Player == 'X' )

{

Player = 'O';

}

else

{

Player = 'X';

}

TicTac.PrintBoard();

cout << "\\Player \"" << Player << "\", it's your turn: ";

cin >> num;

cout << "\\";

TicTac.playerTurn (num, Player);

GameOver = TicTac.CheckWin (Player, GameOver);

GameOver = TicTac.CheckDraw(GameOver);

if(GameOver == true)

{

cout << "Thank you for playing!";

finish = true;

}

} while(!finish);

return 0;

}

User Xodarap
by
4.0k points