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