Understanding the game tree[edit]

With tic-tac-toe the task is to attain a straight line horizontally, vertically or diagonally on a diagram of two vertical and two horizontal lines intersecting at right angles. The total available space for the symbols is equal to nine and the symbols used are usually 'x' for one player and 'o' for the other. This is the sixth in a series where a multi-player Tic Tac Toe (or Noughts and Crosses) game will be built using MySQL and PHP technologies. This is a walk-t.

A YC Data Flow Diagram showing DFD Tic Tac Toe level0. You can edit this YC Data Flow Diagram using Creately diagramming tool and include in your report/presentation/website. Homework 4 - Tic-Tac-Toe Game Sequence Diagrams CSCI 360 Create System Sequence Diagrams (SSDs) and operation contracts for the tic tac toe game. You should try to create as many sequence diagrams you think are required for your game design. Add a short description for each sequence diagram. Include only a sample of five operation contracts.

To better understand the game tree, it can be thoughts of as a technique for analyzing adversarial games, which determine the actions that player takes to win the game. In game theory, a game tree is a directed graph whose nodes are positions in a game (eg, the arrangement of the pieces in a board game) and whose edges are moves (eg, to move pieces from one position on a board to another).[1]

The complete game tree for a game is the game tree starting at the initial position and containing all possible moves from each position; the complete tree is the same tree as that obtained from the extensive-form game representation. To be more specific, the complete game is a norm for the game in game theory. Which can clearly express many important aspects. For example, the sequence of actions that stakeholders may take, their choices at each decision point, information about actions taken by other stakeholders when each stakeholder makes a decision, and the benefits of all possible game results.[2]

The first two plies of the game tree for tic-tac-toe.

The diagram shows the first two levels, or plies, in the game tree for tic-tac-toe. The rotations and reflections of positions are equivalent, so the first player has three choices of move: in the center, at the edge, or in the corner. The second player has two choices for the reply if the first player played in the center, otherwise five choices. And so on.

The number of leaf nodes in the complete game tree is the number of possible different ways the game can be played. For example, the game tree for tic-tac-toe has 255,168 leaf nodes.

Game trees are important in artificial intelligence because one way to pick the best move in a game is to search the game tree using any of numerous tree search algorithms, combined with minimax-like rules to prune the tree. The game tree for tic-tac-toe is easily searchable, but the complete game trees for larger games like chess are much too large to search. Instead, a chess-playing program searches a partial game tree: typically as many plies from the current position as it can search in the time available. Except for the case of 'pathological' game trees[3] (which seem to be quite rare in practice), increasing the search depth (i.e., the number of plies searched) generally improves the chance of picking the best move.

Two-person games can also be represented as and-or trees. For the first player to win a game, there must exist a winning move for all moves of the second player. This is represented in the and-or tree by using disjunction to represent the first player's alternative moves and using conjunction to represent all of the second player's moves.

Solving game trees[edit]

Deterministic algorithm version[edit]

An arbitrary game tree that has been fully colored

With a complete game tree, it is possible to 'solve' the game – that is to say, find a sequence of moves that either the first or second player can follow that will guarantee the best possible outcome for that player (usually a win or a tie). The algorithm (which is generally called backward induction or retrograde analysis) can be described recursively as follows.

Tic Tac Toe Game online, free

  1. Color the final ply of the game tree so that all wins for player 1 are colored one way (Blue in the diagram), all wins for player 2 are colored another way (Red in the diagram), and all ties are colored a third way (Grey in the diagram).
  2. Look at the next ply up. If there exists a node colored opposite as the current player, color this node for that player as well. If all immediately lower nodes are colored for the same player, color this node for the same player as well. Otherwise, color this node a tie.
  3. Repeat for each ply, moving upwards, until all nodes are colored. The color of the root node will determine the nature of the game.
Sequence Diagram For Tic Tac Toe Game

The diagram shows a game tree for an arbitrary game, colored using the above algorithm.

It is usually possible to solve a game (in this technical sense of 'solve') using only a subset of the game tree, since in many games a move need not be analyzed if there is another move that is better for the same player (for example alpha-beta pruning can be used in many deterministic games).

Any subtree that can be used to solve the game is known as a decision tree, and the sizes of decision trees of various shapes are used as measures of game complexity.[4]

