C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 23rd, 2005, 10:06 PM
DarkDove DarkDove is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 3 DarkDove User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 24 m 34 sec
Reputation Power: 0
Lightbulb Help with Tic-Tac-Toe

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;}
	}	
}

Reply With Quote
  #2  
Old April 24th, 2005, 03:50 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 1,001 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3
Time spent in forums: 6 Days 15 h 6 m 59 sec
Reputation Power: 6
Send a message via ICQ to Itsacon
Seems to me this line doesn't make any sense:

Code:
for(MOVE > 9 || MOVE < 1 ('X' == board[MOVE] || 'O' == board[MOVE]);)
(line 74 or thereabouts)

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]);)
(line 59 or so) Though this one IS correct (you didn't forget the 'OR' operator in the middle), it's still unwieldy to use a 'for' there. This is what 'while's are for.

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.

Reply With Quote
  #3  
Old April 24th, 2005, 04:16 AM
Cirus Cirus is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 276 Cirus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 48 m 58 sec
Reputation Power: 4
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. 

Reply With Quote
  #4  
Old April 24th, 2005, 12:55 PM
DarkDove DarkDove is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 3 DarkDove User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 24 m 34 sec
Reputation Power: 0
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

Reply With Quote
  #5  
Old April 24th, 2005, 03:06 PM
Cirus Cirus is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 276 Cirus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 48 m 58 sec
Reputation Power: 4
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'

Reply With Quote
  #6  
Old April 24th, 2005, 03:07 PM
Cirus Cirus is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 276 Cirus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 48 m 58 sec
Reputation Power: 4
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.

Reply With Quote
  #7  
Old April 24th, 2005, 03:32 PM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 1,001 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3
Time spent in forums: 6 Days 15 h 6 m 59 sec
Reputation Power: 6
Send a message via ICQ to Itsacon
Or simply declare board as
Code:
char board[9];

Reply With Quote
  #8  
Old April 24th, 2005, 03:59 PM
DarkDove DarkDove is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 3 DarkDove User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 24 m 34 sec
Reputation Power: 0
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.

Reply With Quote
  #9  
Old April 25th, 2005, 01:31 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 1,001 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3
Time spent in forums: 6 Days 15 h 6 m 59 sec
Reputation Power: 6
Send a message via ICQ to Itsacon
We aim to please (and we've got good targeting systems :-) )

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Help with Tic-Tac-Toe


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT