Public Group

# Help with my first basic AI

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

## Recommended Posts

Hi, I was just wondering how I would go about implementing my first smart-AI for a TicTacToe game. I know I could have a simple loop, where the next available space would get chosen for the AI or I could just use a random number to decide its move. That would work, but it would not be very effective. I could also just hard-code every possible choice, but that seems a little crazy. Is there a more effective way at going about it? Thank you for any help.

##### Share on other sites
Personally I had the AI 1st look for a position that would give it a winning line...if one was available I used that position...if not then I looked for a position that would block a players winning line...if one was available I used that one...Otherwise I just chose the next empty space on the board.

##### Share on other sites
hi there,

i havent done any AI in C++ or C, and i haven't done any TicTacToe game, but my guess is this:

In the first round, the computer chooses a random position.

In the second round, the computer chooses the next available position to the upperleft(if any)to the left(if any)to the right(if any)to the upperight(if any)to the lowerleft(if any) or to the lowerright(if any).

in the third round, the computer checks to see where the player has to go in order to win(depending on wheter or not the player or computer starts the first round)and checks to see where he has to go in order to get three in a row.

As i said i'm not at all an AI programmer, but that would be my guess =)

Good luck and cheers,

Samsonite

##### Share on other sites
There's another active thread about that right now. The solution there is to use the Min-Max algorithm to perform an exhaustive search over the possible moves. There are many other ways to have an AI play optimal Tic Tac Toe, but that is rather straight-forward and the algorithm is intuitive. I'd suggest you look up some background info on the Min-Max algorithm, and refer back to that thread (here) if you get stuck.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 11
• 9
• 9
• 38
• ### Forum Statistics

• Total Topics
634128
• Total Posts
3015698
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!