I am having trouble replacing the numbers on my Tic Tac board with the X and O from my if and else if statements. I would also like to know how to make sure how to keep the X and O in place without it being replaced after every loop. Thank You! Any hint will be appreciated.

/**
 
 
 1. Implement displayBoard to display Tic Tac Toe board.
 2. Prompt User for a box on the board to select, i.e. a number between 1 and 9 with 1 being the upper left corner.
 
 use cin.get(box) to get the box number and isdigit to verify it is a
 number;
 1 | 2 | 3
 4 | 5 | 6
 7 | 8 | 9
 If the box is available put the appropriate X or O in there and switch players, i.e. X becomes O and vice versa.
 If the box is NOT available warn the user and get another box until they select a valid open box.
 
 3. After all spots have been select Display "Game Over!";
 4. Write a main function to use the TicTacToe class and test all of the above functionality.
 
 **/


#include<iostream>
#include<limits>

using namespace std;


class TicTacToe {
public:
	void displayBoard();
	void getMove();
	void playGame();
private:
	char board[9];
	char player; // Switch after each move.
};

int main ()
{
	TicTacToe ttt;
        
    
    for (int i = 0; i < 9; i++) {
        ttt.playGame();

    }
    
    
    
}

void TicTacToe::playGame()
{
    
    displayBoard();
    
    
    getMove();    
    
	// Your implementation here...
    
    
    
    
}

void TicTacToe::displayBoard()
{
	// Your implementation here...
    
    board[0] = '1';
    board[1] = '2';
    board[2] = '3';
    board[3] = '4';
    board[4] = '5';
    board[5] = '6';
    board[6] = '7';
    board[7] = '8';
    board[8] = '9';

    
       
    for (int i = 0; i < 9; i++) 
    {
        
        
        if ( (i+1) % 3 == 0 )
        {
            cout << board[i] << endl;
        }
        else 
        {
            
            cout << board[i] <<	" | ";
        }
        
        
    }
    
}

void TicTacToe::getMove()
{
    
    if (player == 'X'){ 
        player = 'O';
    }
    else {
        player = 'X';
    }
    
    cout << player << " ";
	cout << "Enter Box: ";
	char c;
	
    
    bool move;
    move = true;
    
        cin.get(c);
        cin.ignore(numeric_limits<int>::max(), '\n');
        if (c > '9' || c < '0')
            // error message
            cout << "please enter a number 1-9" << endl;
        
        int number = c - '0';
        
        cout << "your number is " << number << endl;
        // Your implementation here...
    
    for (int i = 0; i < 9; i++) {
        if (board[i] == 'X' || board[i] == 'O') {
            cout << "Space has been taken" << endl;
        }
    }
    
    
        
        if (c == '1' && board[0] == '1') {
            board[0] = player;
            
        }
        else if(c == '2' && board[1] == '2')
        {
            board[1] = player;
            
            
        }
        else if(c == '3' && board[2] == '3')
        {
            board[2] = player;
            
            
            
        }
        else if(c == '4' && board[3] == '4')
        {
            
            board[3] = player;
            
            
        }
        else if(c == '5' && board[4] == '5')
        {
            board[4] = player;
            
            
            
        }
        else if(c == '6' && board[5] == '6')
        {
            board[5] = player;
            
            
            
        }
        else if(c == '7' && board[6] == '7')
        {
            board[6] = player;
            
            
            
        }
        else if(c == '8' && board[7] == '8')
        {
            board[7] = player;
            
            
        }
        else if(c == '9' && board[8] == '9')
        {
            board[8] = player;
            
            
        }
        
    
    
    
    
    
    
    
}

Edited 5 Years Ago by Nick Evan: Changed ICODE to CODE tags. ICODE is for 1 line of code, CODE is for blocks of code

board[i] should hold either 1-9 or an X or an O. But in your void TicTacToe::displayBoard() you reset the values of board[] to their original values.

The "initialization" of your game variables should come in a constructor. That's where you could set the values 1-9 in your board array. Then void TicTacToe::displayBoard() would just output whatever board array holds.

Edited 5 Years Ago by akhena: n/a

Here is how you could initialize and draw the board.

TicTacToe::TicTacToe()
{
	player = 'O';
	for( int i = 0; i < 9; i++ )
		board[i] = i+'1';
}

void TicTacToe::displayBoard()
{
	// Your implementation here...
	for (int i = 0; i < 9; i++)
		if( (i+1) % 3 == 0 )
			cout << board[i] << endl;
		else
			cout << board[i] << " | ";
}

You will need to add the prototype TicTacToe(); within the class also.


There are many other things with this program that are either useless or will not work as intended.

In main() you use a for() loop to run the game but if I were to enter something invalid 9 times in a row the game would end since it runs exactly 9 times.

One way you could fix this is by using a while() loop and having a variable called winner or something that holds values of 0, 1, 2 or 3 (0 means no winner, 1 or 2 is the winner and 3 is draw).

Also when the player gives bad input it switches to the other players turn so you have to come up with a way to get their input again without breaking out of the function (hint: use a do/while loop).

Since you are doing error checking before placing the piece on the board you can replace all the if/else ifs with board[c-'1'] = player; and it will do the exact same thing (you have to make it so if the player gives bad input this will not run until good input is given)

You didn't do this # use cin.get(box) to get the box number and isdigit to verify it is a number; And you will probably need somewhere in your TicTacToe class a function checking if the game is over. There are 3 different cases : player X won, player Y won or it's a tie. Having such a function would be helpful, as you could write something like

class TicTacToe
{
public:
    bool gameOver() // Returns true when game is over. Could maybe set some private variable with who the winner is or if it's a tie.
    //Rest of class definition
    // [...]
};
int main(){
    while(!ttt.gameOver()) // While the game has not ended
    {
        ttt.playGame();
    }
}
This article has been dead for over six months. Start a new discussion instead.