Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(Python) Create a NimGame class that implements the simplest form of two-player

ID: 3683705 • Letter: #

Question

(Python) Create a NimGame 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.

For this assignment, we'll keep things very simple. We'll 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:

1. an __init__ method, with an argument specifying how many items (we'll call them balls) the game should start with

2. 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).

Explanation / Answer

class Player():
    def __init__(self, person, ball, game):
        global balls
        self.posx = 300
        self.velo = 0
        self.gameover = False
        self.pearson = person
        self.ball = ball
        self.game = game

class Game():
    global balls
    def __init__(self):
        self.person = Person()
        self.ball = Ball()
        self.player = Player(self.person, self.ball, self)
    def spBall(self):
        for i in range(10):
            balls.append(self.ball)
    def move(self):
        pass
    def gameover(self):
        pass