So here is my problem. I have been working on a program that impliments a tic tac toe game, but for some reason my array gets messed up after the switch statement in playerX function. If you could take a look that would be great.
Heres my program:

#include<iostream>
using namespace std;

const int num_rows = 3;
const int num_colm = 3;

void xPlayer(char board[][num_colm], int);

void oPlayer(char board[][num_colm], int);

bool checkBoardX(char board[][num_colm], int);

bool checkBoardO(char board[][num_colm], int);

int main()
{
        int X;
        int O;
        int spots;

        char board[num_rows][num_colm] = {{1,2,3},{4,5,6},{7,8,9}};

        cout << "TIC-TAC-Toe" << endl;
        for(int i=0; i < num_rows; i++)
        {
                for(int j=0; j<num_colm; j++)
                {
                        spots = board[i][j];
                        cout << spots;
                        cout << "   ";
                }
                cout << endl;

        }
        cout << "^Beginning Board^"<<endl;
        do
        {
                xPlayer(board, num_rows);
                for(int k = 0; k< num_rows; k++)
                        if(!checkBoardX(board, num_rows))
                                oPlayer(board, num_rows);
        }
        while (!checkBoardX(board, num_rows)|| !checkBoardO(board,
num_rows));

        if(checkBoardX(board, num_rows))
                cout << "Player X wins" <<endl;
        else
                cout << "Player O wins" <<endl;

        return 0;
}

void xPlayer(char board[][num_colm], int num_rows)
{
        int move;
        int spots;

        for(int i = 0; i < 3; i++)
        {
                for(int j = 0; j < num_rows; j++)
                {
                spots = board[i][j];
                cout << spots;
                cout << "   ";
                }
                cout << endl;

        }
        cout << "Player X, please make your move (1-9): ";
        cin >> move;

        switch (move)
        {
        case 1: board[0][0] = 'X';
                break;
        case 2: board[0][1] = 'X';
                break;
        case 3: board[0][2] = 'X';
                break;
        case 4: board[1][0] = 'X';
                break;
        case 5: board[1][1] = 'X';
                break;
        case 6: board[1][2] = 'X';
                break;
        case 7: board[2][0] = 'X';
                break;
        case 8: board[2][1] = 'X';
                break;
        case 9: board[2][2] = 'X';
                break;
        default: cout << "Your pick was not in the range of 1 - 9 please start over";
        }
}

void oPlayer(char board[][num_colm], int num_rows)
{
        int move;
        int spots;

        for(int i = 0; i < 3; i++)
        {
                for(int j = 0; j < num_rows; j++)
                {
                cout << board[i][j];
                cout << spots;
                cout <<  "   ";
                }
                cout << endl;
        }
        cout << "Player O, please make your move (1-9): ";
        cin >> move;

        switch(move)
        {
        case 1: board[0][0] = 'O';
                break;
        case 2: board[0][1] = 'O';
                break;
        case 3: board[0][2] = 'O';
                break;
        case 4: board[1][0] = 'O';
                break;
        case 5: board[1][1] = 'O';
                break;
        case 6: board[1][2] = 'O';
                break;
        case 7: board[2][0] = 'O';
                break;
        case 8: board[2][1] = 'O';
                break;
        case 9: board[2][2] = 'O';
                break;
        default: cout << "Your pick was not in the range 1 - 9 please start over ";
        }
        return;
}

bool checkBoardX(char board[][num_colm], int num_rows)
{
        bool winner;

        if(board[0][0] == board[0][1] && board[0][2] == board[0][1])
                winner = true;
        else if(board[1][0] == board[1][1] && board[1][2] == board[1][1])
                winner = true;
        else if(board[2][0] == board[2][1] && board[2][2] == board[2][1])
                winner = true;
        else if(board[0][0] == board[1][0] && board[2][0] == board[1][0])
                winner = true;
        else if(board[0][1] == board[1][1] && board[2][1] == board[1][1])
                winner = true;
        else if(board[0][2] == board[1][2] && board[2][2] == board[1][2])
                winner = true;
        else if(board[0][0] == board[1][1] && board[2][2] == board[1][1])
                winner = true;
        else if(board[2][0] == board[1][1] && board[0][2] == board[1][1])
                winner = true;
        else
                winner = false;

        return winner;
}

