Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

MTT

Help with tictactoe

This topic is 5649 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok i got this tic tac toe code done but i cant get the AI function to return choice, So if sombody can please point out to me where i went wrong. It looks like alot of code but not much probably needs to be read, its in the ai function possibly in my definition, declaration, or the call i make to it. And its pretty repetitive. Im hopeing theat its just a small thing that will seem odvious once sombody says it but here is the code anyhow.
  
 using namespace std;

extern int choice;

//Function declarations


void drawboard ();	//draws out the tictactoe board

void pchoice ();	//gets the players choice

void compchoice ();	//gets the computers choice

void wincheck ();	//checks if a player has won	

int AI (int choice);//finds which sqare the comp chooses

   

  
//Defining functions and globals

#include <iostream>
#include <time.h>
#include "TicTacToe.h"
using namespace std;

//Globals


int player = 1;			//which player is choosing

int choice = 0;			//which sqare they chose

int randnum = 0;		//for finding rands

int max = 0;			//max for rands


char xoboard [9] = {'1', '2', '3', 
					'4', '5', '6',	//contents of the board

					'7', '8', '9'};

int sqaremark [9] = { 0, 0, 0,
					  0, 0, 0,		//puts marker on chosen sqares 

					  0, 0, 0, };	//for wincheck


                        



/*********************DRAWBOARD********************/

void drawboard ()	//makes drawing of board on the screen
{
	cout << endl << endl;
	cout << " " << xoboard [0] << " | " << xoboard [1] << " | " << xoboard [2] << " " << endl;
	cout << "---+---+---" << endl;
	cout << " " << xoboard [3] << " | " << xoboard [4] << " | " << xoboard [5] << " " << endl;
	cout << "---+---+---" << endl;
	cout << " " << xoboard [6] << " | " << xoboard [7] << " | " << xoboard [8] << " " << endl;
}



/*********************PCHOICE********************/

void pchoice ()		//finds which sqare is chosen and switches the player
{
psqarechoice:
	//for player
	cout << "Pick a sqare: ";
	cin >> choice;

	if (((choice < 1) || (choice > 9)) || (sqaremark [choice - 1] != 0))//colision detection
	{
		cout << endl << "That sqare is not accesable" << endl;
		goto psqarechoice;	//return back to sqare choosing
	}
	
	else	//if sqare is not allready chosen
	{
		sqaremark [choice - 1] = 1;	//marks a 1 on sqaremark
		xoboard [choice - 1] = 'O';	//drawes 'O' in chosen srare and makes player = 2
		player = 2;
	}
}



/*********************COMPCHOICE********************/

void compchoice ()
{
compsqarechoice:
	cout << AI (choice);

	if (((choice < 1) || (choice > 9)) || (sqaremark [choice - 1] != 0))//colision detection
	{
		goto compsqarechoice;	//return back to sqare choosing
	}

	else
	{
		sqaremark [choice - 1] = 2;	//marks a 2 on sqaremark
		xoboard [choice - 1] = 'X';	//drawes 'X' in chosen srare and makes player = 1
		player = 1;
	}
}


/*********************WINCHECK********************/

void wincheck ()
{
	if ( //rows
	     (sqaremark [0] == 1 && sqaremark [1] == 1 && sqaremark [2] == 1) ||
	     (sqaremark [3] == 1 && sqaremark [4] == 1 && sqaremark [5] == 1) ||
		 (sqaremark [6] == 1 && sqaremark [7] == 1 && sqaremark [8] == 1) ||
		 //coloms
		 (sqaremark [0] == 1 && sqaremark [3] == 1 && sqaremark [6] == 1) || 
		 (sqaremark [1] == 1 && sqaremark [4] == 1 && sqaremark [7] == 1) || 
		 (sqaremark [2] == 1 && sqaremark [5] == 1 && sqaremark [8] == 1) ||
		 //diagonals
		 (sqaremark [0] == 1 && sqaremark [4] == 1 && sqaremark [8] == 1) || 
		 (sqaremark [6] == 1 && sqaremark [4] == 1 && sqaremark [2] == 1)
	   )
	
	{
		cout << "'O' is the winner" << endl << endl; //displays if player 1 has 3 in a row
	}


	else if ( //rows
			  (sqaremark [0] == 2 && sqaremark [1] == 2 && sqaremark [2] == 2) ||
			  (sqaremark [3] == 2 && sqaremark [4] == 2 && sqaremark [5] == 2) ||
			  (sqaremark [6] == 2 && sqaremark [7] == 2 && sqaremark [8] == 2) ||
			  //coloms
			  (sqaremark [0] == 2 && sqaremark [3] == 2 && sqaremark [6] == 2) || 
			  (sqaremark [1] == 2 && sqaremark [4] == 2 && sqaremark [7] == 2) || 
			  (sqaremark [2] == 2 && sqaremark [5] == 2 && sqaremark [8] == 2) ||
			  //diagonals
			  (sqaremark [0] == 2 && sqaremark [4] == 2 && sqaremark [8] == 2) || 
			  (sqaremark [6] == 2 && sqaremark [4] == 2 && sqaremark [2] == 2)
			)
	{
		cout << "'X' is the winner" << endl << endl; //displayes if player 2 has 3 in a row
	}
}



