Hi, I need to create a tic tac toe game in visual studio. I need it to be basic
ID: 3837487 • Letter: H
Question
Hi, I need to create a tic tac toe game in visual studio.
I need it to be basic and easily performed. it would be great if you could comment too. and also it would be great if the source code could easily be copied to see the demonstration. some of the rules are, •User plays against the computer
•User or computer starts, next move is done by the opponent and so on
•To win, get three X or three O in a row, column or diagonal •It is draw if the game board is full.
•Computer may be random controlled •3x3 board size. thanks a lot
Explanation / Answer
PROGRAM CODE:
#include <iostream>
using namespace std;
class TicTac
{
private:
//use a 3X3 char array to make the data structure
char board[3][3];
//You can pick your own data structure
public:
//Default Constructor
//Makes a board with all blank spaces
TicTac()
{
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
board[i][j] = '.';
}
}
//Makes a move on the board
//X is the row and y is the column
//m is the symbol to place (either X or O)
//It returns true if the move was made
//If the move is illegal, return false and do not change the table
bool move(char option, int x, int y)
{
if(x<3 && x>=0 && y<3 && y>=0)
{
// I'm not having idea of the symbol data type used here. be used to change the data types
// to fit your requirement
//update the board only when the indexes match
board[x][y] = option;
return true;
}
else return false;
}
//Returns true if the game is over
//This could be because of a winner or because of a tie
bool game_over()
{
//checking if any of the line - vertical or horizontal or diagonals is having the same symbol
for(int i=0; i<3; i++)
{
// if all the three different cells are same then we have a winner
if(board[i][0] == board[i][1] && board[i][1] == board[i][2])
return true;
if(board[0][i] == board[1][i] && board[1][i] == board[2][i])
return true;
if(board[0][0] == board[1][1] && board[1][1] == board[2][2])
return true;
if(board[0][2] == board[1][1] && board[1][1] == board[2][0])
return true;
}
return false;
}
//Returns who won X or O.
//If the game was a tie, return BLANK
char winner()
{
for(int i=0; i<3; i++)
{
// for x checking vertical, horizontal, diagonals
if(board[i][0] == 'X' && board[i][0] == board[i][1] && board[i][1] == board[i][2])
return 'X';
if(board[0][i] == 'X' && board[0][i] == board[1][i] && board[1][i] == board[2][i])
return 'X';
if(board[0][0] == 'X' && board[0][0] == board[1][1] && board[1][1] == board[2][2])
return 'X';
if(board[0][2] == 'X' && board[0][2] == board[1][1] && board[1][1] == board[2][0])
return 'X';
// for o
if(board[i][0] == 'O' && board[i][0] == board[i][1] && board[i][1] == board[i][2])
return 'O';
if(board[0][i] == 'O' && board[0][i] == board[1][i] && board[1][i] == board[2][i])
return 'O';
if(board[0][0] == 'O' && board[0][0] == board[1][1] && board[1][1] == board[2][2])
return 'O';
if(board[0][2] == 'O' && board[0][2] == board[1][1] && board[1][1] == board[2][0])
return 'O';
}
}
//Overload the output operator
friend ostream & operator<<(ostream& os, TicTac& myboard)
{
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
os<<myboard.board[i][j]<<" ";
os<<endl;
}
return os;
}
};
int randomNumber()
{
return rand()%3;
}
int main() {
srand(NULL);
TicTac tictacGame;
cout<<"You are playing against the computer. Computer playes first ";
while(true)
{
while(!tictacGame.move('O', randomNumber(), randomNumber()))
{
}
cout<<"Your turn. Enter row and column: ";
}
cout<<tictacGame;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.