bool checkBoardO(char board[][num_colm], int num_rows)
{
        bool winner;

        if(board[0][0] == board[0][1] && board[0][2] == board[0][1])
                winner = true;
        else if(board[1][0] == board[1][1] && board[1][2] == board[1][1])
                winner = true;
        else if(board[2][0] == board[2][1] && board[2][2] == board[2][1])
                winner = true;
        else if(board[0][0] == board[1][0] && board[2][0] == board[1][0])
                winner = true;
        else if(board[0][1] == board[1][1] && board[2][1] == board[1][1])
                winner = true;
        else if(board[0][2] == board[1][2] && board[2][2] == board[1][2])
                winner = true;
        else if(board[0][0] == board[1][1] && board[2][2] == board[1][1])
                winner = true;
        else if(board[2][0] == board[1][1] && board[0][2] == board[1][1])
                winner = true;
        else
                winner = false;

        return winner;
}
TIC-TAC-Toe
1   2   3
4   5   6
7   8   9
^Beginning Board^
1   2   3
4   5   6
7   8   9
Player X, please make your move (1-9): 2
9   X9   9
9   9   9
9  9    9
Player O, please make your move (1-9): 3
9   X9   O9
9   9   9
9  9    9
Player O, please make your move (1-9): PuTTY

Recommended Answers

All 7 Replies

You can completely omit the need to place your chars into ints for display by simply adding 48 to all of your chars in your 2D char array--

int X;
        int O;

        char board[num_rows][num_colm] = {{1,2,3},{4,5,6},{7,8,9}};

        cout << "TIC-TAC-Toe" << endl;
        for(int i=0; i < num_rows; i++)
        {
                for(int j=0; j<num_colm; j++)
                {
                        board[i][j] += 48;
                        cout << board[i][j];
                        cout << "   ";
                }
                cout << endl;

        }

-- now you have no need for any occurrence of "spots" and can simply output your [j] character(s).

First thing I'd do is create a function that simply outputs a board and call that instead of hard coding it in other functions.

Then, add output statements to follow your program (or use the debugger) to see where the problem shows up.

That didnt help my problem though because when I do that the next board that is outputed is:
TIC-TAC-Toe
1 2 3
4 5 6
7 8 9
^Beginning Board^
49 50 51
52 53 54
55 56 57
Player X, please make your move (1-9): 1
X57 257 357
457 557 657
757 857 957
Player O, please make your move (1-9): 2
X57 O57 357
457 557 657
757 857 957
Player O, please make your move (1-9): ^C

I said omit all occurrences of spots, not just the one in main.

I was tinkering around with your program some--

#include<iostream>
using namespace std;

const int num_rows = 3;
const int num_colm = 3;
int inputCount = 0;

bool taken[9] = {false, false, false, false, false, false, false, false, false};

void xPlayer(char board[][num_colm], int);

void oPlayer(char board[][num_colm], int);

bool checkBoardX(char board[][num_colm], int);

bool checkBoardO(char board[][num_colm], int);



int main()
{
        int X;
        int O;

        char board[num_rows][num_colm] = {{1,2,3},{4,5,6},{7,8,9}};

        cout << "TIC-TAC-Toe" << endl;
        for(int i=0; i < num_rows; i++)
        {
                for(int j=0; j<num_colm; j++)
                {
                        board[i][j] += 48;
                        cout << board[i][j];
                        cout << "   ";
                }
                cout << endl;

        }
        cout << "^Beginning Board^"<<endl;

		while(true){

			if(!checkBoardO(board, num_rows) && inputCount < num_rows * num_colm)
				xPlayer(board, num_rows);
			else break;
			if(!checkBoardX(board, num_rows) && inputCount < num_rows * num_colm)
				oPlayer(board, num_rows);
			else break;
		}

        if(checkBoardX(board, num_rows))
                cout << "Player X wins" <<endl;
        else if(checkBoardO(board, num_rows))
                cout << "Player O wins" <<endl;
		else cout << "Neither player won!" << endl;

		cin.ignore();
		cin.get();
        return 0;
}

