Skip to main content
GameDev.net gamedev.net
🔒 Locked

Tic Tac Toe AI

Started by skatejeppe1 Oct 10, 2010 at 4:48 AM 6 replies 3.3k views
Original Post
skatejeppe1
skatejeppe1
Hey!
I developed a tic tac toe in visual c++, Allegro. It's only player1 vs player2 at the moment.Now I want to make a singleplayer option. How would I make the AI block me from getting 3 in a row? would it be done by checking if there is a mark at place a and C then put the marker in B?

lets say this is the board:

[1]|[2]|[3]
-----------
[4]|[5]|[6]
-----------
[7]|[8]|[9]

would I make it check if there is a marker at lets say 1 and 3, and if there are it would check if 2 is marked,(game would be over if I had a cross there but if he had put one there it wouldn't)if it isnt it would place a O there. but then, how could I make it so if he had for example 1 and 3 and i had 4 and 6, then how could i do so he does not block instead of win there?

please give me a hint. note that I'm not really asking for code, only how it would be done :)
IADaveMark
IADaveMark
Tic-Tac-Toe AI is well documented. In fact, it is one of the core techniques behind most tree-search-based functions. Search for it by name. Also try "chess tree search" or simply "tree search AI". Even Wikipedia will show you how it is done.
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<
skatejeppe1
skatejeppe1
Thanks! I think i can figure out how to make this now :) My knowledge of c++ is not the greatest but I think I'll figure it out with the help
Pyrogame
Pyrogame
For your specific problem, you can resolve it this way:

1) Check, if the AI can win on this round (if yes, let it win ^^)
2) Check, if the Player can win on the next round (if yes, try to block)


There are some rules you can implement directly (because this game is not too difficult to brute force the KI mechanics):

1) If the player marks center [5] at game start, let the AI mark a corner [1], [3], [7] or [9]. Then, there are 6 possibilities always to block the players win move. But, if the player marks the opposite corner (ie. AI marks [1], and player [9]), then let the AI mark a corner as well -> draw.

2) If the player marks a corner [1], [3], [7] or [9], then let the AI mark the center [5]. Then simply block the player -> draw.

3) If the AI marks center [5] at game start, and the player marks a side [2], [4], [6] or [8], let the AI win by marking one of the sides, that are not the opposite to the marked field by the player. Ie. the player marked [2], then the AI should choose [6] or [4], but not [8]. The player has to block this move, but then the AI can mark a field opening 2 possibilities to win the game.

4) If the AI marks center [5] at game start, and then the player marks a corner, then let the AI mark the opposite corner. Now if the player marks the next corner, simply block the next moves -> draw. But if the player marks a side, then mark the next corner (blocking the player ofcourse, if he is going to win the next round). At the same time this opens 2 possibilities for the AI to win the game itself in the next round.
BjsAust
BjsAust
TIC-TAC-TOE is a solved game, so the question is less what can you do, and more what should you do? Is the game fun if the computer always uses optimal strategy?
IADaveMark
IADaveMark
Quote:
Original post by BjsAust
TIC-TAC-TOE is a solved game, so the question is less what can you do, and more what should you do? Is the game fun if the computer always uses optimal strategy?

You didn't really ask this question, did you? Tic-tac-toe is never a fun game if you are older than the age of about 8 specifically because it is a solved game with a minuscule state space.
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<
sss_1234
sss_1234
Here's a simple method so the computer won't win always:

Before the game starts, the player choose a level between 0 and 10.
When it's the computer turn, generate random number between 0 and 10;
If the random number is less than the level - use the optimal algorithm to choose the best turn. If the random number is less or equal to the level - randomly choose an unoccupied cell.

I saw this method once in a tic-tac-toe program programmed in QBASIC.

Good Luck!
------------------------Computers do what you tell them to do, not what you want them to do.
mandar9589
mandar9589
3x3 tictactoe can be solved by using if-else loops however if one want to code a proper tictactoe using min-max methods then it would be the first step in AI programing. Once 3x3 tictactoe is done using min-max(or alike),its interesting to program connect m on nxn and so on...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.