/*********************AI********************/

int AI (int choice)	//finds the sqare the computer chooses

{
	//For the win

	//across

	//line 1

	if ( sqaremark [2] == 1 && sqaremark [1] == 1 && sqaremark [0] == 0)
	{	choice = 1, return choice;	}
	if ( sqaremark [2] == 1 && sqaremark [0] == 1 && sqaremark [1] == 0)
	{	choice = 2, return choice;	}
	if ( sqaremark [1] == 1 && sqaremark [0] == 1 && sqaremark [2] == 0)
	{	choice = 3, return choice;	}
	//line 2

	if ( sqaremark [5] == 1 && sqaremark [4] == 1 && sqaremark [3] == 0)
	{	choice = 1, return choice;	}
	if ( sqaremark [5] == 1 && sqaremark [3] == 1 && sqaremark [4] == 0)
	{	choice = 2, return choice;	}
	if ( sqaremark [4] == 1 && sqaremark [3] == 1 && sqaremark [5] == 0)
	{	choice = 3, return choice;	}
	//line 3

	if ( sqaremark [8] == 1 && sqaremark [7] == 1 && sqaremark [6] == 0)
	{	choice = 1, return choice;	}
	if ( sqaremark [8] == 1 && sqaremark [6] == 1 && sqaremark [7] == 0)
	{	choice = 2, return choice;	}
	if ( sqaremark [7] == 1 && sqaremark [6] == 1 && sqaremark [8] == 0)
	{	choice = 3, return choice;	}
	//down

	//line 1

	if ( sqaremark [6] == 1 && sqaremark [3] == 1 && sqaremark [0] == 0)
	{	choice = 1, return choice;	}
	if ( sqaremark [6] == 1 && sqaremark [0] == 1 && sqaremark [3] == 0)
	{	choice = 2, return choice;	}
	if ( sqaremark [3] == 1 && sqaremark [0] == 1 && sqaremark [6] == 0)
	{	choice = 3, return choice;	}
	//line 2

	if ( sqaremark [7] == 1 && sqaremark [4] == 1 && sqaremark [1] == 0)
	{	choice = 1, return choice;	}
	if ( sqaremark [7] == 1 && sqaremark [1] == 1 && sqaremark [4] == 0)
	{	choice = 2, return choice;	}
	if ( sqaremark [4] == 1 && sqaremark [1] == 1 && sqaremark [7] == 0)
	{	choice = 3, return choice;	}
	//line 3

	if ( sqaremark [8] == 1 && sqaremark [5] == 1 && sqaremark [2] == 0)
	{	choice = 1, return choice;	}
	if ( sqaremark [8] == 1 && sqaremark [2] == 1 && sqaremark [5] == 0)
	{	choice = 2, return choice;	}
	if ( sqaremark [5] == 1 && sqaremark [2] == 1 && sqaremark [8] == 0)
	{	choice = 3, return choice;	}
	//diagonal

	//diag 1

	if ( sqaremark [8] == 1 && sqaremark [4] == 1 && sqaremark [0] == 0)
	{	choice = 1, return choice;	}
	if ( sqaremark [8] == 1 && sqaremark [0] == 1 && sqaremark [4] == 0)
	{	choice = 2, return choice;	}
	if ( sqaremark [4] == 1 && sqaremark [0] == 1 && sqaremark [8] == 0)
	{	choice = 3, return choice;	}
	//diag 2

	if ( sqaremark [6] == 1 && sqaremark [4] == 1 && sqaremark [2] == 0)
	{	choice = 1, return choice;	}
	if ( sqaremark [6] == 1 && sqaremark [2] == 1 && sqaremark [4] == 0)
	{	choice = 2, return choice;	}
	if ( sqaremark [4] == 1 && sqaremark [2] == 1 && sqaremark [6] == 0)
	{	choice = 3, return choice;	}

	//For the block

	//across

	//line 1

	if ( sqaremark [2] == 2 && sqaremark [1] == 2 && sqaremark [0] == 0)
	{	choice = 1, return choice;	}
	if ( sqaremark [2] == 2 && sqaremark [0] == 2 && sqaremark [1] == 0)
	{	choice = 2, return choice;	}
	if ( sqaremark [1] == 2 && sqaremark [0] == 2 && sqaremark [2] == 0)
	{	choice = 3, return choice;	}
	//line 2

	if ( sqaremark [5] == 2 && sqaremark [4] == 2 && sqaremark [3] == 0)
	{	choice = 4, return choice;	}
	if ( sqaremark [5] == 2 && sqaremark [3] == 2 && sqaremark [4] == 0)
	{	choice = 5, return choice;	}
	if ( sqaremark [4] == 2 && sqaremark [3] == 2 && sqaremark [5] == 0)
	{	choice = 6, return choice;	}
	//line 3

	if ( sqaremark [8] == 2 && sqaremark [7] == 2 && sqaremark [6] == 0)
	{	choice = 7, return choice;	}
	if ( sqaremark [8] == 2 && sqaremark [6] == 2 && sqaremark [7] == 0)
	{	choice = 8, return choice;	}
	if ( sqaremark [7] == 2 && sqaremark [6] == 2 && sqaremark [8] == 0)
	{	choice = 9, return choice;	}
	//down

	//line 1

	if ( sqaremark [6] == 2 && sqaremark [3] == 2 && sqaremark [0] == 0)
	{	choice = 1, return choice;	}
	if ( sqaremark [6] == 2 && sqaremark [0] == 2 && sqaremark [3] == 0)
	{	choice = 4, return choice;	}
	if ( sqaremark [3] == 2 && sqaremark [0] == 2 && sqaremark [6] == 0)
	{	choice = 7, return choice;	}
	//line 2

	if ( sqaremark [7] == 2 && sqaremark [4] == 2 && sqaremark [1] == 0)
	{	choice = 2, return choice;	}
	if ( sqaremark [7] == 2 && sqaremark [1] == 2 && sqaremark [4] == 0)
	{	choice = 5, return choice;	}
	if ( sqaremark [4] == 2 && sqaremark [1] == 2 && sqaremark [7] == 0)
	{	choice = 8, return choice;	}
	//line 3

	if ( sqaremark [8] == 2 && sqaremark [5] == 2 && sqaremark [2] == 0)
	{	choice = 3, return choice;	}
	if ( sqaremark [8] == 2 && sqaremark [2] == 2 && sqaremark [5] == 0)
	{	choice = 6, return choice;	}
	if ( sqaremark [5] == 2 && sqaremark [2] == 2 && sqaremark [8] == 0)
	{	choice = 9, return choice;	}
	//diagonal

	//diag 1

	if ( sqaremark [8] == 2 && sqaremark [4] == 2 && sqaremark [0] == 0)
	{	choice = 1, return choice;	}
	if ( sqaremark [8] == 2 && sqaremark [0] == 2 && sqaremark [4] == 0)
	{	choice = 5, return choice;	}
	if ( sqaremark [4] == 2 && sqaremark [0] == 2 && sqaremark [8] == 0)
	{	choice = 9, return choice;	}
	//diag 2

	if ( sqaremark [6] == 2 && sqaremark [4] == 2 && sqaremark [2] == 0)
	{	choice = 3, return choice;	}
	if ( sqaremark [6] == 2 && sqaremark [2] == 2 && sqaremark [4] == 0)
	{	choice = 5, return choice;	}
	if ( sqaremark [4] == 2 && sqaremark [2] == 2 && sqaremark [6] == 0)
	{	choice = 7, return choice;	}

	//middle sqare

	if ( sqaremark [4] == 0 )
	{	choice = 5, return choice;	}

	//new rand seed

	srand((unsigned)time( NULL ));
	
	//Corners

	if ( sqaremark [0] == 0 ||  sqaremark [2] == 0 || sqaremark [6] == 0 || sqaremark [8] == 0 )
	{
		int max = 4, randnum = rand() % max;	//get rand between 0 and 4

		if ( randnum == 0)
		{	choice = 1, return choice;	}
		if ( randnum == 1)
		{	choice = 3, return choice;	}
		if ( randnum == 2)
		{	choice = 7, return choice;	}
		if ( randnum == 3)
		{	choice = 9, return choice;	}
	}

	//Anything else

	else
	{
		int max = 9, randnum = rand() % max;
		choice = randnum + 1, return choice;
	}
}
   

  
//program loop

