The assignment is to make a two player tic-tac-toe game. Here's what I have so far:
Code:
char board[9] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}, choice;
int index=0, player = 1, result;
bool found = false;
do{
if (player == 1){
do{
cout << "Player one make your move.\n";
cin >> choice;
cout << "\n";
index = 0;
while ((!found) && (index < 9)){
if (choice == board[index]){
found = true;
board[index] = 'X';
}
else
index++;
}
if(!found)
cout << "Illegal move.\n\n";
}while(!found);
index = 0;
if (board[index]==board[index+1] && board[index+1]==board[index+2])
result = 1;
else if(board[index+3]==board[index+4] && board[index+4]==board[index+5])
result = 1;
else if(board[index+6]==board[index+7] && board[index+7]==board[index+8])
result = 1;
else if(board[index]==board[index+3] && board[index+3]==board[index+6])
result = 1;
else if(board[index+1]==board[index+4] && board[index+4]==board[index+7])
result = 1;
else if(board[index+2]==board[index+5] && board[index+5]==board[index+8])
result = 1;
else if(board[index]==board[index+4] && board[index+4]==board[index+8])
result = 1;
else if(board[index+6]==board[index+4] && board[index+4]==board[index+2])
result = 1;
else
result = -1;
if (result == 1)
cout << "Player one wins!\n";
for (index = 0; index < 9; index++){
cout << board[index] << ' ';
if (index == 2 || index == 5 || index == 8)
cout << "\n";
}
cout << "\n\n";
player++;
}
else if (player == 2){
do{
cout << "Player two make your move.\n";
cin >> choice;
cout << "\n";
index = 0;
while ((!found) && (index < 9)){
if (choice == board[index]){
found = true;
board[index] = 'O';
}
else
index++;
}
if(!found)
cout << "Illegal move.\n\n";
}while(!found);
index = 0;
if (board[index]==board[index+1] && board[index+1]==board[index+2])
result = 1;
else if(board[index+3]==board[index+4] && board[index+4]==board[index+5])
result = 1;
else if(board[index+6]==board[index+7] && board[index+7]==board[index+8])
result = 1;
else if(board[index]==board[index+3] && board[index+3]==board[index+6])
result = 1;
else if(board[index+1]==board[index+4] && board[index+4]==board[index+7])
result = 1;
else if(board[index+2]==board[index+5] && board[index+5]==board[index+8])
result = 1;
else if(board[index]==board[index+4] && board[index+4]==board[index+8])
result = 1;
else if(board[index+6]==board[index+4] && board[index+4]==board[index+2])
result = 1;
else
result = -1;
if (result == 1)
cout << "Player two wins!\n";
for (index = 0; index < 9; index++){
cout << board[index] << ' ';
if (index == 2 || index == 5 || index == 8)
cout << "\n";
}
cout << "\n\n";
player--;
}
}while (result != 1);
When I test it, it works the first time. But after that it doesn't. But I can't figure out why.
Here's the out put:
Code:
Welcome to Tic-Tac-Toe!
Player 1 is 'X' and Player 2 is 'O'.
a b c
d e f
g h i
Choose a spot by entering the corresponding letter.
Player one make your move.
e
a b c
d X f
g h i
Player two make your move.
a
a b c
d X f
g h i
Player one make your move.
i
a b c
d X f
g h i
Player two make your move.
e
a b c
d X f
g h i
Player one make your move.
Any help would be appreciated.