| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hello Everyone, I seem to have a problem everytime i compile my code.
It displays ISO forbids comparison between pointer and integer. This happens from Line 70 on. I dont see how its comparing it. (well i am a C++ noob) Any help or solutions will be appreciated. To compile my code im using Dev C++ Thanks Code:
#include<iostream>
#include<vector>
#include<string>
using namespace std;
//global constants
char board[3][3];
const int TURN = 9;
int MOVE = 0;
//functions
void instructions();
void displayBoard();
char PLAYER_TURN();
char CPU_TURN();
void checkWinner();
char CPU_TURN(); // Human player for now
int main()
{
instructions();
while (TURN < 10)
{
if(TURN == 0)
displayBoard();
PLAYER_TURN();
checkWinner();
displayBoard();
CPU_TURN();
checkWinner();
}
return 0;
}
void instructions()
{
cout << "Tic-Tac-Toe";
cout << "The rules are simple, try to outwit your opponent by getting three.\n";
cout << "In a row before he does.\n";
cout << "The board looks something like this...";
cout << " 0 | 1 | 2\n";
cout << " ---------\n";
cout << " 3 | 4 | 5\n";
cout << " ---------\n";
cout << " 6 | 7 | 8\n\n";
}
void displayBoard()
{
cout << board[0] << " | " << board[1] << " | " << board[2];
cout << "---------";
cout << board[3] << " | " << board[4] << " | " << board[5];
cout << "---------";
cout << board[6] << " | " << board[7] << " | " << board[8];
cout << "\n\n";
}
char PLAYER_TURN()
{
cout << "select a number from 0-8.";
cin >> MOVE;
for(MOVE > 9 || MOVE < 1 || ('X' == board[MOVE] || 'O' == board[MOVE]);)
{
cout << "You cannot make that move, please try again: ";
cin >> MOVE;
}
board[MOVE] = 'X';
TURN++;
MOVE++;
}
char CPU_TURN() // this is a test right now CPU is actually a second player...
{
cout << "select a number from 0-8.";
cin >> MOVE;
for(MOVE > 9 || MOVE < 1 ('X' == board[MOVE] || 'O' == board[MOVE]);)
{
cout << "You cannot make that move, please try again: ";
cin >> MOVE;
}
board[MOVE] = 'O';
TURN++;
MOVE++;
}
void checkWinner()
{
while(TURN < 10)
{
// Check for Player 1...
// diagonal \/
if (board[0] && board[4] && board[8]== 'X') {cout<<"Player 1 is the winner";break;}
if (board[6] && board[4] && board[2]== 'X') {cout<<"Player 1 is the winner";break;}
// vertical |
if (board[0] && board[3] && board[6]== 'X') {cout<<"Player 1 is the winner";break;}
if (board[1] && board[4] && board[7]== 'X') {cout<<"Player 1 is the winner";break;}
if (board[2] && board[5] && board[8]== 'X') {cout<<"Player 1 is the winner";break;}
// horizontal ---
if (board[0] && board[1] && board[2]== 'X') {cout<<"Player 1 is the winner";break;}
if (board[3] && board[4] && board[5]== 'X') {cout<<"Player 1 is the winner";break;}
if (board[6] && board[7] && board[8]== 'X') {cout<<"Player 1 is the winner";break;}
// Check for Player 2...
// diagonal \/
if (board[0] && board[4] && board[8]== 'O') {cout<<"Player 2 is the winner";break;}
if (board[6] && board[4] && board[2]== 'O') {cout<<"Player 2 is the winner";break;}
// vertical |
if (board[0] && board[3] && board[6]== 'O') {cout<<"Player 2 is the winner";break;}
if (board[1] && board[4] && board[7]== 'O') {cout<<"Player 2 is the winner";break;}
if (board[2] && board[5] && board[8]== 'O') {cout<<"Player 2 is the winner";break;}
// horizontal ---
if (board[0] && board[1] && board[2]== 'O') {cout<<"Player 2 is the winner";break;}
if (board[3] && board[4] && board[5]== 'O') {cout<<"Player 2 is the winner";break;}
if (board[6] && board[7] && board[8]== 'O') {cout<<"Player 2 is the winner";break;}
// After 9 MOVES there is no winner...
if (MOVE == 9){cout<<"The game is over and no one wins.";break;}
}
}
|
|
#2
|
||||
|
||||
|
Seems to me this line doesn't make any sense:
Code:
for(MOVE > 9 || MOVE < 1 ('X' == board[MOVE] || 'O' == board[MOVE]);)
I suggest changing it to something like this: Code:
while(MOVE > 8 || MOVE < 0 || 'X' == board[MOVE] || 'O' == board[MOVE]) remember, if you tell your user to input a number from 0 to 8, don't check if it's smaller than 1 or greater than 9. :-) ) the same goes for the earlier Code:
for(MOVE > 9 || MOVE < 1 || ('X' == board[MOVE] || 'O' == board[MOVE]);)
Also, what's the point of this line: Code:
MOVE++; It's in there twice, and both times you read a new value into that variable right after Some more notes: The CPU_TURN and PLAYER turn are defined to return a char value, but neither of them does. Furthermore, you have defined TURN as a const, and initiated it at 9, so it won't be incremented (const) and even if it was, you'd only have one move before the game ends. Also, you seem to mix up TURN and MOVE a lot a line like this Code:
if (MOVE == 9){cout<<"The game is over and no one wins.";break;}
makes no sense, as MOVE is the variable you use to read the user input. I suspect you meant Code:
if (TURN == 9){cout<<"The game is over and no one wins.";break;}
Then finally, last but definitely not least: this doesn't work: Code:
if (board[0] && board[4] && board[8]== 'X') you can't compare that way, you have to write it out completely, like this: Code:
if (board[0] == 'X' && board[4] == 'X' && board[8] == 'X') I hope all this helps...
__________________
This is my code. Is it not nifty? "The biggest problem encountered while trying to design a system that was completely foolproof, was, that people tended to underestimate the ingenuity of complete fools." ---Douglas Adams Join the Itsacon fanclub! Zero Tolerance: Spammers banned so far: 280
![]() Last edited by Itsacon : April 24th, 2005 at 04:08 AM. |
|
#3
|
|||
|
|||
|
TURN declared as a constant integer, you can not increment it.
Like 9++ is illegal, so is TURN++. The statement Code:
if(board[0]&&board[4]&&board[8]== 'X') is correct except that condition won't be met properly.Since, each individual array element is true in itself. |
|
#4
|
|||
|
|||
|
Hey thanks guys, I still seem to get that comparison and integer error, its probably due to board[3][3] in some way or another. Anyway thanx for the help
|
|
#5
|
|||
|
|||
|
It is because board is a double pointer.
the statement board[MOVE] => (board + MOVE) , it is still a pointer, in order to access it content you need to do some thing like this : board[MOVE][0] == 'X' |
|
#6
|
|||
|
|||
|
It is because board is a double pointer.
the statement board[MOVE] => (board + MOVE) , it is still a pointer, in order to access it content you need to do some thing like this : board[MOVE][0] == 'X' But there are other flaws as pointed above,particularly, TURN ++ operation is wrong. A constant , once given value can not be changed. |
|
#7
|
||||
|
||||
|
Or simply declare board as
Code:
char board[9]; |
|
#8
|
|||
|
|||
|
Now that I made all the corrections i return 0 compile errors. Once again i thank everyone for your help and i hope to see you again.
|
|
#9
|
||||
|
||||
|
We aim to please (and we've got good targeting systems :-) )
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Help with Tic-Tac-Toe |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|