Search |
||||||||||||
Java Tech: An Intelligent Nim Computer Game, Part 1
Tue, 2004-05-18
Many developers like to play computer games. They entertain us and help reduce the stress caused by our jobs. Have you ever wanted to create your own version of a computer game? I have. This Java Tech article begins a two-part series on developing two versions of an intelligent computer game, Nim. In this article, you learn how to play Nim, and discover tools for creating an intelligent computer player. In the next article, you apply those tools to the creation of that player, while building console and GUI Nim games. As you create the GUI version, you'll examine a technique for dragging and dropping game objects. Introducing NimThe game of Nim typically involves two players and a pile of matches, stones, marbles, or some other kind of objects. Each player alternately makes a move by taking one, two, or three objects from the pile. The player who takes the last object(s) loses -- and the other player wins. For example, suppose there's a pile of five matches. Player A takes two matches, leaving three. Player B then takes two matches, leaving one. Player A must take the final match, and loses. Player B wins. Note: The paragraph above describes one way to play Nim; I use that technique in this article. To learn about other ways to play Nim, and to find out where that name comes from, consult the Wikipedia entry for Nim. An Intelligent Computer PlayerWe could create a Nim computer game that requires two human players. However, if one player was absent, the game would certainly lose its appeal. To solve that problem, we will design a computer player to challenge the human player. In a nutshell, the computer player will be intelligent. How do we create an intelligent computer player? For starters, we need to know a little game theory. According to game theory, Nim is an example of a zero-sum game of perfect information. (Chess, tic-tac-toe, Othello, and checkers are other examples.) "Zero-sum" means that the interests of the players are exactly opposed. Regardless of the game's outcome, the winnings of one player are exactly balanced by the losses of the other(s). For example, only one player wins and only one player loses in a two-player Nim game. "Perfect information" means that, at every move, each player knows all of the moves that have already been made. For example, each player in a two-player Nim game knows all moves made by the other player, along with the player's own moves. Zero-sum games of perfect information imply that there exists a best strategy for each player, to help that player win or to minimize that player's loss. A pair of tools help intelligent computer players find that best strategy: game trees and the minimax algorithm. Game TreesA game tree describes all possible moves, via its branches, and resulting game configurations, via its nodes, in a two-player game. The root node represents the initial game configuration and the leaf nodes represent the terminal configurations: win, lose, or draw. There is only one terminal configuration in Nim -- no matches are left in the pile. That configuration represents a win for Player A if Player B makes the last move, or a win for Player B if Player A makes the last move. Figure 1 reveals a game tree for a two-player Nim game, with an initial game configuration specifying four matches.
In Figure 1, square pink nodes represent Player A, round green nodes represent Player B, and triangular blue nodes represent the terminal configuration -- a win for Player A or Player B, depending on the parent node (green or pink, respectively). Each branch has a numeric label that indicates how many matches have been taken from the pile, and each node has a numeric label indicating how many matches remain in the pile. The pink square node with numeric label 4 is the root node, and indicates that the pile initially contains four matches and that Player A makes the first move. Suppose Player A takes one match from the pile. The game tree displays this move via the branch (with label 1) from the root node to the Player B node with the 3 label. Now suppose Player B counters Player A's move by taking two matches from the pile. The game tree reveals this move via the branch (with label 2) from the Player B node with the 3 label to the Player A node with label 1. Player A has no choice but to take the final match, and Player B wins. The game tree presents this move via the branch (labeled 1) from the Player A node with label 1 to the terminal configuration node directly below. We can easily create a game tree that completely describes a Nim game, with an initial game configuration that specifies n matches. To accomplish that task, we use both a
Each
We can combine the Storing an entire game tree's nodes in memory is not practical for large game trees -- thousands or millions of nodes. However, with an initial pile of four (or even 11) matches, the resulting Nim game tree can be completely stored in memory. Minimax AlgorithmThe minimax algorithm determines a player's optimal move by assigning a number to each node that is an immediate child of the player's node. The optimal move for Player A is to follow the branch to the immediate child node with the maximum number. In a similar fashion, the optimal move for Player B is to follow the branch to the immediate child node with the minimum number. Although the optimal move doesn't guarantee a win for the player, it indicates the best possible outcome that the player can hope to achieve. Node numbers are first determined at the terminal configuration node level. An evaluation function calculates these numbers. In Nim, this function is simple: return 1 if a terminal configuration node indicates Player A to be the winner, or -1 if Player B is shown to be the winner. Moving up one level, minimax then determines either the maximum or the minimum of all child numbers -- maximum if the parent node represents Player A's turn, or minimum if the parent node represents Player B's turn -- and assigns the result to the parent node. With each level that minimax moves up, it alternately determines the maximum or minimum prior to the assignment (which is how minimax gets its name). Figure 2 illustrates minimax being applied to Figure 1's game tree.
In Figure 2, suppose the current node is the pink square node that contains 1, and is located two levels down and on the left side. That node indicates that it is Player A's turn to make a move. Should Player A take 1 match or 2? Here is what minimax determines: The leftmost terminal configuration node (at the lowest level) is assigned 1 because that node indicates a win for Player A -- Player B has just removed the last match and loses. Minimax assigns that value as the minimum to the parent Player B node. The terminal configuration node to the right of (and at the same level as) the Player B node (which contains 1) is assigned -1, because it indicates a win for Player B -- Player A has just removed the last two matches and loses. The maximum of 1 and -1 then assigns to the Player A parent node, which means Player A's optimal move is to take one match. Earlier, you saw how Java was used to create a game tree. You will now see how Java is used to implement the minimax algorithm to search the game tree for Player A's optimal opening move. Examine the following code fragment, taken from the
After building the game tree (shown in Figures 1 and 2), the code fragment invokes the Note: The observant reader will notice something strange about the code fragment: What does
The If the parent node's ConclusionComputer games are entertaining and can reduce stress. If you have ever wanted to create your own version of a computer game, the simplicity of Nim makes it an excellent choice. In this article, after learning how to play Nim, you discovered game trees and the minimax algorithm for creating an intelligent computer player. As usual, there is some homework for you to accomplish:
Next month's Java Tech creates console-based and GUI-based Nim computer games. Each game applies this article's knowledge to its intelligent computer player. Answers to Previous HomeworkThe previous Java Tech article presented you with some challenging homework on variable arguments. Let's revisit that homework and investigate solutions. Problem 1 Solution Problem 2 Solution »
Related Topics >>
Programming
Comments
Comments are listed in date ascending order (oldest first)
|
||||||||||||
|