In: Computer Science
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.
I've been looking at other post for this question and nothing is working.
Hi, as your requirement there is oops followed tic-tac-toe game with all requirements you illustrated. I have provided the code for same below. I have also commented the code so you can understand. if there is anything else feel free to ask.
// code goes below..
#include <bits/stdc++.h>
#include <time.h>
using std::cout;
class TicTacToe
{
private:
int pos[9]; // board position
int player; // current player
int totalTurns; // how many turns so far?
int numberPlayers; // 1 to 20
bool playerType[20]; // player #, 0 = comp, 1 = human
public:
TicTacToe();
void printTurn();
bool playerHuman();
void humanMove();
void computerMove();
void drawBoard();
bool Winner();
bool fullBoard();
void nextTurn();
};
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
// Tic Tac Toe constructor
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
TicTacToe::TicTacToe()
{
srand (time(0));
player = (rand() % 10 + 1)>5?1:2; // who starts first?
totalTurns = 0;
// new player setup
numberPlayers = 2;
playerType[1] = 1; // playerType[player] is human (1)
playerType[2] = 0; // playerType[player] is computer (0)
for (int i = 0; i < 9; i++)
{
pos[i] = 0;
}
}
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
// Draw the Tic Tac Toe Board and seperate the board
// with the help of |
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
void TicTacToe::drawBoard()
{
std::cout << std::endl
<< pos[0] << " | " << pos[1] << " | "
<< pos[2]
<< "\n−\n"
<< pos[3] << " | " << pos[4] << " | "
<< pos[5]
<< "\n─\n"
<< pos[6] << " | " << pos[7] << " | "
<< pos[8]
<< std::endl;
}
void TicTacToe::printTurn()
{
if(player == 1)
std::cout << "\nPlayer 1s turn.\n";
else
std::cout << "\nComputer turn.\n";
}
void TicTacToe::nextTurn()
{
totalTurns++;
if (++player > numberPlayers)
player = 1;
}
bool TicTacToe::playerHuman()
{
return playerType[player];
}
void TicTacToe::humanMove()
{
std::cout << "\nEnter your move: ";
int move;
START:
std::cin >> move;
move--;
if(move < 0 || move > 8 || pos[move] != 0)
{
cout<<"\nInvalid move, Please enter another
move\n";
goto START;
}
pos[move] = player;
}
void TicTacToe::computerMove()
{
int move;
do
{
// Pick a random number from 1 − 9
move = rand() % 9; // just pick a random number
}
while (move < 0 || move > 8 || pos[move] != 0);
pos[move] = player;
}
bool TicTacToe::Winner()
{
int board[8][3] = {{0,1,2},
// winning possibilities
{3,4,5},
{6,7,8},
{0,3,6},
{1,4,7},
{2,5,8},
{0,4,8},
{2,4,6}};
// Go through the list of possibilities
for (int i = 0; i < 8; i++)
{
if ((pos[board[i][0]] == pos[board[i][1]]) &&
(pos[board[i][1]] == pos[board[i][2]]) && pos[board[i][0]]
!= 0)
{
if(pos[board[i][0]] == 1)
cout << "\nPlayer 1s wins!\n\n";
else
cout << "\nComputer wins!\n\n";
return 1; // return winner true
}
}
return 0; // winner false
}
bool TicTacToe::fullBoard()
{
if (totalTurns == 9)
{
cout << "\nTie game!\n\n";
return 1;
}
else
return 0;
}
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
// Main function of the game
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
int main()
{
cout<<" INSTRUCTIONS \n";
cout<<"#####################################################\n";
cout<<"The instructions for Tic Tac Toe are as
follows: \n";
cout<<"The First move will be randomly
chosen\n";
cout<<"The human is represented by a
Player1.\n";
cout<<"Continue until there are three of the
same number in a row or the board is full.\n";
cout<<"If the board is full and no one wins then
tie!!\n\n";
cout<<"#####################################################\n\n";
TicTacToe Game;
Game.drawBoard();
cout<<"\n\nInitial Structure Drawn!!\n\n";
do
{
Game.printTurn();
if (Game.playerHuman()) // human turn?
Game.humanMove();
else
Game.computerMove();
Game.drawBoard();
Game.nextTurn();
}
// Keep running the game until there is a winner and the game board
is full
while (!Game.Winner() && !Game.fullBoard());
return 0;
}