#### Archived

This topic is now archived and is closed to further replies.

# AI tic-tac- toe

This topic is 5841 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hi have anyone done a tic-tac-toe before,can you guy teach me how to begin to write it since i''m a beginner.by the way i''m using C to begin to write this game.

##### Share on other sites
I''ve done this in the past, although I don''t have my code with me. My approach was:

- If the AI can win in one move, make that move.
- If the player could win on the next move, block it.
- Otherwise, find the board position, or a rotation/reflection of it, in a predefined list.

Making the list takes a little while, but there weren''t many positions on it.

##### Share on other sites
I made a TicTacToe once too.

- If the AI can win in one move, make that move.
- If the player could win on the next move, block it.
- Otherwise, use a random place on the board that is free

The Sorcerer

##### Share on other sites
Try here and here.

Miles

##### Share on other sites
1. Make a winning move.
2. Block the player's winning move.
3. Get the first available corner.
4. Get the first available non corner.
5. Get the center.

If the computer gets the first move the best the player can do is draw.

I used a nine element array and lots of if statements.

When I did this in VB I used an array of label objects.

[edited by - murph on December 4, 2002 9:39:43 AM]

##### Share on other sites
i want to ask how to write the block.

##### Share on other sites
Use MinMax with AlphaBeta pruning to build a tree of the entire game and choose the best good move available. Plays perfectly. Can easily be extended to Four-In-A-Row.

Will

##### Share on other sites
Yeah, but who wants to play against an AI that will automatically win if you make one mistake? Instead, do things like search depth proportional to difficulty, or let it learn which moves are bad or good by a training process. This method is obviously far more difficult to implement, but I think it will make for a far more enjoyable game.
Brendan

##### Share on other sites
An ANN (artificial neural network) sounds like a good solution. You could also use a genitic algorithm to make it learn the more you play (www.ai-junkie.com should help).

One more thing; if you want to check for a win dont check each possibility -> in my gr 10 programming class we had to make a two player tic tac toe game and all you saw were students, except me typing like mad for half an hour trying to solve for a win. All you have to do is extend the tic tac toe grid, assign a value to x,o, and blank. Then add accross each grid and you can tell if there is a win if the ends of the grid add up to a certain value.

  if x = 1, o = -1, and blank = 0       __ _____| 3||  o x| 0||o x o|-1||x____| 1||0_0_0__1|

and as you can see the 3 represents a x win and if we had a -3 it would be a o win. Easy stuff

-- Rule of Acquisition #59

##### Share on other sites
Well, it really depends on how complex you want it to be and whether you want it to be perfect or not. When I once did this, I was just assigning all free fields different weights, so I'd just go through each field and look if it was needed for blocking the player or for winning itself. If so, it got a high weight, if not, it was lower depending on the position and so on.
The actual move was chosen randomly based on the weights, which made the "better" moves far more likely than the bad ones but the AI could still make errors.
Worked fine.. Although you still have a lot of checks (probably even more than with the perfect ones), but it makes the play somewhat random.

[edited by - Wuntvor on December 5, 2002 8:04:21 AM]

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633678
• Total Posts
3013292
×