Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Tic-Tac-Toe AI Problem

Author
Message
zapakitul
17
Years of Service
User Offline
Joined: 1st Mar 2007
Location: In my world
Posted: 13th Jun 2009 18:49
I'm trying to do a small 3D Tic Tac Toe game, and I'm having a small problem with the AI! I have 9 square objects set up as the playing field, 9 X-es, and 9 0-s! (1-9 plain, 10-19 X, 20-29 0)! After the player selects a "square", his turn ends! Now, the enemy AI, in theory, picks a number from 1 to 9, and compares it to a square number "picked" by the player. If the number chosen by the AI is equal to one of the numbers occupied by the player, it will pick again, else, it places the object at that position! But i can't manage to do it right. Here's my code:

Randomize function:


Any help? Suggestions? Hints?
Alex843
15
Years of Service
User Offline
Joined: 12th Jun 2009
Location:
Posted: 13th Jun 2009 20:02
Hello.

I just recently finished making a tic tac toe game with AI the only difference was mine was in 2d. Now since it was in 2d I had to do things a little bit differently and I don't know if this will help you but its worth a shot. To save the position of X's and O's I used a 2d array called:

int tic[3][3] = {0,0,0,
0,0,0,
0,0,0};

If a number in the array was a zero then it meant there wasn't anything in that part of board. If a 1 then it was an X, if 2 then it was an O.

Here is my code for the random function. Basically I assigned each random number 0-9 a different index in the array. Now it would only attempt to put an O in if that particular index equaled 0.



To test and see if it changed anything I made a function to add up all the variables in that array and return the number. It's called
testarray().



At the beginning I put test == testArray(tic), this tells me what the array equaled before anything was changed. Then the loop would continue as long as nothing was changed. Once the rand function finally hit a spot that wasn't occupied by 1 or 2 the testArray(tic) returned a larger number than the varible test and the loop exited.

Now later on I added a ton of if statements that would make the AI play a lot smarter then just picking random spots, but I still had the random function in there just in case none of the if statements were triggered.
zapakitul
17
Years of Service
User Offline
Joined: 1st Mar 2007
Location: In my world
Posted: 13th Jun 2009 21:11 Edited at: 13th Jun 2009 21:14
You gave me a small idea, and it seems to be working, but i do have some problems with it! I declared 2 new varaibles:
int pPicked[9] and pickAgain = true;
When the player chooses a square, pPicked[square_chosen] == 1;
Now, when the computer plays, it chooses a random number between 1 and 9. Then checks if pPicked[chosen_number] != 1. If it is equal, then pickAgain = true, and it chooses it again, if not, then it positiones the object ePicked+20 to ePicked.X,0,ePicked.Z but i do seem to have a problem with that! Here's the new source:

The problem is, that sometimes it doesn't check again (at least i think it doesn't), since it doesn't always position the object where it has to.
zapakitul
17
Years of Service
User Offline
Joined: 1st Mar 2007
Location: In my world
Posted: 13th Jun 2009 21:45
Solved it!

Thanks for the help, Alex843. You pointed me in the right direction.

Login to post a reply

Server time is: 2024-11-05 23:15:57
Your offset time is: 2024-11-05 23:15:57