Nov 29, 2012 at 10:42pm UTC
the my program can work result not correct. i think some wrong about displayBoard(gameBoard). please help me solve it. Thank you so much. THIS IS MY CODE
#include<iostream>
using namespace std;
const int row = 3;
const int colm = 3;
const char X = 'X';
const char O = 'O';
int inputCount = 0;
bool taken[9] = {false, false, false, false, false, false, false, false, false};
void displayBroad(char gameBoard[][colm]);
void playerTurn_X(char gameBoard[][colm], int);
void playerTurn_O(char gameBoard[][colm], int);
bool gameOver_X(char gameBoard[][colm], int);
bool gameOver_O(char gameBoard[][colm], int);
int main()
{
char gameBoard[row][colm] = {{1,2,3},{4,5,6},{7,8,9}};
cout << "WELCOM TO TIC-TAC-TOE GAME" << endl;
displayBroad(gameBoard);
cout << "Beginning Board"<<endl;
while(true){
if(!gameOver_O(gameBoard, row) && inputCount < row * colm)
playerTurn_X(gameBoard, row);
else if(!gameOver_X(gameBoard, row) && inputCount < row * colm)
playerTurn_O(gameBoard, row);
}
if(gameOver_X(gameBoard, row))
cout << "Player X wins" <<endl;
else if(gameOver_O(gameBoard, row))
cout << "Player O wins" <<endl;
else cout << "Neither player won!" << endl;
cin.ignore();
cin.get();
return 0;
}
void displayBroad(char gameBoard[][colm])
{
for(int i=0; i < row; i++)
{
for(int j=0; j<colm; j++)
{
gameBoard[i][j] ;
cout << gameBoard[i][j];
cout << " ";
}
cout << endl;
}
}
void playerTurn_X(char gameBoard[][colm], int row)
{
int move;
displayBroad(gameBoard);
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);
gameBoard[(move-1)/3][(move-1)%3] = 'X';
}
void playerTurn_O(char gameBoard[][colm], int row)
{
int move;
displayBroad(gameBoard);
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);
gameBoard[(move-1)/3][(move-1)%3] = 'O';
}
bool gameOver_X(char gameBoard[][colm], int row)
{
bool winner;
if(gameBoard[0][0] == 'X' && gameBoard[0][0] == gameBoard[0][1] && gameBoard[0][2] == gameBoard[0][1])
winner = true;
else if(gameBoard[1][0] == 'X' && gameBoard[1][0] == gameBoard[1][1] && gameBoard[1][2] == gameBoard[1][1])
winner = true;
else if(gameBoard[2][0] == 'X' && gameBoard[2][0] == gameBoard[2][1] && gameBoard[2][2] == gameBoard[2][1])
winner = true;
else if(gameBoard[0][0] == 'X' && gameBoard[0][0] == gameBoard[1][0] && gameBoard[2][0] == gameBoard[1][0])
winner = true;
else if(gameBoard[0][1] == 'X' && gameBoard[0][1] == gameBoard[1][1] && gameBoard[2][1] == gameBoard[1][1])
winner = true;
else if(gameBoard[0][2] == 'X' && gameBoard[0][2] == gameBoard[1][2] && gameBoard[2][2] == gameBoard[1][2])
winner = true;
else if(gameBoard[0][0] == 'X' && gameBoard[0][0] == gameBoard[1][1] && gameBoard[2][2] == gameBoard[1][1])
winner = true;
else if(gameBoard[2][0] == 'X' && gameBoard[2][0] == gameBoard[1][1] && gameBoard[0][2] == gameBoard[1][1])
winner = true;
else
winner = false;
return winner;
}
bool gameOver_O(char gameBoard[][colm], int row)
{
bool winner;
if(gameBoard[0][0] == 'O' && gameBoard[0][0] == gameBoard[0][1] && gameBoard[0][2] == gameBoard[0][1])
winner = true;
else if(gameBoard[1][0] == 'O' && gameBoard[1][0] == gameBoard[1][1] && gameBoard[1][2] == gameBoard[1][1])
winner = true;
else if(gameBoard[2][0] == 'O' && gameBoard[2][0] == gameBoard[2][1] && gameBoard[2][2] == gameBoard[2][1])
winner = true;
else if(gameBoard[0][0] == 'O' && gameBoard[0][0] == gameBoard[1][0] && gameBoard[2][0] == gameBoard[1][0])
winner = true;
else if(gameBoard[0][1] == 'O' && gameBoard[0][1] == gameBoard[1][1] && gameBoard[2][1] == gameBoard[1][1])
winner = true;
else if(gameBoard[0][2] == 'O' && gameBoard[0][2] == gameBoard[1][2] && gameBoard[2][2] == gameBoard[1][2])
winner = true;
else if(gameBoard[0][0] == 'O' && gameBoard[0][0] == gameBoard[1][1] && gameBoard[2][2] == gameBoard[1][1])
winner = true;
else if(gameBoard[2][0] == 'O' && gameBoard[2][0] == gameBoard[1][1] && gameBoard[0][2] == gameBoard[1][1])
winner = true;
else
winner = false;
return winner;
}
Last edited on Nov 29, 2012 at 10:45pm UTC