Randomized algorithms version[edit]

Randomized algorithms can be used in solving game trees. There are two main advantages in this type of implementation: speed and practicality. Whereas a deterministic version of solving game trees can be done in Ο(n), the following randomized algorithm has an expected run time of θ(n0.792) if every node in the game tree has degree 2. Moreover, it is practical because randomized algorithms are capable of 'foiling an enemy', meaning an opponent cannot beat the system of game trees by knowing the algorithm used to solve the game tree because the order of solving is random.

The following is an implementation of randomized game tree solution algorithm:[5]

The algorithm makes use of the idea of 'short-circuiting': if the root node is considered an 'OR' operator, then once one True is found, the root is classified as True; conversely, if the root node is considered an 'AND' operator then once one False is found, the root is classified as False.

Mini–max game-tree search[edit]

In this algorithm, it is first verified that the constant MaxDepth. Which represents the maximum level of tree search is given. The current state of the board can be represented by the root node of the tree. All available moves are considered to be from this node. The player searches for the maximum value of the cost function 'estimate', while the opponent calculates its minimum.[6]

See also[edit]


  1. ^Zuckerman, Inon; Wilson, Brandon; Nau, Dana S. (2018). 'Avoiding game-tree pathology in 2-player adversarial search'. Computational Intelligence. 34 (2): 542–561. doi:10.1111/coin.12162. ISSN1467-8640.
  2. ^'A novel optimization model based on game tree for multi-energy conversion systems'. Energy. 150: 109–121. 2018-05-01. doi:10.1016/j.energy.2018.02.091. ISSN0360-5442.
  3. ^Nau, Dana (1982). 'An investigation of the causes of pathology in games'. Artificial Intelligence. 19: 257–278. doi:10.1016/0004-3702(82)90002-9.
  4. ^Victor Allis (1994). Searching for Solutions in Games and Artificial Intelligence(PDF). Ph.D. Thesis, University of Limburg, Maastricht, The Netherlands. ISBN90-900748-8-0.
  5. ^Daniel Roche (2013). SI486D: Randomness in Computing, Game Trees Unit. United States Naval Academy, Computer Science Department.
  6. ^Pekař, Libor; Matušů, Radek; Andrla, Jiří; Litschmannová, Martina (September 2020). 'Review of Kalah Game Research and the Proposition of a Novel Heuristic–Deterministic Algorithm Compared to Tree-Search Solutions and Human Decision-Making'. Informatics. 7 (3): 34. doi:10.3390/informatics7030034.

Further reading[edit]

  • Hu, Te Chiang; Shing, Man-tak (2002). Combinatorial Algorithms. Courier Dover Publications. ISBN0-486-41962-2. Retrieved 2007-04-02.
  • Judea Pearl, Heuristics, Addison-Wesley, 1984
Retrieved from 'https://en.wikipedia.org/w/index.php?title=Game_tree&oldid=1021302598'

Zero Sum Game

If you know what you are doing, you can't lose at Tic-Tac-Toe. If your opponent knows what they are doing, you can't win at Tic-Tac-Toe. The game is a zero sum game. If both players are playing with an optimal strategy, every game will end in a tie.

Surpisingly few people know optimal Tic-Tac-Toe stategy. Yes there are some people (and computers) that you will never beat, but they are relatively rare. Knowing this, you can become a Tic-Tac-Toe master.

Before getting started, open a Tic-Tac-Toe playing board in a new browser window so that you can experiment while you read this article.


Sequence Diagram For Tic Tac Toe Game

There are four player types in Tic-Tac-Toe.

  • The Novice player makes random moves
  • The Intermediate player will blocks their opponent from winning
  • The Experienced player knows that playing in certain first squares will lose the game
  • The Expert player will never lose

Pitting these players against each other, you can see that in all cases, the better player wins more.

