jpayne@69: """ turtle-example-suite: jpayne@69: jpayne@69: tdemo_nim.py jpayne@69: jpayne@69: Play nim against the computer. The player jpayne@69: who takes the last stick is the winner. jpayne@69: jpayne@69: Implements the model-view-controller jpayne@69: design pattern. jpayne@69: """ jpayne@69: jpayne@69: jpayne@69: import turtle jpayne@69: import random jpayne@69: import time jpayne@69: jpayne@69: SCREENWIDTH = 640 jpayne@69: SCREENHEIGHT = 480 jpayne@69: jpayne@69: MINSTICKS = 7 jpayne@69: MAXSTICKS = 31 jpayne@69: jpayne@69: HUNIT = SCREENHEIGHT // 12 jpayne@69: WUNIT = SCREENWIDTH // ((MAXSTICKS // 5) * 11 + (MAXSTICKS % 5) * 2) jpayne@69: jpayne@69: SCOLOR = (63, 63, 31) jpayne@69: HCOLOR = (255, 204, 204) jpayne@69: COLOR = (204, 204, 255) jpayne@69: jpayne@69: def randomrow(): jpayne@69: return random.randint(MINSTICKS, MAXSTICKS) jpayne@69: jpayne@69: def computerzug(state): jpayne@69: xored = state[0] ^ state[1] ^ state[2] jpayne@69: if xored == 0: jpayne@69: return randommove(state) jpayne@69: for z in range(3): jpayne@69: s = state[z] ^ xored jpayne@69: if s <= state[z]: jpayne@69: move = (z, s) jpayne@69: return move jpayne@69: jpayne@69: def randommove(state): jpayne@69: m = max(state) jpayne@69: while True: jpayne@69: z = random.randint(0,2) jpayne@69: if state[z] > (m > 1): jpayne@69: break jpayne@69: rand = random.randint(m > 1, state[z]-1) jpayne@69: return z, rand jpayne@69: jpayne@69: jpayne@69: class NimModel(object): jpayne@69: def __init__(self, game): jpayne@69: self.game = game jpayne@69: jpayne@69: def setup(self): jpayne@69: if self.game.state not in [Nim.CREATED, Nim.OVER]: jpayne@69: return jpayne@69: self.sticks = [randomrow(), randomrow(), randomrow()] jpayne@69: self.player = 0 jpayne@69: self.winner = None jpayne@69: self.game.view.setup() jpayne@69: self.game.state = Nim.RUNNING jpayne@69: jpayne@69: def move(self, row, col): jpayne@69: maxspalte = self.sticks[row] jpayne@69: self.sticks[row] = col jpayne@69: self.game.view.notify_move(row, col, maxspalte, self.player) jpayne@69: if self.game_over(): jpayne@69: self.game.state = Nim.OVER jpayne@69: self.winner = self.player jpayne@69: self.game.view.notify_over() jpayne@69: elif self.player == 0: jpayne@69: self.player = 1 jpayne@69: row, col = computerzug(self.sticks) jpayne@69: self.move(row, col) jpayne@69: self.player = 0 jpayne@69: jpayne@69: def game_over(self): jpayne@69: return self.sticks == [0, 0, 0] jpayne@69: jpayne@69: def notify_move(self, row, col): jpayne@69: if self.sticks[row] <= col: jpayne@69: return jpayne@69: self.move(row, col) jpayne@69: jpayne@69: jpayne@69: class Stick(turtle.Turtle): jpayne@69: def __init__(self, row, col, game): jpayne@69: turtle.Turtle.__init__(self, visible=False) jpayne@69: self.row = row jpayne@69: self.col = col jpayne@69: self.game = game jpayne@69: x, y = self.coords(row, col) jpayne@69: self.shape("square") jpayne@69: self.shapesize(HUNIT/10.0, WUNIT/20.0) jpayne@69: self.speed(0) jpayne@69: self.pu() jpayne@69: self.goto(x,y) jpayne@69: self.color("white") jpayne@69: self.showturtle() jpayne@69: jpayne@69: def coords(self, row, col): jpayne@69: packet, remainder = divmod(col, 5) jpayne@69: x = (3 + 11 * packet + 2 * remainder) * WUNIT jpayne@69: y = (2 + 3 * row) * HUNIT jpayne@69: return x - SCREENWIDTH // 2 + WUNIT // 2, SCREENHEIGHT // 2 - y - HUNIT // 2 jpayne@69: jpayne@69: def makemove(self, x, y): jpayne@69: if self.game.state != Nim.RUNNING: jpayne@69: return jpayne@69: self.game.controller.notify_move(self.row, self.col) jpayne@69: jpayne@69: jpayne@69: class NimView(object): jpayne@69: def __init__(self, game): jpayne@69: self.game = game jpayne@69: self.screen = game.screen jpayne@69: self.model = game.model jpayne@69: self.screen.colormode(255) jpayne@69: self.screen.tracer(False) jpayne@69: self.screen.bgcolor((240, 240, 255)) jpayne@69: self.writer = turtle.Turtle(visible=False) jpayne@69: self.writer.pu() jpayne@69: self.writer.speed(0) jpayne@69: self.sticks = {} jpayne@69: for row in range(3): jpayne@69: for col in range(MAXSTICKS): jpayne@69: self.sticks[(row, col)] = Stick(row, col, game) jpayne@69: self.display("... a moment please ...") jpayne@69: self.screen.tracer(True) jpayne@69: jpayne@69: def display(self, msg1, msg2=None): jpayne@69: self.screen.tracer(False) jpayne@69: self.writer.clear() jpayne@69: if msg2 is not None: jpayne@69: self.writer.goto(0, - SCREENHEIGHT // 2 + 48) jpayne@69: self.writer.pencolor("red") jpayne@69: self.writer.write(msg2, align="center", font=("Courier",18,"bold")) jpayne@69: self.writer.goto(0, - SCREENHEIGHT // 2 + 20) jpayne@69: self.writer.pencolor("black") jpayne@69: self.writer.write(msg1, align="center", font=("Courier",14,"bold")) jpayne@69: self.screen.tracer(True) jpayne@69: jpayne@69: def setup(self): jpayne@69: self.screen.tracer(False) jpayne@69: for row in range(3): jpayne@69: for col in range(self.model.sticks[row]): jpayne@69: self.sticks[(row, col)].color(SCOLOR) jpayne@69: for row in range(3): jpayne@69: for col in range(self.model.sticks[row], MAXSTICKS): jpayne@69: self.sticks[(row, col)].color("white") jpayne@69: self.display("Your turn! Click leftmost stick to remove.") jpayne@69: self.screen.tracer(True) jpayne@69: jpayne@69: def notify_move(self, row, col, maxspalte, player): jpayne@69: if player == 0: jpayne@69: farbe = HCOLOR jpayne@69: for s in range(col, maxspalte): jpayne@69: self.sticks[(row, s)].color(farbe) jpayne@69: else: jpayne@69: self.display(" ... thinking ... ") jpayne@69: time.sleep(0.5) jpayne@69: self.display(" ... thinking ... aaah ...") jpayne@69: farbe = COLOR jpayne@69: for s in range(maxspalte-1, col-1, -1): jpayne@69: time.sleep(0.2) jpayne@69: self.sticks[(row, s)].color(farbe) jpayne@69: self.display("Your turn! Click leftmost stick to remove.") jpayne@69: jpayne@69: def notify_over(self): jpayne@69: if self.game.model.winner == 0: jpayne@69: msg2 = "Congrats. You're the winner!!!" jpayne@69: else: jpayne@69: msg2 = "Sorry, the computer is the winner." jpayne@69: self.display("To play again press space bar. To leave press ESC.", msg2) jpayne@69: jpayne@69: def clear(self): jpayne@69: if self.game.state == Nim.OVER: jpayne@69: self.screen.clear() jpayne@69: jpayne@69: jpayne@69: class NimController(object): jpayne@69: jpayne@69: def __init__(self, game): jpayne@69: self.game = game jpayne@69: self.sticks = game.view.sticks jpayne@69: self.BUSY = False jpayne@69: for stick in self.sticks.values(): jpayne@69: stick.onclick(stick.makemove) jpayne@69: self.game.screen.onkey(self.game.model.setup, "space") jpayne@69: self.game.screen.onkey(self.game.view.clear, "Escape") jpayne@69: self.game.view.display("Press space bar to start game") jpayne@69: self.game.screen.listen() jpayne@69: jpayne@69: def notify_move(self, row, col): jpayne@69: if self.BUSY: jpayne@69: return jpayne@69: self.BUSY = True jpayne@69: self.game.model.notify_move(row, col) jpayne@69: self.BUSY = False jpayne@69: jpayne@69: jpayne@69: class Nim(object): jpayne@69: CREATED = 0 jpayne@69: RUNNING = 1 jpayne@69: OVER = 2 jpayne@69: def __init__(self, screen): jpayne@69: self.state = Nim.CREATED jpayne@69: self.screen = screen jpayne@69: self.model = NimModel(self) jpayne@69: self.view = NimView(self) jpayne@69: self.controller = NimController(self) jpayne@69: jpayne@69: jpayne@69: def main(): jpayne@69: mainscreen = turtle.Screen() jpayne@69: mainscreen.mode("standard") jpayne@69: mainscreen.setup(SCREENWIDTH, SCREENHEIGHT) jpayne@69: nim = Nim(mainscreen) jpayne@69: return "EVENTLOOP" jpayne@69: jpayne@69: if __name__ == "__main__": jpayne@69: main() jpayne@69: turtle.mainloop()