|
First Summer 2008 |
import random
class Nim(object):
def __init__(self, height):
self.height = height
self.player = 0
self.names = ["user", "computer"]
self.scores = {}
self.scores["user"] = 0
self.scores["computer"] = 0
def move(self):
if self.height == 1:
print self.names[self.player], "has lost the game."
winner = self.names[(self.player+1)%2]
self.scores[winner] += 1
self.player = 0
self.height = int(raw_input("New height for new game:"))
else:
if self.player == 0:
user = int(raw_input("How many? "))
if user > 0 and user <= self.height / 2:
self.height -= user
else:
print self.names[self.player], "has lost the game."
winner = self.names[(self.player+1)%2]
self.scores[winner] += 1
self.player = 0
self.height = int(raw_input("New height for new game:"))
else:
comp = random.randrange(1, self.height/2+1)
print "The computer chooses", comp
self.height -= comp
self.player = (self.player + 1) % 2
self.report()
def report(self):
print "The height is now:", self.height
print "The",self.names[self.player],"moves."