#include <iostream>
#include <time.h>
#include "TicTacToe.h"
using namespace std;

extern int choice;		//sqare player chooses

extern int player;		//what player it is (1 or 2)

extern int randnum = 0;	//for finding rands

extern int max = 0;		//max for rands


extern char xoboard [];	//the 9x9 board contents

extern int sqaremark [];//marked sqares


int main ()
{
	for (int i = 9; i > 0; i--)	//execute loop 9 times

	{
		drawboard ();	//draws board

		wincheck ();	//checks for a winner

		if (player == 1)
		{	pchoice ();	}	 //gets player choice

		else
		{	compchoice ();	}//gets comps choice

	}

	return 0;
}
   
heere is the error that comes up 54 times (each return call) error C2059: syntax error : 'return' can anyone plese tell me what i have done wrong with the funtion calling. [edited by - MTT on April 1, 2003 2:05:07 AM]

Share this post


Link to post
Share on other sites
Advertisement
At the end of each void function definition you need to put:

return;

for example.. this should be your pchoice function:

  
/*********************PCHOICE********************
/void pchoice () //finds which sqare is chosen and switches the player

{
psqarechoice: //for player

cout << "Pick a sqare: ";
cin >> choice;
if (((choice < 1) || (choice > 9)) || (sqaremark [choice - 1] != 0))//colision detection

{
cout << endl << "That sqare is not accesable" << endl;
goto psqarechoice; //return back to sqare choosing

}
else //if sqare is not allready chosen

{
sqaremark [choice - 1] = 1; //marks a 1 on sqaremark

xoboard [choice - 1] = 'O'; //drawes 'O' in chosen srare and makes player = 2

player = 2;
}
return;
}


Also, goto statements are not looked upon favorably in C/C++..

(silencer)

[edited by - -silencer- on April 1, 2003 2:09:21 AM]

Share this post


Link to post
Share on other sites
Thnx il try that tommorow cuz im tired and got to goto bed now.

Oh and i know that goto arnt good but there all i could come up with at the time (it just seemed soooooooooo much easier) so got any suggestions for code i can replace those with?

Share this post


Link to post
Share on other sites
Use a do/while loop instead of that goto..

Like this:

      
/*********************PCHOICE********************/
void pchoice () //finds which sqare is chosen and switches the player

