Tic Tac Toe (Artificial Intelligence)

Can any one please help me that how would i do artificial intelligence in tic tac toe game to make computer's turn intelligent?
Thats pretty broad. What are you having trouble with? AI programming can be very fun, you should give it a shot and tell us where you get stuck at
I am getting confused that which smallest logic should I apply? One thing is that if I store all the possible turns in a file and then read it during the start of the program and then compare it with the current game. So the computer will become intelligent? I am taking random values using srand and rand function. But it is becoming very bold that the computer is doing very silly turns.
It's a lot easier to help if you post some code for us... but let me see what I can do in psuedocode for you.

I am assuming you have made the computer:
-Check your moves each turn
-Pick a random move.

You should probably give the computer some sense of priority. for example
-Check your moves each turn
-->if you have 2 x in a row, then the third space becomes move
-->else pick a random move

something like that.

Also, as for it's wild choices, make sure you only seed srand once, or it may continuously pick the same number as it's random value.

Another suggestion I'd make is to have the computer check its own moves (perhaps have two functions, a void playerturn() and a void compturn() )
so on compturn

-Check your moves each turn
-->if you have 2 x in a row, then the third space becomes move
-------->block you
-Check computers moves
-->if two x in a row, priority on third space
------->play that third space
-->else if one x has a free space adjacent to it
------>check for possible three in a row
---------->if it's open, play here
-->else play a random move

during your turn, you could do all of your input, etc
Last edited on
Ever heard of MinMax? Not?? I suggest hearing about it then. :P LOL!!
NerdTastic thank you very much. I got your point.
I've posted my TTT AI on here, http://cplusplus.com/forum/general/64067/ take a look at it if you think it will help you, maybe you can come up with a more elegant solution.
Last edited on
@Waleed Azam

no problem
Seriously, why don't you check out the MiniMax algorithm? Its easy, but extremely efficient. I recently developed a fully functional chess game using Minimax algorithm.
Topic archived. No new replies allowed.