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