void xPlayer(char board[][num_colm], int num_rows)
{
        int move;

        for(int i = 0; i < 3; i++)
        {
                for(int j = 0; j < num_rows; j++)
                {
                cout << board[i][j];
                cout << "   ";
                }
                cout << endl;

        }

		bool validMove;

		do{
			validMove = true;
			cout << "Player X, please make your move (1-9): ";
			cin >> move;

			validMove = (move > 0 && move < 10);
			
			if(!validMove){
				cout << "Your pick was not in the range 1 - 9 please start over " << endl;
			}else if(taken[move - 1] == true){
				validMove = false;
				cout << "Position " << move << " is already taken. Please try another position." << endl;
			}else{
				inputCount++;
				taken[move - 1] = true;
			}

		}while(!validMove);

		board[(move-1)/3][(move-1)%3] = 'X';
}

void oPlayer(char board[][num_colm], int num_rows)
{
        int move;

        for(int i = 0; i < 3; i++)
        {
                for(int j = 0; j < num_rows; j++)
                {
                cout << board[i][j];
                cout <<  "   ";
                }
                cout << endl;
        }
		bool validMove;

		do{
			validMove = true;
			cout << "Player O, please make your move (1-9): ";
			cin >> move;

			validMove = (move > 0 && move < 10);
			
			if(!validMove){
				cout << "Your pick was not in the range 1 - 9 please start over " << endl;
			}else if(taken[move - 1] == true){
				validMove = false;
				cout << "Position " << move << " is already taken. Please try another position." << endl;
			}else{
				inputCount++;
				taken[move - 1] = true;
			}

		}while(!validMove);

		board[(move-1)/3][(move-1)%3] = 'O';
}

bool checkBoardX(char board[][num_colm], int num_rows)
{
        bool winner;

        if(board[0][0] == 'X' && board[0][0] == board[0][1] && board[0][2] == board[0][1])
                winner = true;
        else if(board[1][0] == 'X' && board[1][0] == board[1][1] && board[1][2] == board[1][1])
                winner = true;
        else if(board[2][0] == 'X' && board[2][0] == board[2][1] && board[2][2] == board[2][1])
                winner = true;
        else if(board[0][0] == 'X' && board[0][0] == board[1][0] && board[2][0] == board[1][0])
                winner = true;
        else if(board[0][1] == 'X' && board[0][1] == board[1][1] && board[2][1] == board[1][1])
                winner = true;
        else if(board[0][2] == 'X' && board[0][2] == board[1][2] && board[2][2] == board[1][2])
                winner = true;
        else if(board[0][0] == 'X' && board[0][0] == board[1][1] && board[2][2] == board[1][1])
                winner = true;
        else if(board[2][0] == 'X' && board[2][0] == board[1][1] && board[0][2] == board[1][1])
                winner = true;
        else
                winner = false;

        return winner;
}

bool checkBoardO(char board[][num_colm], int num_rows)
{
        bool winner;

        if(board[0][0] == 'O' && board[0][0] == board[0][1] && board[0][2] == board[0][1])
                winner = true;
        else if(board[1][0] == 'O' && board[1][0] == board[1][1] && board[1][2] == board[1][1])
                winner = true;
        else if(board[2][0] == 'O' && board[2][0] == board[2][1] && board[2][2] == board[2][1])
                winner = true;
        else if(board[0][0] == 'O' && board[0][0] == board[1][0] && board[2][0] == board[1][0])
                winner = true;
        else if(board[0][1] == 'O' && board[0][1] == board[1][1] && board[2][1] == board[1][1])
                winner = true;
        else if(board[0][2] == 'O' && board[0][2] == board[1][2] && board[2][2] == board[1][2])
                winner = true;
        else if(board[0][0] == 'O' && board[0][0] == board[1][1] && board[2][2] == board[1][1])
                winner = true;
        else if(board[2][0] == 'O' && board[2][0] == board[1][1] && board[0][2] == board[1][1])
                winner = true;
        else
                winner = false;

        return winner;
}

-- notice how there are no conversions from char to int because there isn't any need! You assigned the chars in the 2D array the bit-values of 1-through-9 but the ASCII chart characters '0'-through-'9' start at '0' with bit value 48. That's what I was getting at.

http://www.commfront.com/Images/Standard-ASCII-Table.jpg

commented: Good job! Now he can cheat his way to an A -3

Thank you very much INTRADE.

Intrade, this is why we don't tinker with people's code and post the answer. You just finished the program for him and he passes on your work, not his.

Intrade, this is why we don't tinker with people's code and post the answer. You just finished the program for him and he passes on your work, not his.

My apologies. I just got done reading Narue's post http://www.daniweb.com/forums/thread78223.html

Next time I'll just point out the problem(s).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.