{
int repeat = 0;
do
{
if (repeat != 0)
{
cout << endl << "That sqare is not accesable" << endl;
}
cout << "Pick a sqare: ";
cin >> choice;
repeat = 1;
} while (((choice < 1) || (choice > 9)) || (sqaremark [choice - 1] != 0)); //colision detection

sqaremark [choice - 1] = 1; //marks a 1 on sqaremark

xoboard [choice - 1] = 'O'; //drawes 'O' in chosen srare and makes player = 2

player = 2;
return;
}

(silencer)

[edited by - -silencer- on April 1, 2003 2:18:14 AM]

Share this post


Link to post
Share on other sites
if you want ai a little more sophisticated than this, have a look at the minimax algorithm

Share this post


Link to post
Share on other sites
Also.. you''ve got the variable name "choice" declared in 3 different places. Once in the header for extern in choice, once in the cpp file global in choice, and again in the function definition for AI(int choice). What exactly are you trying to do with the choice? If you want AI to just return a value, you don''t need to pass it a value (int choice) since it''s returning an int already. You''ve got this line:
cout << AI (choice);
..which is just going to output the choice to the screen. I think you want to pass choice into AI by reference, which you need to add & into the definition and prototype to do.

After looking it over again, I don''t think you''re trying to pass a value to your AI function, just return a choice. To do this, change your AI prototype and header to this:

int AI();

Everywhere inside your function AI definition, when you see the line:
choice = X, return choice;
..just change it to:
return X;

In the else statement at the bottom of your AI function, change this:
choice = randnum + 1, return choice;
..into this:
return randnum + 1;

You don''t need to have that choice variable in there. Now you just have to fix your compchoice function..
This should look better:

  
/*********************COMPCHOICE********************/
void compchoice ()
{
do
{
choice = AI(); // gets the AI''s choice

} while (((choice < 1) || (choice > 9)) || (sqaremark [choice - 1] != 0)) //colision detection

sqaremark [choice - 1] = 2; //marks a 2 on sqaremark

xoboard [choice - 1] = ''X''; //drawes ''X'' in chosen srare and makes player = 1

player = 1;
return;
}

Share this post


Link to post
Share on other sites
quote:
Original post by -silencer-
At the end of each void function definition you need to put:

return;



Just to be nit-picky, that''s not necessarily true. void functions do not(*) need a return at the end, although it certainly doesn''t hurt, and they can be used to end the function before it reaches the end. Personally, though, I think it''s good style.

Mike

(*) - This is true on all the compilers I''ve used, but I''m not 100% on whether or not it''s required by the all-mighty standard.

Share this post


Link to post
Share on other sites
You''re right.. it''s not required. The ''return'' error he''s getting is because he''s got a comma before all 54 return statements in his AI function. If he uses that fix I posted a bit ago by just return X; instead of setting choice equal to a number then "comma" return choice; it should be fixed. Can''t use commas like that.. they all need to be semi-colons to be correct syntax.

(silencer)

Share this post


Link to post
Share on other sites
k, i got it working. I only got 2 more questions for now

1: is there any way to clear the screen off every time before it draws the board

2: Where can i see this minimax algorithm, I might not relly understand it now but maybe il implement it in the future

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!