Tic Tac Toe

I needed to write a tictactoe game for class and hit some snags. Don't worry, the assignment was all ready turne in, but I wanted to get some answers for myself.

Here are the problems I am having...

1) I can't get the game counters to work. If O wins or X wins, it should count those wins and loses for each player. I also wanted to keep count of the draws.

2) Once one game is played, durring the second game, after the 4th move is made it ends and chalks up a win for the last move made.

3) I also wanted to draw the board using an array, but couldn't figure that out.

If anyone could help that would be great. I've been banging my head against the wall for over two weeks with this.
////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <cstdlib>
#include <string>
#include <cctype>
#define NOT !
#define AND &&
#define OR ||
using namespace std;

// global Variables
char answer;
char p1='1', // position 1 on the board
p2='2', // position 2 on the board
p3='3', // position 3 on the board
p4='4', // position 4 on the board
p5='5', // position 5 on the board
p6='6', // position 6 on the board
p7='7', // position 7 on the board
p8='8', // position 8 on the board
p9='9'; // position 9 on the board
char pos; // the position of the next move
char symbol; // the symbol used on the next move
char playAgain; // allows to play again without ending the program

bool valid; // used to track whether a move was valid
int freeSpaces = 9; // used to indicate # of spaces remaining
int iGameCountX = 0; // Counts the number of games won by X
int iGameCountO = 0; // Counts the number of games won by O
int iGameCountD = 0; // Counts the number of games tied
string X; // Player 1's name
string O; // Player 2's name

// function declarations
void clearBoard(); // Function that resets the board
void drawBoard(); // Function that Draws the board
void chooseXandO(); // Function that chooses who is X and O
void xMove(); // Function that controls X's move
void oMove(); // Function that controls O's move
void displayResults(); // Function that displays the win/loss/tie results
void playerMove(); // Function that controls valid moves
void displayPlayAgain(); // Function that repeats the game

bool gameIsOver(); // Function that determines if the game is over
bool bGameCountX(); // Function that counts wins by X
bool bGameCountO(); // Function that counts wins by O
bool bGameCountD(); // Function that counts Draws

int main() // Main Function
{
do
{
clearBoard();
drawBoard(); // Draws the board
chooseXandO(); // Chooses who is X and O

while ( NOT gameIsOver() ) // while (game is Not over)
{
xMove();
if ( NOT gameIsOver() ) // if (game is Not over)
{
oMove();
}

}

displayResults(); // displays win/loss/tie results

cout << "Play again? (Y or N)\n"; // starts loop for playing again
cin >> playAgain;

playAgain = toupper(playAgain); // changes case of input to match if loop

if( playAgain == 'N' )
{ cout << "Thank you for playing!\n";

}
}
while ( playAgain == 'Y' );
system("pause"); // pauses the screen
return 0; // returns nothing to the screen

}

// function definitions
void drawBoard() // Function definition to draw the board
{
cout << " | | \n";
cout << " "
<< p1 << " | "
<< p2 << " | "
<< p3 << " \n";
cout << " | | \n";
cout << "-----------------------------\n";
cout << " | | \n";
cout << " "
<< p4 << " | "
<< p5 << " | "
<< p6 << " \n";
cout << " | | \n";
cout << "-----------------------------\n";
cout << " | | \n";
cout << " "
<< p7 << " | "
<< p8 << " | "
<< p9 << " \n";
cout << " | | \n";
}

void chooseXandO() // Function definition to choose X and O
{
cout << "Choose X & O\n";
cout << "Enter the name of Player 1 (X's)\n";
cin >> X;

cout << "Enter the name of Player 2 (O's)\n";
cin >> O;
}

void xMove() // Function definition to control X's move
{
cout << " " << X << " move\n";
symbol = 'X';
playerMove(); // Initiates playerMove function
}

void oMove() // Function definition to control O's move
{
cout << " " << O << " move\n";
symbol = 'O';
playerMove(); // Initiates playerMove function
}

