Tic-Tac-Toe
Oops. I just noticed that Tic-Tac-Toe was the game I was trying to create. It has a different name from the one I know here, but well, now that I know that i''m trying to make a Tic-Tac-Toe like game, could you help me with the concepts I have to learn and some code pls? I don''t wanna go as far as chess programming for now, I just wanna learn the basics to do some Tic-Tac-Tow AI.
Read the article on Gamedev about chess programming. The article talks about chess programming, but the concepts are the same for any board game of this type. In other words, the same concepts are used in chess, checkers, tic-tac-toe, etc.
Read that article and pay attention to the things that aren''t necessarily chess specific. To make AI for tic-tac-toe, you will need a way to evaluate who is winning, a way to generate all of the possible moves that can be made on the board, and a method for making a move on the board, as well as undoing a move that was played on the board. In other words:
1. A way to evaluate a position and tell who is winning
2. A way to generate legal moves
3. A way to make moves
4. A way to undo moves after they have been made.
Anyway, read that article and pay attention to those things, and then come back here with any questions that you have after you read that article. Until you understand the basic idea of how it works, you will probably only get more confused by code. I (and probably others) will help you along once you do a little research on your own. It''s ok if you don''t understand everything after doing your research, but at least then you can ask better questions instead of just saying, "I want to make a tic-tac-toe program so tell me everything I need to know."
Russell
Read that article and pay attention to the things that aren''t necessarily chess specific. To make AI for tic-tac-toe, you will need a way to evaluate who is winning, a way to generate all of the possible moves that can be made on the board, and a method for making a move on the board, as well as undoing a move that was played on the board. In other words:
1. A way to evaluate a position and tell who is winning
2. A way to generate legal moves
3. A way to make moves
4. A way to undo moves after they have been made.
Anyway, read that article and pay attention to those things, and then come back here with any questions that you have after you read that article. Until you understand the basic idea of how it works, you will probably only get more confused by code. I (and probably others) will help you along once you do a little research on your own. It''s ok if you don''t understand everything after doing your research, but at least then you can ask better questions instead of just saying, "I want to make a tic-tac-toe program so tell me everything I need to know."
Russell
I wrote a kind of AI in the next way (it''s done by checking per turn):
1st Turn: Computer player checks if the human player played on the middle square.
2nd Turn: - Comp player checks if the human player is about to win, if so, blocks him, if not, plays in other square, trying to win.
And so and on...
Well, I was wondering if there was a shorter way of writting the 2nd turn, cause I use ''if'' statements for each square. That means a long code.
1st Turn: Computer player checks if the human player played on the middle square.
2nd Turn: - Comp player checks if the human player is about to win, if so, blocks him, if not, plays in other square, trying to win.
And so and on...
Well, I was wondering if there was a shorter way of writting the 2nd turn, cause I use ''if'' statements for each square. That means a long code.
Check this out:
http://www.gametutorials.com/Tutorials/Console/Console_Pg1.htm
Very simple AI and very detailed comments (although the code is a bit clunky).
http://www.gametutorials.com/Tutorials/Console/Console_Pg1.htm
Very simple AI and very detailed comments (although the code is a bit clunky).
I just went through a huge set of threads where various AI concepts were kindly explained to me, both here and on usenet. Study up on the minmax (or minimax) algorithm and how it works with recursion to test the leafs of a tree.
The easiest AI to make for tic-tac-toe imo would be an expert system, aka a list of if statements. There are a few basic rules that you can follow to almost never lose in 3*3(standard board size) tic-tac-toe:
1) If you can win, place you marker in the appropraite square to win.
2) If you oppnent is about to win, attempt to block.
3) If the opponent went in the center, place your marker in a corner.
4) If the center is open, place your marker there.
5) If the opponent went in a corner, place your markeron a side (any place but a corner or center)
6) If the opponent chose in a side, choose a corner.
(when choosing a corner or side, randomly pick one. Actually, it might be good to pick one next to an opponent piece if you are just trying to tie every time)
It was a long time ago that I used those rules in my Tic-Tac-Toe AI so they might not be 100% correct but iirc there is only 1 way to beat it and it requires a little luck.
"The Requested Information Is Unknown Or Classified" -Anonymous
1) If you can win, place you marker in the appropraite square to win.
2) If you oppnent is about to win, attempt to block.
3) If the opponent went in the center, place your marker in a corner.
4) If the center is open, place your marker there.
5) If the opponent went in a corner, place your markeron a side (any place but a corner or center)
6) If the opponent chose in a side, choose a corner.
(when choosing a corner or side, randomly pick one. Actually, it might be good to pick one next to an opponent piece if you are just trying to tie every time)
It was a long time ago that I used those rules in my Tic-Tac-Toe AI so they might not be 100% correct but iirc there is only 1 way to beat it and it requires a little luck.
"The Requested Information Is Unknown Or Classified" -Anonymous
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
Since the board is so small (only 3x3), and there are only 0''s or X''s in each square, it is possible to make the AI perfect - that is it would never lose, but might draw. This of course makes a boring game if you are playing against the computer, but then tic-tac-toe isn''t the world''s most interesting game in the first place
.
A simple way to get the AI to be this good is to make is by using the methods described by Russell and green_guy, taking them to the extreme, and evaluating the moves right until the end of the game. This is possible (as mentioned above), due the the very small ''size'' of the game.
What is effectively done is to try each possible move from the current position (like placing a X or O in each possible free position), and then simulate each of the possible moves the opposition could do from the resulting position. By doing this right to the end of the game (a full board), it is possible to determine a move (or selection of moves) from the current position that will definitely result in either a win, or a draw.
This way of doing things doesn''t require tactics as such, and is simply brute force. But if implemented correctly, should work just fine. If someone made a really really super computer (whose performance would have to be quite unimaginable), it would be possible to have a computer play chess perfectly, by using this method.
Hope that all makes sense...

