Question
python: Create a NimGame in class that implements the simplest form of two-player Nim-style games (http://en.wikipedia.org/wiki/Nim), sometimes called the subtraction game. The game starts with
python: Create a NimGame in class that implements the simplest form of two-player Nim-style games (http://en.wikipedia.org/wiki/Nim), sometimes called the "subtraction game." The game starts with some specific number of items - pebbles, coins, matchsticks, balls. Players take turns removing between 1 and 3 items. The player who removes the last item loses. You can play an interactive on-line version of the game here:http://upload.wikimedia.org/wikipedia/commons/4/4d/Subtraction_game_SMIL.svg assume there's one human player playing against the computer and that the human always goes first.
To create the NimGame class, you only need to implement two methods:
an __init__ method, with an argument specifying how many items (we'll call them balls) the game should start with
a take method, with an argument specifying how many balls the human player wishes to remove. This method should check that the specified number of balls is valid and, if so, remove that many balls, and check whether or not the player has lost. Next, it should automatically select a number of balls for the computer to remove, and update the game state accordingly (and check whether the computer has lost).
A sample use of the class might produce these results:
>>> game = NimGame(7) Nim game initialized with 7 balls. >>> game.take(2) You took 2 balls. 5 remain. Computer took 3 balls. 2 remain. >>> game.take(2) You took 2 balls. 0 remain. Computer wins!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started