Need code for the Tic Tac Toe game

Status
Not open for further replies.

truth_seeker

Member level 1
Joined
Dec 7, 2005
Messages
37
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,620
Hello all ,

does anyone have a code for the tic tac toe game???

I would appreciate any help

Thanks a lot
 

Re: Tic Tac Toe??!!!

What language?
 

Re: Tic Tac Toe??!!!

Hello,

C language if possible , Java is another choice

but if you have it with other languages , then it is ok

thanks a lot
 

Tic Tac Toe??!!!

Try Google search for the following:
"tic tac toe" c
 

Re: Tic Tac Toe??!!!

Hello guys

I found a code on the internet written in C language , but there are few things I don't understand well , and I need your help

I don't understand the minimax function, an dI don't understand why using (-who) for example..what does ( _ ) mean??

I aslo don't understand the compmove function

in the output function..what is [4*i][8*j+6]=i*3+j+49 and [4*i+1][8*j+3]='X' and [4*i+1][8*j+3]='O'???

Besids can you please tell me..how many levels is this game..I mean minmax uses ply depth ..how many level does the computer in this code use to decide a mov??

how to know it?

i'm so sorry..i know they are too much questions..but I need your help..

Here is the functions i don't get :

/
Code:
* Outputs the board to the screen */
void output(void)
{
int i,j;
char out[12][24]={"       |       |       ",
		  "       |       |       ",
		  "       |       |       ",
		  "-----------------------",
		  "       |       |       ",
		  "       |       |       ",
		  "       |       |       ",
		  "-----------------------",
		  "       |       |       ",
		  "       |       |       ",
		  "       |       |       "};
for(i=0;i<3;i++)
	for(j=0;j<3;j++)
		{
		/* Put the square number in upper right corner */
		out[4*i][8*j+6]=i*3+j+49;
		/* Put 'X' or 'O' in the out array */
		if(board[i][j]==-1)
			out[4*i+1][8*j+3]='X';
		else if(board[i][j]==1)
			out[4*i+1][8*j+3]='O';
		}
/* Output the array to screen */
for(i=0;i<12;i++)
	printf("%s\n",out[i]);
}

/* Finds the best computer move for the given board
	using the minimax function */
void compmove(int who)
{
/* a[] is a list of the all the moves and it's score */
int i,j,a[9];
for(i=0;i<9;i++)
	a[i]=-who;
for(i=0;i<3;i++)
	for(j=0;j<3;j++)
		{
		/* Check for invalid move */
		if (board[i][j]!=0)
			continue;
		/* Try a move */
		board[i][j]=who;
		/* Score this board with the trial move */
		a[i*3+j]=minimax(-who);
		/* Undo the move */
		board[i][j]=0;
		}
/* Find the best move in the array */
i=find(a,who);
/* Make the move */
board[i/3][i%3]=who;
}

/* Recursive minimax the finds the score of
	a branch of the move tree */
int minimax(int who)
{
/* Tie = boolean test to see if a tie move has been found
   Best = The best score so far */
int tie=0,i,j,best;
/* Test for a loss (win for one level up) */
if(win(-who))
	return -who;
/* Test for tie */
if(boardfull())
	return 0;
for(i=0;i<3;i++)
	for(j=0;j<3;j++)
		{
		/* Skip invalid moves */
		if (board[i][j]!=0)
			continue;
		/* Try move */
		board[i][j]=who;
		/* Score this move */
		best=minimax(-who);
		/* Undo move */
		board[i][j]=0;
		/* Return the best move if it's a win
			because it's not going to get better */
		if(best==who)
			return who;
		/* At least one tie has been found */
		else if (best==0)
			tie=-1;
		}
/* If a tie has been found return tie */
if(tie)
	return 0;
/* Return a loss */
else
	return -who;
}

/* Finds the best move for who in a[] */
int find(int a[9],int who)
{
int i,best,index=0;
best=-who;
for(i=0;i<9;i++)
	{
	if(a[i]==who)
		return i;
	if(a[i]==0 && best==-who)
		{
		best=0;
		index=i;
		}
	}
/* Returns the location of the best move */
return index;
}
[/code]
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…