Player Two
Player OneNovice1 wins: 57.1%
2 wins: 30.6%
Ties: 12.3%
1 wins: 6.40%
2 wins: 68.3%
Ties: 25.3%
1 wins: 2.60%
2 wins: 76.4%
Ties: 21.0%
1 wins: 0.00%
2 wins: 79.6%
Ties: 20.4%
Intermediate1 wins: 90.4%
2 wins: 1.60%
Ties: 8.00%
1 wins: 31.6%
2 wins: 17.1%
Ties: 51.3%
1 wins: 16.1%
2 wins: 10.3%
Ties: 73.6%
1 wins: 0.00%
2 wins: 16.1%
Ties: 83.9%
Experienced1 wins: 90.8%
2 wins: .700%
Ties: 8.50%
1 wins: 35.5%
2 wins: 11.7%
Ties: 52.8%
1 wins: 13.3%
2 wins: .800%
Ties: 85.9%
1 wins: 0.00%
2 wins: 1.70%
Ties: 98.3%
Expert1 wins: 97.8%
2 wins: 0.00%
Ties: 2.20%
1 wins: 76.6%
2 wins: 0.00%
Ties: 23.4%
1 wins: 27.1%
2 wins: 0.00%
Ties: 72.9%
1 wins: 0.00%
2 wins: 0.00%
Ties: 100.%

Expert - The player that can't lose

On the Tic-Tac-Toe game board that you have opened in a new window, choose the 'Expert' type for each player and hit 'New Game'. Verify that the result of each game is a tie (cat's game).

Hard tic tac toe game
Cat's Game

Neither of the computer players can be beaten because they are playing as well as game can be played. The computer does this by playing out every single game of Tic-Tac-Toe ahead of time and figuring out which moves are good and which are bad. The computer can do this because there are not a lot of possible games. The first move can be played in any of nine squares, the second in any of eight squares, the third in any of seven squares and so on. That means there are at most nine factorial or 362,880 possible games. A computer can breeze though 400,000 games in a blink of an eye. In reality, it has to play far fewer games than that. There are only 125,168 games of Tic-Tac-Toe because somebody wins most of them before all off the squares have been filled. The Tic-Tac-Toe is symetrical and if the computer can realize that many games are the same because it could rotate the board, there are even fewer games than that. Suffice to say that computers win through brute force.

So how can a human become unbeatable in Tic-Tac-Toe? A human doesn't even have time to play out one hundred games in the mind and still make a move in a reasonable amount of time. However, a human can compensate with experience and reasoning.

A player that moves randomly will not see that an opponent should be blocked.

Novice - Stupid random play

Everybody can beat the 'Novice' player virtually every game. Set one player to Novice and the other to human and observe how easy it is to beat the novice.

The novice simply places its mark in any empty square. This stategy is very poor and almost never wins.

A reactionary player will block an opponent's win.
Typical statistics after two intermediate players play many games.

Intermediate - reactionary play

Most Tic-Tac-Toe players start off as reactionary players. Reactionary players will block their opponents three in a row, or take any three in a row that they can. Otherwise, they play like a novice and choose random moves. This style of play is what the 'Intermediate' computer player uses.

Experienced - knows how to start

Edup home clock. The experienced player knows the best starting moves. The stategies below explain these moves in detail.


Move First

If two intermediate players play many games. The player that goes first will win about twice as often as the player that goes second. Verify this for yourself by pitting two intermediate players against each other and watching the stats as you have them play many new games.

Tic Tac Toe

On some level this makes sense. There are only nine squares on a Tic-Tac-Toe board, the first player will get five of them but the second player will only get four.

When two experts play, the game always ends in a tie. In all other cases, the player that goes first wins far more than they would have if their opponent had gone first.

Know the bad first moves

Safe moves for player 1's first move

Player 1

If you are going first, know the safe first moves. The trick is to avoid the edges. The corners and the center are safe moves:

Player 2

There are two possibilities. Either player 1 took the corner, or the center.

Safe moves for player 2's first move (player 1 in center)
Safe moves for player 2's first move (player 1 in corner)

Best moves for player 1's first move

Player 1 can be ruthless

If player 1 moves in the corner for the first move, player 2 must take the center. If player 1 is playing against a novice, player 1 can be ruthless and always play in the corner first. That leaves a lot of board for novice to choose from and player 1 will win more often.

Become an expert

Tic Tac Toe Board Game

The first moves (or opening book) are the hardest to figure out. Beyond the first move, it doesn't take much practice to move from being an experienced player to being an expert player. One good way to go about it is to play the fool against a computer expert player and see how you get beaten. Try known bad first moves and see how the compuer can outwit you every time. Beyond this point, I leave becoming an expert as an exercise to the reader.