Hi,
I've created a program for the Tic Tac Toe game. I'm working on an exercise posted on the cplusplus.com website. Here is the exercise:
I have so far reached the one star ★ part of the exercise and noticed that the program requires arrays, which I have not used. I'm not exactly sure how to use arrays to do the exercise. Could someone point me in the right direction ans well as critiquing my existing code? Here's my code so far:Code:Requires:
variables, data types, and numerical operators
basic input/output
logic (if statements, switch statements)
loops (for, while, do-while)
arrays
Make a two player tic tac toe game.
★ Modify the program so that it will announce when a player has won the game (and which player won, x or o)
★★ Modify the program so that it is a one player game against the computer (with the computer making its moves randomly)
★★★★ Modify the program so that anytime the player is about to win (aka, they have 2 of 3 x's in a row, the computer will block w/ an o)
Code:#include <iostream>
using namespace std;
//void printBoard()
//{
// cout << cell1 <<"|"<< cell2 << "|" << cell3 << endl;
// cout <<"-+-+-"<< endl;
// cout << cell4 <<"|"<< cell5 << "|" << cell6 << endl;
// cout <<"-+-+-"<< endl;
// cout << cell7 <<"|"<< cell8 << "|" << cell9 << endl;
// cout <<"-+-+-"<< endl;
//}
int main()
{
char cell1 = '1';
char cell2 = '2';
char cell3 = '3';
char cell4 = '4';
char cell5 = '5';
char cell6 = '6';
char cell7 = '7';
char cell8 = '8';
char cell9 = '9';
char player = '1';
char mark;
bool gameover = false;
do
{
cout << cell1 <<"|"<< cell2 << "|" << cell3 << endl;
cout <<"-+-+-"<< endl;
cout << cell4 <<"|"<< cell5 << "|" << cell6 << endl;
cout <<"-+-+-"<< endl;
cout << cell7 <<"|"<< cell8 << "|" << cell9 << endl;
cout <<"-+-+-"<< endl;
//printBoard();
//Handle player marker
if (player == '1')
{
mark = 'X';
}
else
{
mark = 'O';
}
bool invalid;
do
{
cout << "Player "<<player<< ": \n";
char move;
cin >> move;
invalid = false;
//Validate move
if (move == '1' && cell1 == '1')
{
cell1 = mark;
}
else if (move == '2' && cell2 == '2')
{
cell2 = mark;
}
else if (move == '3' && cell3 == '3')
{
cell3 = mark;
}
else if (move == '4' && cell4 == '4')
{
cell4 = mark;
}
else if (move == '5' && cell5 == '5')
{
cell5 = mark;
}
else if (move == '6' && cell6 == '6')
{
cell6 = mark;
}
else if (move == '7' && cell7 == '7')
{
cell7 = mark;
}
else if (move == '8' && cell8 == '8')
{
cell8 = mark;
}
else if (move == '9' && cell9 == '9')
{
cell9 = mark;
}
else
{
cout <<" Invalid Move"<<endl;;
invalid = true;
}
}
while (invalid);
//Check for winning & draw conditions
bool draw = false;
if (cell1 != '1')
{
if (cell1 == cell2 && cell1 == cell3)
{
gameover = true;
}
else if (cell1 == cell4 && cell1 == cell7)
{
gameover = true;
}
}
if (cell9 != '9')
{
if (cell9 == cell3 && cell9 == cell6)
{
gameover = true;
}
else if (cell9 == cell8 && cell9 == cell7)
{
gameover = true;
}
}
if (cell5 != '5')
{
if (cell5 == cell2 && cell5 == cell8)
{
gameover = true;
}
else if (cell5 == cell4 && cell5 == cell6)
{
gameover = true;
}
else if (cell5 == cell1 && cell5 == cell9)
{
gameover = true;
}
else if (cell5 == cell3 && cell5 == cell7)
{
gameover = true;
}
}
// Check for draw conditions
if ( cell1 != '1' && cell2 != '2' && cell3 != '3' &&
cell4 != '4' && cell5 != '5' && cell6 != '6' &&
cell7 != '7' && cell8 != '8' && cell9 != '9')
{
cout <<" Its a draw"<<endl;
gameover = true;
draw = true;
}
if (gameover)
{
if (draw == false)
{
cout << "Player "<< player << " wins!"<<endl;
cout <<endl;
}
//printBoard();
cout << cell1 <<"|"<< cell2 << "|" << cell3 << endl;
cout <<"-+-+-"<< endl;
cout << cell4 <<"|"<< cell5 << "|" << cell6 << endl;
cout <<"-+-+-"<< endl;
cout << cell7 <<"|"<< cell8 << "|" << cell9 << endl;
cout <<"-+-+-"<< endl;
cout <<endl;
cout<< "Play again? (y/n)"<< endl;
char playAgain;
cin >> playAgain;
//Reset board if user wants to play again
if (playAgain == 'y')
{
gameover = false;
cell1 = '1';
cell2 = '2';
cell3 = '3';
cell4 = '4';
cell5 = '5';
cell6 = '6';
cell7 = '7';
cell8 = '8';
cell9 = '9';
player = '1';
}
}
else
{
//Alternate players
if (player == '1')
{
player = '2';
}
else
{
player = '1';
}
}
}
while (gameover == false);
return 0;
}
Thanks in advance