void playerMove() // Function definition to control valid moves
{
valid = false;
while (NOT valid)
{
valid = true;
cout << "Enter position 1-9 ";
cin >> pos;
if (pos == '1' && p1 == '1') p1 = symbol;
else if (pos == '2' && p2 == '2') p2 = symbol;
else if (pos == '3' && p3 == '3') p3 = symbol;
else if (pos == '4' && p4 == '4') p4 = symbol;
else if (pos == '5' && p5 == '5') p5 = symbol;
else if (pos == '6' && p6 == '6') p6 = symbol;
else if (pos == '7' && p7 == '7') p7 = symbol;
else if (pos == '8' && p8 == '8') p8 = symbol;
else if (pos == '9' && p9 == '9') p9 = symbol;
else {
valid = false;
cout << "That is not a valid move! Please re-enter\n";
}
}
freeSpaces--; // freeSpaces = freeSpaces - 1;
drawBoard(); // Initiates drawboard function
}

void displayResults() // Function definition to display win/loss/tie results
{
bGameCountX();
bGameCountO();
bGameCountD();
cout << " " << X << " has won " << " " << iGameCountX <<" time(s) and lost "
<< iGameCountO << " time(s).\n";
cout << " " << O << " has won " << " " << iGameCountO <<" time(s) and lost "
<< iGameCountX << " time(s).\n";
cout << "There have been " << iGameCountD << " draw(s).\n";
}

bool gameIsOver() // Function definition for the game being over or not
{
if (p1 == p2 && p2 == p3) return true;
if (p4 == p5 && p5 == p6) return true;
if (p7 == p8 && p8 == p9) return true;
if (p1 == p4 && p4 == p7) return true;
if (p2 == p5 && p5 == p8) return true;
if (p3 == p6 && p6 == p9) return true;
if (p1 == p5 && p5 == p9) return true;
if (p3 == p5 && p5 == p7) return true;
return freeSpaces == 0;
}

void clearBoard() // Function definition to clear the board
{
p1 = '1';
p2 = '2';
p3 = '3';
p4 = '4';
p5 = '5';
p6 = '6';
p7 = '7';
p8 = '8';
p9 = '9';
}

bool bGameCountX() // Function definition for X winning
{
if (p1 == p2 && p2 == p3 || p4 == p5 && p5 == p6 || p7 == p8 && p8 == p9
|| p1 == p4 && p4 == p7 || p2 == p5 && p5 == p8 || p3 == p6 && p6 == p9
|| p1 == p5 && p5 == p9 || p3 == p5 && p5 == p7);
iGameCountX++;
}

bool bGameCountO() // Function definition for O winning
{
if (p1 == p2 && p2 == p3 || p4 == p5 && p5 == p6 || p7 == p8 && p8 == p9
|| p1 == p4 && p4 == p7 || p2 == p5 && p5 == p8 || p3 == p6 && p6 == p9
|| p1 == p5 && p5 == p9 || p3 == p5 && p5 == p7);
iGameCountO++;
}

bool bGameCountD() // Function definition for a Draw
{
if (p1 = p2 && p2 == p3) return true;
if (p4 == p5 && p5 == p6) return true;
if (p7 == p8 && p8 == p9) return true;
if (p1 == p4 && p4 == p7) return true;
if (p2 == p5 && p5 == p8) return true;
if (p3 == p6 && p6 == p9) return true;
if (p1 == p5 && p5 == p9) return true;
if (p3 == p5 && p5 == p7) return true;
}
this looks very complicated for a tic tac toe game. I would suggest two three things:

1.) K.I.S.S.

2.) multidimensional array

3.) generally you shouldn't use non const (#define if you're a C programmer) global vars
Hello, I am a student at UAT and one of my assignments is to provide some help on these forums. Keeping the grid in an array will simplify a lot of your problems. a simple array such as
int grid [3][3];
will do. Without changing your UI too much I would recommend initializing the array so that the value of the array equals the corresponding position.
{1,2,3}
{4,5,6}
{7,8,9}

From there as play commences you would alter the value of a valid move to one of two numbers other than 1-9 that would correspond to X and O. You will have to modify almost all of your code to make the array work, but seeing as that the basic game play mechanics work fine the transfiguration to an array will only make the program run better.
This setup will run much more efficiently and thus allow you to pinpoint the score keeping bugs to work properly.
My last name is McClure!
Last edited on
Topic archived. No new replies allowed.