Advertisement

My first AI gone crazy

Started by June 13, 2010 09:09 AM
19 comments, last by Smarty_T2 14 years, 8 months ago
Hi all, i am new to Game programming and have never done AI programming before...I am designing a Tic-Tac-Toe game and am having problems with the AI moves...Here is my function for the same..I don't know where the problem is, but it doesn't seem to work as expected...

void ai_move(){	static int priority[3][3],p,no,nx,s[9],c,i,j;	for(i=0;i<3;i++)	{		no=0;		nx=0;		for(j=0;j<3;j++)		{			if(box[j]==-1)				no++;			if(box[j]==1)				nx++;		}		if(no+nx == 3)			p=10;		if(no==2 && nx==0)			p=0;		if(no==0 && nx==2)			p=1;		if(no==0 && nx==0)			p=2;		if(no==1 && nx==0)			p=3;		if(no==0 && nx==1)			p=4;		for(j=0;j<3;j++)			priority[j]=p;	}	for(j=0;j<3;j++)	{		no=0;		nx=0;		for(i=0;i<3;i++)		{			if(box[j]==-1)				no++;			if(box[j]==1)				nx++;		}		if(no+nx == 3)			p=10;		if(no==2 && nx==0)			p=0;		if(no==0 && nx==2)			p=1;		if(no==0 && nx==0)			p=2;		if(no==1 && nx==0)			p=3;		if(no==0 && nx==1)			p=4;		for(i=0;i<3;i++)			priority[j]+=p;	}	for(int a=0;a<9;a++)	{		for(i=0;i<3;i++)		{			for(j=0;j<3;j++)			{				if(priority[j]==a && box[j]==0)				{					s[c]=(j*3)+i;					c++;				}			}		}		if(c>0)			break;	}	int q=random(0,c-1);	domove(s[q]);	if(complete_check()==1)		finished();}


In this function, priority[3][3] is an array that stores the priority number of each block and the one with least priority is what the AI moves...
"no" and "nx" store the number of O's and X's respectively.

The problem is that the priority calculation has some error(I guess so) which i am unable to find...Or if there is some other problem, that i don't know about....
Please somebody help me...This AI has really made me mad :(
One more thing i forgot to mention...
box[3][3] is a global integer array that stores the current state of the boxes..
-1 means O
0 means empty
1 means X
Advertisement
Quote:
Original post by Smarty_T2
I don't know where the problem is, but it doesn't seem to work as expected...


What is expected? How is the function supposed to work?

Also, I recommend that you get rid of globals as well as static variables, and just pass data as parameters to the functions that need it. And on that note, you should also create helper functions to avoid duplicating code.

BTW, you might want to take a look at game trees and Minimax.
I expect the function to make an appropriate move...I tried this algorithm on paper and it works fine, but not when i run the program...
Also, box is a global variable because it is used everywhere in the program as it stores the main info regarding the game and about static, it's good to use it because i want the variables to be pre-initialized..

To be more precise,
I want the function to read the current game status from the "box" integer array and then choose a random move from all possible and appropriate moves available. By that i mean that computer player should play intelligently and randomly..
So you're saying that the function doesn't implement your algorithm correctly - but what is the algorithm?
The algorithm is that my function assigns a priority number(0-4) to each block/cell row wise and column wise and add both the numbers. The resultant number is the final priority for each cell. Then it checks for the cells with minimum priority and then chooses randomly among the ones that have minimum(and same) priority.
The priority of each cell is given on this basis...
2Os and no X = 0
no Os and 2X = 1
no Os and no Xs = 2
1 O and no Xs = 3
no Os and 1 X = 4

I hope i am clear...And sorry for my bad english..
Advertisement
Yep. You're doing it the hard way. Go look up minimax.

Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC
Professional consultant on game AI, mathematical modeling, simulation modeling
Co-founder and 10 year advisor of the GDC AI Summit
Author of the book, Behavioral Mathematics for Game AI
Blogs I write:
IA News - What's happening at IA | IA on AI - AI news and notes | Post-Play'em - Observations on AI of games I play

"Reducing the world to mathematical equations!"

To be honest, i didn't understood any of that thing.. :(
Also, i want to know the problem with my method.
Thanks

Did you try debugging the program?
Quote:
Original post by Gage64
Did you try debugging the program?

This.

To be honest, the first thing you should do is run through your program in debug mode and watch the execution of every single line and how it changes every single variable. Find our answer there, you will.


Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC
Professional consultant on game AI, mathematical modeling, simulation modeling
Co-founder and 10 year advisor of the GDC AI Summit
Author of the book, Behavioral Mathematics for Game AI
Blogs I write:
IA News - What's happening at IA | IA on AI - AI news and notes | Post-Play'em - Observations on AI of games I play

"Reducing the world to mathematical equations!"

This topic is closed to new replies.

Advertisement