• ### What is your GameDev Story?

#### Archived

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

# Tic Tac Toe AI question...

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

## Recommended Posts

Ok right now im working on relearning c++, for the second time, and I found an old program I was working on called TicAIToe, its basicially a smart tic tac toe game where the computer can accuratly block your moves. Ok now my question is for the AI is it a bad design to type out an if statement to determine what blocks the player picked and if there is a line (combo) then the computer places its x into the last space in the combo? I cant think of a better way to do it, it just seems kind of rediculus to have a 200+ line tic tac toe game, or am I doing it right?

##### Share on other sites
200 lines could either be way too much or it might be fine. It''s hard to comment to whether or not code is ugly without seeing the actual code.

##### Share on other sites
Sounds as if combination of ''for'' statements should do the job. Check each column and see if it contains two opposition pieces and one empty space. If so, put a piece in the empty space. Do the same for the rows and you might need to handle the two diagonals in a special case…

##### Share on other sites
I would use data files... makes it so much simpler and easier to code for (and adding learning abilities are easier- at least some types)

##### Share on other sites
Are you at all interested in doing Tic Tac Toe AI the "right way" with MiniMax? There are some good resources in the "Articles & Resources" section above on doing exactly that.

Dave Mark - President and Lead Designer
Intrinsic Algorithm -
"Reducing the world to mathematical equations!"

##### Share on other sites
ok, the TTT game board is jsut a simple sqaure right? So you could represent it with a two-dimensional array in C++ or a vector. The access the colum and row you need like this:

// prototypes
int Board[3][3]; // init''s a 3x3 board.
int rows;
int colums;

// in main
int x,y;
for (int Board; // whatever else here)
// loop through and see where the advailable places are
// then place the piece in the most optimal one.

// you could use nested for loops and vectors, this should
// give you a good idea of what you need to do though. I hope.

If-else loops are ok, but they make for a much longer program that is harder to read. This solution is a bit better in my opinion. Good luck.

My Kung Fu is stronger.
neo88

##### Share on other sites
I must say, I love Tic Tac Toe AI questions, as for once I feel that I can actually be helpful. My T3AI (no reference to any Ahnold movies intended) is located here, I believe. Feel free to check it out, although I'm not sure that it's the most recent version of that code. Ahh, good times back then!

- CD

// Brought to you by Code_Dark
| [( Politics Forum Beta-Test )]

[edited by - Code_Dark on April 13, 2004 8:10:00 PM]

##### Share on other sites
quote:
Original post by InnocuousFox
Are you at all interested in doing Tic Tac Toe AI the "right way" with MiniMax?

Dave, why is Minimax the "right way" in your opinion? It is merely "one way" to solve Tic Tac Toe (and no, I''m not suggesting people use ANNs).

Timkin

##### Share on other sites
quote:
Original post by Timkin
Dave, why is Minimax the "right way" in your opinion? It is merely "one way" to solve Tic Tac Toe (and no, I''m not suggesting people use ANNs).

It beats the hell out of "[typing] out an if statement to determine what blocks the player picked and if there is a line (combo) then the computer places its x into the last space in the combo" Sorry if I inadvertantly excluded some of the more esoteric methods.

Dave Mark - President and Lead Designer
Intrinsic Algorithm -
"Reducing the world to mathematical equations!"

##### Share on other sites
I always liked to assign weights to each possible place where the computer could place a tile and then choose one randomly. Makes it at least a little bit interesting to play against the AI, as it will not always make the same moves. Was quite easy and short to implement as well, if I recall correctly.

• ### What is your GameDev Story?

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

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634088
• Total Posts
3015449
×