A simple way to get the AI to be this good is to make is by using the methods described by Russell and green_guy, taking them to the extreme, and evaluating the moves right until the end of the game. This is possible (as mentioned above), due the the very small ''size'' of the game.
What is effectively done is to try each possible move from the current position (like placing a X or O in each possible free position), and then simulate each of the possible moves the opposition could do from the resulting position. By doing this right to the end of the game (a full board), it is possible to determine a move (or selection of moves) from the current position that will definitely result in either a win, or a draw.
This way of doing things doesn''t require tactics as such, and is simply brute force. But if implemented correctly, should work just fine. If someone made a really really super computer (whose performance would have to be quite unimaginable), it would be possible to have a computer play chess perfectly, by using this method.
Hope that all makes sense...
hi,
in 3x3 case you can do simple "learning algorythm" because the no of all possible cases is finite and resonably small.
Let start with DUMB computer player and as game proceeds store all moves - in such a case computer looses at first but comes to draw at the very end. Unfortunately it doesn''t seek for possible wins.
The more interesting problem is where you play on infinite board and try to score 5 in a row.
you gotta have more universal approach
can someone comment it?
Pet
in 3x3 case you can do simple "learning algorythm" because the no of all possible cases is finite and resonably small.
Let start with DUMB computer player and as game proceeds store all moves - in such a case computer looses at first but comes to draw at the very end. Unfortunately it doesn''t seek for possible wins.
The more interesting problem is where you play on infinite board and try to score 5 in a row.
you gotta have more universal approach
can someone comment it?
Pet
Pet
I recently made a 3D Tic-Tac-Toe game for a college class, and we had to implement 3 distinctly different AI difficulties. For the first (and easiest), the computer basically followed these steps:
1. Complete a row if all but one squares belong to the computer.
2. Block a player-owned 3-in-a-row if one exists.
3a. Pick a random, empty (or computer-controlled) "row" to persue and pick a point in that row.
3b. If a row is already chosen, make sure the human has not placed a piece in it. If he has not, pick a square in that row. If he has, use 3a.
For the medium difficulty, each square was given a value based on the number of potentially winnable rows it was in. For example, the center piece in a 2D board is in 4 potentially winnable rows (1 horizontal, 1 vertical, and 2 diagonal), corner squares are in 3, and edges are in 2. Agter the points are all added up, the square with the highest score is picked.
Uhhh...I have to run to work right now, so I''ll finish this later.
1. Complete a row if all but one squares belong to the computer.
2. Block a player-owned 3-in-a-row if one exists.
3a. Pick a random, empty (or computer-controlled) "row" to persue and pick a point in that row.
3b. If a row is already chosen, make sure the human has not placed a piece in it. If he has not, pick a square in that row. If he has, use 3a.
For the medium difficulty, each square was given a value based on the number of potentially winnable rows it was in. For example, the center piece in a 2D board is in 4 potentially winnable rows (1 horizontal, 1 vertical, and 2 diagonal), corner squares are in 3, and edges are in 2. Agter the points are all added up, the square with the highest score is picked.
Uhhh...I have to run to work right now, so I''ll finish this later.

Tolerance is a drug. Sycophancy is a disease.
The third AI strategy used the technique Miles suggested: A recursive function that plots all the possible moves, counter-moves, counter-counter-moves, etc. until the board is full. If you''re tricky, you can use this method to "trap" the human (which is simple for a 3x3 board, but much harder as more dimensions and planes are added). To avoid a draw, make the AI revert to the second, or even the first strategy if no "trap" is found.
Tolerance is a drug. Sycophancy is a disease.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement