| 1 | n/a | """ turtle-example-suite: |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | tdemo_nim.py |
|---|
| 4 | n/a | |
|---|
| 5 | n/a | Play nim against the computer. The player |
|---|
| 6 | n/a | who takes the last stick is the winner. |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | Implements the model-view-controller |
|---|
| 9 | n/a | design pattern. |
|---|
| 10 | n/a | """ |
|---|
| 11 | n/a | |
|---|
| 12 | n/a | |
|---|
| 13 | n/a | import turtle |
|---|
| 14 | n/a | import random |
|---|
| 15 | n/a | import time |
|---|
| 16 | n/a | |
|---|
| 17 | n/a | SCREENWIDTH = 640 |
|---|
| 18 | n/a | SCREENHEIGHT = 480 |
|---|
| 19 | n/a | |
|---|
| 20 | n/a | MINSTICKS = 7 |
|---|
| 21 | n/a | MAXSTICKS = 31 |
|---|
| 22 | n/a | |
|---|
| 23 | n/a | HUNIT = SCREENHEIGHT // 12 |
|---|
| 24 | n/a | WUNIT = SCREENWIDTH // ((MAXSTICKS // 5) * 11 + (MAXSTICKS % 5) * 2) |
|---|
| 25 | n/a | |
|---|
| 26 | n/a | SCOLOR = (63, 63, 31) |
|---|
| 27 | n/a | HCOLOR = (255, 204, 204) |
|---|
| 28 | n/a | COLOR = (204, 204, 255) |
|---|
| 29 | n/a | |
|---|
| 30 | n/a | def randomrow(): |
|---|
| 31 | n/a | return random.randint(MINSTICKS, MAXSTICKS) |
|---|
| 32 | n/a | |
|---|
| 33 | n/a | def computerzug(state): |
|---|
| 34 | n/a | xored = state[0] ^ state[1] ^ state[2] |
|---|
| 35 | n/a | if xored == 0: |
|---|
| 36 | n/a | return randommove(state) |
|---|
| 37 | n/a | for z in range(3): |
|---|
| 38 | n/a | s = state[z] ^ xored |
|---|
| 39 | n/a | if s <= state[z]: |
|---|
| 40 | n/a | move = (z, s) |
|---|
| 41 | n/a | return move |
|---|
| 42 | n/a | |
|---|
| 43 | n/a | def randommove(state): |
|---|
| 44 | n/a | m = max(state) |
|---|
| 45 | n/a | while True: |
|---|
| 46 | n/a | z = random.randint(0,2) |
|---|
| 47 | n/a | if state[z] > (m > 1): |
|---|
| 48 | n/a | break |
|---|
| 49 | n/a | rand = random.randint(m > 1, state[z]-1) |
|---|
| 50 | n/a | return z, rand |
|---|
| 51 | n/a | |
|---|
| 52 | n/a | |
|---|
| 53 | n/a | class NimModel(object): |
|---|
| 54 | n/a | def __init__(self, game): |
|---|
| 55 | n/a | self.game = game |
|---|
| 56 | n/a | |
|---|
| 57 | n/a | def setup(self): |
|---|
| 58 | n/a | if self.game.state not in [Nim.CREATED, Nim.OVER]: |
|---|
| 59 | n/a | return |
|---|
| 60 | n/a | self.sticks = [randomrow(), randomrow(), randomrow()] |
|---|
| 61 | n/a | self.player = 0 |
|---|
| 62 | n/a | self.winner = None |
|---|
| 63 | n/a | self.game.view.setup() |
|---|
| 64 | n/a | self.game.state = Nim.RUNNING |
|---|
| 65 | n/a | |
|---|
| 66 | n/a | def move(self, row, col): |
|---|
| 67 | n/a | maxspalte = self.sticks[row] |
|---|
| 68 | n/a | self.sticks[row] = col |
|---|
| 69 | n/a | self.game.view.notify_move(row, col, maxspalte, self.player) |
|---|
| 70 | n/a | if self.game_over(): |
|---|
| 71 | n/a | self.game.state = Nim.OVER |
|---|
| 72 | n/a | self.winner = self.player |
|---|
| 73 | n/a | self.game.view.notify_over() |
|---|
| 74 | n/a | elif self.player == 0: |
|---|
| 75 | n/a | self.player = 1 |
|---|
| 76 | n/a | row, col = computerzug(self.sticks) |
|---|
| 77 | n/a | self.move(row, col) |
|---|
| 78 | n/a | self.player = 0 |
|---|
| 79 | n/a | |
|---|
| 80 | n/a | def game_over(self): |
|---|
| 81 | n/a | return self.sticks == [0, 0, 0] |
|---|
| 82 | n/a | |
|---|
| 83 | n/a | def notify_move(self, row, col): |
|---|
| 84 | n/a | if self.sticks[row] <= col: |
|---|
| 85 | n/a | return |
|---|
| 86 | n/a | self.move(row, col) |
|---|
| 87 | n/a | |
|---|
| 88 | n/a | |
|---|
| 89 | n/a | class Stick(turtle.Turtle): |
|---|
| 90 | n/a | def __init__(self, row, col, game): |
|---|
| 91 | n/a | turtle.Turtle.__init__(self, visible=False) |
|---|
| 92 | n/a | self.row = row |
|---|
| 93 | n/a | self.col = col |
|---|
| 94 | n/a | self.game = game |
|---|
| 95 | n/a | x, y = self.coords(row, col) |
|---|
| 96 | n/a | self.shape("square") |
|---|
| 97 | n/a | self.shapesize(HUNIT/10.0, WUNIT/20.0) |
|---|
| 98 | n/a | self.speed(0) |
|---|
| 99 | n/a | self.pu() |
|---|
| 100 | n/a | self.goto(x,y) |
|---|
| 101 | n/a | self.color("white") |
|---|
| 102 | n/a | self.showturtle() |
|---|
| 103 | n/a | |
|---|
| 104 | n/a | def coords(self, row, col): |
|---|
| 105 | n/a | packet, remainder = divmod(col, 5) |
|---|
| 106 | n/a | x = (3 + 11 * packet + 2 * remainder) * WUNIT |
|---|
| 107 | n/a | y = (2 + 3 * row) * HUNIT |
|---|
| 108 | n/a | return x - SCREENWIDTH // 2 + WUNIT // 2, SCREENHEIGHT // 2 - y - HUNIT // 2 |
|---|
| 109 | n/a | |
|---|
| 110 | n/a | def makemove(self, x, y): |
|---|
| 111 | n/a | if self.game.state != Nim.RUNNING: |
|---|
| 112 | n/a | return |
|---|
| 113 | n/a | self.game.controller.notify_move(self.row, self.col) |
|---|
| 114 | n/a | |
|---|
| 115 | n/a | |
|---|
| 116 | n/a | class NimView(object): |
|---|
| 117 | n/a | def __init__(self, game): |
|---|
| 118 | n/a | self.game = game |
|---|
| 119 | n/a | self.screen = game.screen |
|---|
| 120 | n/a | self.model = game.model |
|---|
| 121 | n/a | self.screen.colormode(255) |
|---|
| 122 | n/a | self.screen.tracer(False) |
|---|
| 123 | n/a | self.screen.bgcolor((240, 240, 255)) |
|---|
| 124 | n/a | self.writer = turtle.Turtle(visible=False) |
|---|
| 125 | n/a | self.writer.pu() |
|---|
| 126 | n/a | self.writer.speed(0) |
|---|
| 127 | n/a | self.sticks = {} |
|---|
| 128 | n/a | for row in range(3): |
|---|
| 129 | n/a | for col in range(MAXSTICKS): |
|---|
| 130 | n/a | self.sticks[(row, col)] = Stick(row, col, game) |
|---|
| 131 | n/a | self.display("... a moment please ...") |
|---|
| 132 | n/a | self.screen.tracer(True) |
|---|
| 133 | n/a | |
|---|
| 134 | n/a | def display(self, msg1, msg2=None): |
|---|
| 135 | n/a | self.screen.tracer(False) |
|---|
| 136 | n/a | self.writer.clear() |
|---|
| 137 | n/a | if msg2 is not None: |
|---|
| 138 | n/a | self.writer.goto(0, - SCREENHEIGHT // 2 + 48) |
|---|
| 139 | n/a | self.writer.pencolor("red") |
|---|
| 140 | n/a | self.writer.write(msg2, align="center", font=("Courier",18,"bold")) |
|---|
| 141 | n/a | self.writer.goto(0, - SCREENHEIGHT // 2 + 20) |
|---|
| 142 | n/a | self.writer.pencolor("black") |
|---|
| 143 | n/a | self.writer.write(msg1, align="center", font=("Courier",14,"bold")) |
|---|
| 144 | n/a | self.screen.tracer(True) |
|---|
| 145 | n/a | |
|---|
| 146 | n/a | def setup(self): |
|---|
| 147 | n/a | self.screen.tracer(False) |
|---|
| 148 | n/a | for row in range(3): |
|---|
| 149 | n/a | for col in range(self.model.sticks[row]): |
|---|
| 150 | n/a | self.sticks[(row, col)].color(SCOLOR) |
|---|
| 151 | n/a | for row in range(3): |
|---|
| 152 | n/a | for col in range(self.model.sticks[row], MAXSTICKS): |
|---|
| 153 | n/a | self.sticks[(row, col)].color("white") |
|---|
| 154 | n/a | self.display("Your turn! Click leftmost stick to remove.") |
|---|
| 155 | n/a | self.screen.tracer(True) |
|---|
| 156 | n/a | |
|---|
| 157 | n/a | def notify_move(self, row, col, maxspalte, player): |
|---|
| 158 | n/a | if player == 0: |
|---|
| 159 | n/a | farbe = HCOLOR |
|---|
| 160 | n/a | for s in range(col, maxspalte): |
|---|
| 161 | n/a | self.sticks[(row, s)].color(farbe) |
|---|
| 162 | n/a | else: |
|---|
| 163 | n/a | self.display(" ... thinking ... ") |
|---|
| 164 | n/a | time.sleep(0.5) |
|---|
| 165 | n/a | self.display(" ... thinking ... aaah ...") |
|---|
| 166 | n/a | farbe = COLOR |
|---|
| 167 | n/a | for s in range(maxspalte-1, col-1, -1): |
|---|
| 168 | n/a | time.sleep(0.2) |
|---|
| 169 | n/a | self.sticks[(row, s)].color(farbe) |
|---|
| 170 | n/a | self.display("Your turn! Click leftmost stick to remove.") |
|---|
| 171 | n/a | |
|---|
| 172 | n/a | def notify_over(self): |
|---|
| 173 | n/a | if self.game.model.winner == 0: |
|---|
| 174 | n/a | msg2 = "Congrats. You're the winner!!!" |
|---|
| 175 | n/a | else: |
|---|
| 176 | n/a | msg2 = "Sorry, the computer is the winner." |
|---|
| 177 | n/a | self.display("To play again press space bar. To leave press ESC.", msg2) |
|---|
| 178 | n/a | |
|---|
| 179 | n/a | def clear(self): |
|---|
| 180 | n/a | if self.game.state == Nim.OVER: |
|---|
| 181 | n/a | self.screen.clear() |
|---|
| 182 | n/a | |
|---|
| 183 | n/a | |
|---|
| 184 | n/a | class NimController(object): |
|---|
| 185 | n/a | |
|---|
| 186 | n/a | def __init__(self, game): |
|---|
| 187 | n/a | self.game = game |
|---|
| 188 | n/a | self.sticks = game.view.sticks |
|---|
| 189 | n/a | self.BUSY = False |
|---|
| 190 | n/a | for stick in self.sticks.values(): |
|---|
| 191 | n/a | stick.onclick(stick.makemove) |
|---|
| 192 | n/a | self.game.screen.onkey(self.game.model.setup, "space") |
|---|
| 193 | n/a | self.game.screen.onkey(self.game.view.clear, "Escape") |
|---|
| 194 | n/a | self.game.view.display("Press space bar to start game") |
|---|
| 195 | n/a | self.game.screen.listen() |
|---|
| 196 | n/a | |
|---|
| 197 | n/a | def notify_move(self, row, col): |
|---|
| 198 | n/a | if self.BUSY: |
|---|
| 199 | n/a | return |
|---|
| 200 | n/a | self.BUSY = True |
|---|
| 201 | n/a | self.game.model.notify_move(row, col) |
|---|
| 202 | n/a | self.BUSY = False |
|---|
| 203 | n/a | |
|---|
| 204 | n/a | |
|---|
| 205 | n/a | class Nim(object): |
|---|
| 206 | n/a | CREATED = 0 |
|---|
| 207 | n/a | RUNNING = 1 |
|---|
| 208 | n/a | OVER = 2 |
|---|
| 209 | n/a | def __init__(self, screen): |
|---|
| 210 | n/a | self.state = Nim.CREATED |
|---|
| 211 | n/a | self.screen = screen |
|---|
| 212 | n/a | self.model = NimModel(self) |
|---|
| 213 | n/a | self.view = NimView(self) |
|---|
| 214 | n/a | self.controller = NimController(self) |
|---|
| 215 | n/a | |
|---|
| 216 | n/a | |
|---|
| 217 | n/a | def main(): |
|---|
| 218 | n/a | mainscreen = turtle.Screen() |
|---|
| 219 | n/a | mainscreen.mode("standard") |
|---|
| 220 | n/a | mainscreen.setup(SCREENWIDTH, SCREENHEIGHT) |
|---|
| 221 | n/a | nim = Nim(mainscreen) |
|---|
| 222 | n/a | return "EVENTLOOP" |
|---|
| 223 | n/a | |
|---|
| 224 | n/a | if __name__ == "__main__": |
|---|
| 225 | n/a | main() |
|---|
| 226 | n/a | turtle.mainloop() |
|---|