• ### Announcements

#### Archived

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

# Tic Tac Toe AI

## Recommended Posts

Ekim_Gram    418
I''m going to be making a Tic Tac Toe clone in C++ and I''m wondering, how am I going to do the AI? Also, I have never programmed any AI at all.
There''s no town drunk here, we all take turns. Velocity Gaming Force

##### Share on other sites
Jakarta    140
go through every single move the computer can do and at every move go through every possiblity that the opponent can do. If any of the opponents moves make you loose don''t do them. then do the same thing backwards and see if there is any move that the computer can do that will force the computer to win no matter what. Look like two moves ahead and you will be fine.

sorry I don''t have much time to explain now, did any of that make sense to you?

I must''ve told you a million times, don''t over-exaggerate.

##### Share on other sites
Ekim_Gram    418
I sort of get it but wouldn''t that take like 30 if statements?

[offtopic]
I like your sig by the way
[/offtopic]

There''s no town drunk here, we all take turns.
Velocity Gaming Force

##### Share on other sites
DarkWhoppy    122
Heh, if you do that it''ll be impossible for the player to win. I did a TicTacToe game with AI just a few weeks ago. Its harder than it seems

##### Share on other sites
Ekim_Gram    418
quote:
Original post by DarkWhoppy
Heh, if you do that it''ll be impossible for the player to win. I did a TicTacToe game with AI just a few weeks ago. Its harder than it seems

Can you explain to me what you did? Or at least post a piece of your source?

There''s no town drunk here, we all take turns.
Velocity Gaming Force

##### Share on other sites
Kevlar-X    122
Ekim_Gram:
I'm guessing the logic for a game like Tic-Tac-Toe (aka knots and crosses for them brits ) will take quite a few if statements, or comparisons between arrays to enforce the AI rules.

However, you should be able to cut back on the repition of things with a few recursive calls.

Anyway, try googling about for some examples of how to do the AI. (I'd suggest how to do it but I gave up on that one along time ago, found something more interesting to work on at the time )

[edited by - Kevlar-X on August 13, 2003 12:40:15 AM]

##### Share on other sites
Guest Anonymous Poster
there are many ways to program an AI for tic tac toe.
One way is to have a list of all possible board positions and then associate with each one the correct next move for each. This is good but not fun to write. (of course there is a lot of symmetry to tic tac toe so many variations are just the same but with the board rotated)

You can do a recursive tree structure that automatically puts a numerical value for each level based on the chances of a win... its cool to accomplish if you are interested in AI in and of itself.

You could also just go through each square at a time and then afterwards randomly fill the board in many times. Use a stat for each square and the one with the highest amount of wins for the computer gets selected.

##### Share on other sites
quote:
Original post by Kevlar-X
(aka knots and crosses for them brits )

lol, its Naughts and Crosses. close though

An ASCII tetris clone... | AsciiRis

##### Share on other sites
Nervo    344
[threadjacking]Yeah, those brits! So like can I be british if I grew up in a british commonwealth country? Even only if until I was 10?[/threadjacking]

##### Share on other sites
medovid    122
I made a tic tact toe game once except it was in VB and it had a very stupid AI. it''s on Vbcode.com. search for it. They have a few cool tic-tac-toe games.

##### Share on other sites
look up game trees and the mini-max algorithm.

basically it involves calculating every possible move that could be made by either player. Every time its the computers turn, it makes the best move available, with the assumption that the human player will make the best move available to him.

Mini max trees are really a neat thing and not too complicated at all. As a bonus, I think a lot of the info ive seen on mini-max use tic-tac-toe as their example

http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20401595.html

[edited by - AndreTheGiant on August 14, 2003 1:53:19 PM]

##### Share on other sites
capn_midnight    1707
basically, if the computer has the first move it needs to take the center square.

After that, you want to take the corners. There reason is, you wan''t to try to setup a situation like this:
o|.|x-+-+-.|o|.-+-+-o|x|.

in this situation, x cannot win. To prevent this situation, you try to take the corners.

Always look for a blocking move. The way I did it was to assign numeric values to X, blank and O. X=-1, blank=0, O=1. I would then add all the values in a line (there are 8 different lines, 3 horizontal, 3 vertical and 2 diagonal). if any of the lines came to an absolute value of 2, then someone was about to win. If it was the computers piece (line_value/computers_value will be greater than 0), then the computer needs to play there to win. If it is not (line_value/computers_value is < 0) then the computer needs to mark that as the most likely place to play, continue checking to make sure there isn''t a winning play.

and that''s pretty much it.

Do you use your powers for good or for awesome?
My newly updated site | The Cutter Project | Association of Computing Machinery