Question

In: Computer Science

Programming assignment (100 pts): In the C++ programming language write a program capable of playing Tic-Tac-Toe...

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.

Solutions

Expert Solution

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


Related Solutions

In the C++ programming language write a program capable of playing Tic-Tac-Toe against the user. Your...
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...
The objective of this assignment is to implement the tic-tac-toe game with a C program. The...
The objective of this assignment is to implement the tic-tac-toe game with a C program. The game is played by two players on a board defined as a 5x5 grid (array). Each board position can contain one of two possible markers, either ‘X’ or ‘O’. The first player plays with ‘X’ while the second player plays with ‘O’. Players place their markers in an empty position of the board in turns. The objective is to place 5 consecutive markers of...
Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3...
Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3 grid as shown below: The game is played by two players, who take turns. The first player marks moves with a circle, the second with a cross. The player who has formed a horizontal, vertical, or diagonal sequence of three marks wins. Your program should draw the game board, ask the user for the coordinates of the next mark (their move), change the players...
Write a Java program to play the game Tic-Tac-Toe. Start off with a human playing a...
Write a Java program to play the game Tic-Tac-Toe. Start off with a human playing a human, so each player makes their own moves. Follow the design below, creating the methods indicated and invoking them in the main program. Use a char array of size 9 as the board; initialize with the characters 0 to 8 so that it starts out looking something like the board on the left. 0|1|2 3|4|5 6|7|8 and then as moves are entered the board...
Using C language Problem!! <3 x 3 tic tac toe> 2 players are doing tic tac...
Using C language Problem!! <3 x 3 tic tac toe> 2 players are doing tic tac toe alternatively. Anyone could win when they first make a bingo for any line(horizontal, vertical, diagonal) [Constraints] 1. Use 2dimensional Array(3 X 3) 2. Each elements are 1 or 2 so that can show their player1, player2's mark. 3. Inputs are 1 through 9 as you can see 1 2 3 4 5 6 7 8 9 4. No inputs are duplicated 5. if...
PYTHON (Game: Tic-tac-toe): Write a program that plays the tic-tac-toe game. Two players take turns clicking...
PYTHON (Game: Tic-tac-toe): Write a program that plays the tic-tac-toe game. Two players take turns clicking an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A draw (no winner) occurs when all the cells in the grid have been filled with tokens and neither player has...
In C# A Tic Tac Toe program that uses functions and arrays. The program should start...
In C# A Tic Tac Toe program that uses functions and arrays. The program should start by asking the player one for their name and then player two for their name. Then should print the board and ask the user what column and then what row they would like to put their x (or o, depending on the player) . Use the clear function to make it look as though the board is never repeating.. Thanks!
For this assignment, you will write a tic-tac-toe application in HTML and JavaScript, using an HTML...
For this assignment, you will write a tic-tac-toe application in HTML and JavaScript, using an HTML <canvas> tag. The game will be played "hot seat" where players take turns using the same device. Requirements: The canvas should be 600px tall and wide, with the gameplay area occupying most of the canvas. The X's and O's may be drawn using polygons or large-font text The grid should be drawn using polygons, specifically long, thin rectangles Before & between games, the canvas...
Tony Gaddis C++ Tic-Tac-Toe Write a program that allows two players (player X and player O)...
Tony Gaddis C++ Tic-Tac-Toe Write a program that allows two players (player X and player O) to play a game of tic-tac-toe. Use a two- dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The players take turns making moves and the program keeps track of whose turn it is. Player X moves first. The program should run a loop that: Displays the contents...
explain a pseudocode for tic tac toe in c++ between a computer and a player in...
explain a pseudocode for tic tac toe in c++ between a computer and a player in words
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT