|
| 1 | +# 10 gobang |
| 2 | + |
| 3 | +## Requirements |
| 4 | + |
| 5 | +1. Run the code in console using command line. |
| 6 | +2. It'll open a Python window to let 2 players play gobang game. |
| 7 | + |
| 8 | +## What will we practice in this project? |
| 9 | + |
| 10 | +- turtle draw |
| 11 | +- mouse click event handle |
| 12 | +- 2d array |
| 13 | +- list |
| 14 | +- exception handle |
| 15 | +- function |
| 16 | + |
| 17 | +## A reference code |
| 18 | + |
| 19 | +```python |
| 20 | +from turtle import * |
| 21 | + |
| 22 | +win = False |
| 23 | + |
| 24 | +speed(0) |
| 25 | +bgcolor("lightgreen") |
| 26 | +yanse="black" |
| 27 | +gz=40 |
| 28 | + |
| 29 | +judge = Turtle() |
| 30 | +judge.up() |
| 31 | +judge.goto(-460, 330) |
| 32 | +judge.write("Next", font=("Arial", 40, "bold")) |
| 33 | +judge.color(yanse) |
| 34 | +judge.goto(-420, 300) |
| 35 | +judge.dot(30) |
| 36 | + |
| 37 | +for i in range(19): |
| 38 | + up() |
| 39 | + goto(-gz*9, gz*(9-i)) |
| 40 | + down() |
| 41 | + fd(gz*18) |
| 42 | + bk(gz*18) |
| 43 | + |
| 44 | +rt(90) |
| 45 | + |
| 46 | +for i in range(19): |
| 47 | + up() |
| 48 | + goto(-gz*(9-i), gz*9) |
| 49 | + down() |
| 50 | + fd(gz*18) |
| 51 | + bk(gz*18) |
| 52 | + |
| 53 | +pensize(5) |
| 54 | +for i in range(4): |
| 55 | + fd(gz*18) |
| 56 | + rt(90) |
| 57 | + |
| 58 | +# m = [[0] * 19 for i in range(19)] |
| 59 | +m =[ |
| 60 | + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], |
| 61 | + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], |
| 62 | + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], |
| 63 | + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], |
| 64 | + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], |
| 65 | + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], |
| 66 | + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], |
| 67 | + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], |
| 68 | + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], |
| 69 | + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], |
| 70 | + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], |
| 71 | + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], |
| 72 | + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], |
| 73 | + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], |
| 74 | + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], |
| 75 | + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], |
| 76 | + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], |
| 77 | + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], |
| 78 | + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] |
| 79 | +] |
| 80 | + |
| 81 | +def check(i, j): |
| 82 | + global win |
| 83 | + g = [0] * 8 |
| 84 | + fw = ((0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1)) |
| 85 | + for index in range(8): |
| 86 | + d = fw[index] |
| 87 | + next_i = i + d[0] |
| 88 | + next_j = j + d[1] |
| 89 | + while next_i in range(19) and next_j in range(19) and m[next_i][next_j] == m[i][j]: |
| 90 | + g[index] = g[index] + 1 |
| 91 | + next_i = next_i + d[0] |
| 92 | + next_j = next_j + d[1] |
| 93 | + |
| 94 | + for index in range(4): |
| 95 | + if g[index] + g[index + 4] + 1 >= 5: |
| 96 | + win = True |
| 97 | + goto(0, 0) |
| 98 | + if yanse == "black": |
| 99 | + write('Black Win', font=('Arial', 100, ''), align='center') |
| 100 | + else: |
| 101 | + write('White Win', font=('Arial', 100, ''), align='center') |
| 102 | + break |
| 103 | + |
| 104 | +def play(x, y): |
| 105 | + if not win: |
| 106 | + global yanse |
| 107 | + global gz |
| 108 | + color(yanse) |
| 109 | + up() |
| 110 | + x = round(x/gz)*gz |
| 111 | + y = round(y/gz)*gz |
| 112 | + i = int(9 - y / gz) |
| 113 | + j = int(x / gz + 9) |
| 114 | + |
| 115 | + if i >= 0 and i <= 18 and j >=0 and j<=18: |
| 116 | + if m[i][j] == 0: |
| 117 | + goto(x, y) |
| 118 | + dot(30) |
| 119 | + |
| 120 | + if yanse == "black": |
| 121 | + m[i][j] = 1 |
| 122 | + check(i, j) |
| 123 | + yanse="white" |
| 124 | + else: |
| 125 | + m[i][j] = 2 |
| 126 | + check(i, j) |
| 127 | + yanse="black" |
| 128 | + |
| 129 | + judge.color(yanse) |
| 130 | + judge.dot(30) |
| 131 | + |
| 132 | +onscreenclick(play, 1) |
| 133 | +done() |
| 134 | + |
| 135 | +``` |
| 136 | + |
| 137 | +## Run the demo |
| 138 | + |
| 139 | +Please save the Python as gobang.py and run it in console: |
| 140 | + |
| 141 | +``` |
| 142 | +python gobang.py |
| 143 | +``` |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | +---- |
| 148 | + |
| 149 | +# 1分钟数学运算 |
| 150 | + |
| 151 | +## 项目需求 |
| 152 | + |
| 153 | +- 直接在控制台使用命令行运行 |
| 154 | +- 运行之后出现五子棋小游戏 |
| 155 | + |
| 156 | +## 项目练习 |
| 157 | + |
| 158 | +- turtle工具包 |
| 159 | +- 自定义函数 |
| 160 | +- 二维列表 |
| 161 | +- 鼠标事件 |
| 162 | + |
| 163 | +## 项目参考代码 |
| 164 | + |
| 165 | +```python |
| 166 | +# 导入turtle工具包 |
| 167 | +from turtle import * |
| 168 | + |
| 169 | +# 输赢 |
| 170 | +win = False |
| 171 | + |
| 172 | +# 最快速度 |
| 173 | +speed(0) |
| 174 | +# 背景颜色 |
| 175 | +bgcolor("lightgreen") |
| 176 | +# 颜色变量,默认是黑色 |
| 177 | +yanse="black" |
| 178 | +# 格子大小 |
| 179 | +gz=40 |
| 180 | + |
| 181 | +judge = Turtle() |
| 182 | +judge.up() |
| 183 | +judge.goto(-460, 330) |
| 184 | +judge.write("谁下", font=("Kai", 40, "bold")) |
| 185 | +judge.color(yanse) |
| 186 | +judge.goto(-420, 300) |
| 187 | +judge.dot(30) |
| 188 | + |
| 189 | +# 画19条横线 |
| 190 | +for i in range(19): |
| 191 | + up() |
| 192 | + goto(-gz*9, gz*(9-i)) |
| 193 | + down() |
| 194 | + fd(gz*18) |
| 195 | + bk(gz*18) |
| 196 | + |
| 197 | +rt(90) |
| 198 | +# 画19条竖线 |
| 199 | +for i in range(19): |
| 200 | + up() |
| 201 | + goto(-gz*(9-i), gz*9) |
| 202 | + down() |
| 203 | + fd(gz*18) |
| 204 | + bk(gz*18) |
| 205 | + |
| 206 | +# 画最外面的粗边框 |
| 207 | +pensize(5) |
| 208 | +for i in range(4): |
| 209 | + fd(gz*18) |
| 210 | + rt(90) |
| 211 | + |
| 212 | +# 2维列表,存储棋盘棋子数据,0表示没有棋子,1表示黑棋,2表示白棋 |
| 213 | +# m = [[0] * 19 for i in range(19)] |
| 214 | +m =[ |
| 215 | + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], |
| 216 | + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], |
| 217 | + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], |
| 218 | + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], |
| 219 | + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], |
| 220 | + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], |
| 221 | + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], |
| 222 | + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], |
| 223 | + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], |
| 224 | + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], |
| 225 | + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], |
| 226 | + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], |
| 227 | + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], |
| 228 | + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], |
| 229 | + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], |
| 230 | + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], |
| 231 | + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], |
| 232 | + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], |
| 233 | + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] |
| 234 | +] |
| 235 | + |
| 236 | +# 判断输赢 |
| 237 | +def check(i, j): |
| 238 | + global win |
| 239 | + g = [0] * 8 |
| 240 | + fw = ((0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1)) |
| 241 | + for index in range(8): |
| 242 | + d = fw[index] |
| 243 | + next_i = i + d[0] |
| 244 | + next_j = j + d[1] |
| 245 | + while next_i in range(19) and next_j in range(19) and m[next_i][next_j] == m[i][j]: |
| 246 | + g[index] = g[index] + 1 |
| 247 | + next_i = next_i + d[0] |
| 248 | + next_j = next_j + d[1] |
| 249 | + |
| 250 | + for index in range(4): |
| 251 | + if g[index] + g[index + 4] + 1 >= 5: |
| 252 | + win = True |
| 253 | + goto(0, 0) |
| 254 | + if yanse == "black": |
| 255 | + write('黑方胜', font=('', 100, ''), align='center') |
| 256 | + else: |
| 257 | + write('白方胜', font=('', 100, ''), align='center') |
| 258 | + break |
| 259 | + |
| 260 | +# 放棋子的自定义函数 |
| 261 | +def play(x, y): |
| 262 | + if not win: |
| 263 | + global yanse |
| 264 | + global gz |
| 265 | + color(yanse) |
| 266 | + up() |
| 267 | + # x变成格子的整数倍,个位数上要进行4舍5入 |
| 268 | + x = round(x/gz)*gz |
| 269 | + # y变成格子的整数倍,个位数上要进行4舍5入 |
| 270 | + y = round(y/gz)*gz |
| 271 | + # 计算棋子在电脑中的位置 |
| 272 | + i = int(9 - y / gz) |
| 273 | + j = int(x / gz + 9) |
| 274 | + |
| 275 | + # 只有棋子在棋盘范围内才可以下 |
| 276 | + if i >= 0 and i <= 18 and j >=0 and j<=18: |
| 277 | + # 如果[i, j]没有棋子 |
| 278 | + if m[i][j] == 0: |
| 279 | + goto(x, y) |
| 280 | + dot(30) |
| 281 | + |
| 282 | + if yanse == "black": |
| 283 | + m[i][j] = 1 |
| 284 | + # 判断输赢 |
| 285 | + check(i, j) |
| 286 | + yanse="white" |
| 287 | + else: |
| 288 | + m[i][j] = 2 |
| 289 | + # 判断输赢 |
| 290 | + check(i, j) |
| 291 | + yanse="black" |
| 292 | + |
| 293 | + # 裁判变色并重新画点 |
| 294 | + judge.color(yanse) |
| 295 | + judge.dot(30) |
| 296 | + |
| 297 | +# 当鼠标左键点击屏幕时执行放棋子的自定义函数 |
| 298 | +onscreenclick(play, 1) |
| 299 | +done() |
| 300 | + |
| 301 | +``` |
| 302 | + |
| 303 | +## 测试运行 |
| 304 | + |
| 305 | +将代码保存为gobang.py,然后在控制台运行: |
| 306 | + |
| 307 | +``` |
| 308 | +python gobang.py |
| 309 | +``` |
| 310 | + |
| 311 | + |
0 commit comments