Binary Nim
Write a GUI program to play Binary-Nim. Binary-Nim begins with three to eight piles of sticks, such that each pile contains at most 10 sticks. The number of piles and the number of sticks in each pile should be chosen randomly. Each pile may have a different number of sticks. At each turn, a player may remove any number of sticks, but only from a single pile. The player who removes the last stick wins.
The computer and player alternate turns until the game is over. A player should be given the choice of going fi rst or second after he/she sees the initial confi guration. When the game is over, the application should display a message stating who won. A running total of the number of games won and lost should be displayed in some area of the screen. After each game, a player may choose to quit or play again.
Random play is fi ne, but you won’t enjoy the game very much because the player can win too easily. There is a perfect but complex winning strategy that involves binary numbers. The data model for the perfect strategy uses a two-dimensional array with one row for each pile. Each row holds the digits (0’s and 1’s) of the binary number representing the number of sticks in that pile. The bits are right justifi ed. You might research this strategy and incorporate it into your program, or else devise your own strategy. Whatever you do, it is good style to separate the computer’s game strategy from the GUI.
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here