# Tic Tac Toe simulator - AI implementation

Page 1 of 1

## 5 Replies - 1451 Views - Last Post: 11 July 2012 - 05:32 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=285374&amp;s=435c7e21b80ba875c23dcd06b0a88f03&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 cmwise

Reputation: 5
• Posts: 167
• Joined: 14-February 09

# Tic Tac Toe simulator - AI implementation

Posted 10 July 2012 - 10:25 AM

Hey everyone,

So I'm trying to figure out the best way to code the decision-making process of the simple AI I'm implementing. The only way I can think of doing it is going through a list of available moves to make, and checking every single position's row/column/diagonal(in a few cases), and every position in that row/column/diagonal to see what move is the best one.

I'm just wondering if you guys can think of any more efficient ways to accomplish this? I'm stuck with this procedural solution currently, and it just looks hideous - not to mention it's somewhat replicating code that I've already written.

What do you think?

(Sorry if this is in the wrong forum.)

Is This A Good Question/Topic? 0

## Replies To: Tic Tac Toe simulator - AI implementation

### #2 Skydiver

• Code herder

Reputation: 1916
• Posts: 5,725
• Joined: 05-May 12

## Re: Tic Tac Toe simulator - AI implementation

Posted 10 July 2012 - 11:01 AM

The approach you described is the traditional way most game AI's work in larger problem space games.

With tic-tac-toe, since the problem space is so much smaller, you can do a lot more heuristics, and look up tables for what the next move should be. Most chess programs use an opening moves lookup table for the early phases of a game. I used to have an old learning assembly on the 6502 book, and it had a tic-tac-toe solution that used a lookup table of moves where the entire table was less than 64 bytes (if I recall correctly). Building the tables was tedious, but the code to lookup the next move was elegant. I wish I didn't give away that book.

This post has been edited by Skydiver: 10 July 2012 - 11:10 AM

### #3 dartoscoder

• D.I.C Regular

Reputation: 0
• Posts: 313
• Joined: 15-May 09

## Re: Tic Tac Toe simulator - AI implementation

Posted 10 July 2012 - 11:53 AM

If you could post some of the "ugly" code segments we could help you make it more efficent/elegant.

But that is the way you would handle AI for tic-tac-toe. You would start by checking all of the moves that would garuntee a win. Then, the ones that would prevent a win. Then all of the ones that would move tward a win.

You could go another step by figuring out all of the possible games that could be played and just make the program play the game that would best fit the game so far. But that would take some time. This is like the solution skydiver gave above.

This post has been edited by dartoscoder: 10 July 2012 - 11:54 AM

### #4 #define

• Duke of Err

Reputation: 978
• Posts: 3,397
• Joined: 19-February 09

## Re: Tic Tac Toe simulator - AI implementation

Posted 10 July 2012 - 08:22 PM

Would this work? An array that holds a weight/bias for that position.

The array is filled with 0 for a position already filled, and 1 for a vacant square.

A row is checked, if there are opponents the vacant squares are incremented. etc.

The position with a maximum weight can be the next played position.

### #5 blackcompe

• D.I.C Lover

Reputation: 1009
• Posts: 2,186
• Joined: 05-May 05

## Re: Tic Tac Toe simulator - AI implementation

Posted 10 July 2012 - 10:35 PM

cmwise: This may be of interest to you.

### #6 cmwise

Reputation: 5
• Posts: 167
• Joined: 14-February 09

## Re: Tic Tac Toe simulator - AI implementation

Posted 11 July 2012 - 05:32 AM

Thanks very much for all of the replies! I'm not sure I'm looking to create an AI that progressively learns as more games are played, just a simple AI that will have very basic decision making. I suppose I'll just continue with the procedural solution I've been writing, as it seems to be the most straight-forward way to accomplish what I'm aiming to do.

If anyone happens to look back at this post, where should I look to start learning how to make simple graphics that will present this simulator?

Thanks again for all of your help!