Where to call AI_response?

Started by
28 comments, last by Matt Apple 19 years ago
How does your call to AI_Response look now?

*EDIT* And remember to return false after that string of if statements.
----------------------------------------------------------No matter how eloquently you state your argument, the fact remains that the toilet seat is a bistable device. Therefore it's natural position is no more down than it is up.[SDL Smooth Tile Scrolling]
Advertisement
My call to AI_response() looks like this:
if(grid->blitFromInput()) grid->AI_response(); 


So my blitFromInput() looks like this now, after some slight editation:

bool Grid::blitFromInput()         {    if(key[NULL])              // the user isnt pressing anything...    {        return false;              // the user didnt make a move.    }        if(key[KEY_1_PAD]){	blitToRect(0, 2, 'X');return true;}        if(key[KEY_2_PAD]){	blitToRect(1, 2, 'X');return true;}	if(key[KEY_3_PAD]){	blitToRect(2, 2, 'X');return true;}	if(key[KEY_4_PAD]){	blitToRect(0, 1, 'X');return true;}	if(key[KEY_5_PAD]){	blitToRect(1, 1, 'X');return true;}	if(key[KEY_6_PAD]){	blitToRect(2, 1, 'X');return true;}	if(key[KEY_7_PAD]){	blitToRect(0, 0, 'X');return true;}	if(key[KEY_8_PAD]){	blitToRect(1, 0, 'X');return true;}	if(key[KEY_9_PAD]){	blitToRect(2, 0, 'X');return true;}	return false;}      
Okay. All good stuff. If it doesn't work still, I have one more suggestion, then you've got me stumped. Change blitToRect to return a boolean. True on a valid move false on an invalid one. Then change

if(key[KEY_1_PAD]){ blitToRect(0, 2, 'X');return true;}

to

if(key[KEY_1_PAD]){if(blitToRect(0, 2, 'X')) return true; else return false;}

for each of your keys.

I think the computer might read the same keypress through multiple loops since you're just checking for keystate and not for a keydown event.
----------------------------------------------------------No matter how eloquently you state your argument, the fact remains that the toilet seat is a bistable device. Therefore it's natural position is no more down than it is up.[SDL Smooth Tile Scrolling]
Ok, I'll try that.
Quote:Original post by CodeTitan
I have this almost working, but it's just a minor logic error somewhere that is causing AI_response() to be called more than once in a turn.


Aha! I think I figured it out!
bool Grid::AI_response(){	srand(time(0));		int x = rand()%3;	int y = rand()%3;	if(theGrid[x][y] == 0)	{		blitToRect(x, y, 'O');		return true;	}	else	{		for(int i = 0; i < 3; i++)			for(int j = 0; j < 3; j++)			{				if(theGrid[j] == 0) blitToRect(i, j, 'O');				break;   // Only breaks you out of Inner Loop!			}		return true;	}}


The break statement in AI_response() just breaks you out of the INNER LOOP! The outer loop keeps going resulting in multiple turns for the AI for each player turn!

Bow before my debugging prowess. :-)
*bows*
----------------------------------------------------------No matter how eloquently you state your argument, the fact remains that the toilet seat is a bistable device. Therefore it's natural position is no more down than it is up.[SDL Smooth Tile Scrolling]
Found some other stuff....

bool Grid::AI_response(){	srand(time(0));		int x = rand()%3;	int y = rand()%3;	if(theGrid[x][y] == 0)	{		blitToRect(x, y, 'O');		return true;	}	else	{		for(int i = 0; i < 3; i++)			for(int j = 0; j < 3; j++)			{				if(theGrid[j] == 0) blitToRect(i, j, 'O');				break;   			}		return true;	}}


An IF statment without brackets only encompasses up to the first semi-colon following(barring a nested IF or LOOP). This break statement actually executes every time the inner loop executes and always on the first iteration whether or not the IF statement is true. From long, hard experience I have learned: Use the friggin' brackets even if you think it is unnecessary.

Also don't seed your rand in a function that is going to get called multiple times. You only need to seed your rand once at the beginning of your program.
How do I fix the srand() problem? Sorry, I'm not very experienced in this field.

Now the problem is that when you make a single move, it makes 8 moves and fills up the board.

Here's my code:

bool Grid::AI_response(){	srand(time(0));		int x = rand()%3;	int y = rand()%3;	if(theGrid[x][y] == 0)	{		blitToRect(x, y, 'O');		return true;	}		else	{		for(int i = 0; i < 3; i++)			for(int j = 0; j < 3; j++)			{				if(theGrid[j] == 0)				{					blitToRect(i, j, 'O'); 					return true;				}							}	}	return true;}
Call srand() before you start your main game loop. Other than that, I'm out of ideas for you.

*EDIT* Figure out how many times AI_response is being called. If it's once and the computer takes 8 moves the problem is in AI_response. If it's 8 times the problem is with the code that is calling AI_response. Just increment a counter at the end of the function and then print it out somewhere.
----------------------------------------------------------No matter how eloquently you state your argument, the fact remains that the toilet seat is a bistable device. Therefore it's natural position is no more down than it is up.[SDL Smooth Tile Scrolling]
Quote:Original post by CodeTitan
How do I fix the srand() problem? Sorry, I'm not very experienced in this field.

Now the problem is that when you make a single move, it makes 8 moves and fills up the board.


In regards to the srand() thing. It's not really a problem you just need to know that you only need to execute srand() one time during the entire program(somewhere before the first time you use the rand() function). If you srand() more than once you can run into unwanted results(like getting the same number over and over).

I agree with wasted_druid you need to figure out how many times AI_Response() is being called. Put a counter inside of it and increment it every time it is called.

If you want more help than than you are going to have to post all the code. Trying to debug a program from a code snippet is like trying to fix a car by just looking at the carburetor.

This topic is closed to new replies.

Advertisement