一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術(shù)及教程分享平臺!
分類導(dǎo)航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務(wù)器之家 - 腳本之家 - Python - python實現(xiàn)簡單的五子棋游戲

python實現(xiàn)簡單的五子棋游戲

2020-09-02 00:09assasinSteven Python

這篇文章主要為大家詳細(xì)介紹了python實現(xiàn)簡單的五子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了python實現(xiàn)五子棋游戲的具體代碼,供大家參考,具體內(nèi)容如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# -*- coding:utf-8 -*-
# @Time: 2017/8/29 0029 10:14
# @Author: assasin
 
from tkinter import *
import math
 
class chessBoard():
  def __init__(self):
    # 創(chuàng)建一個tk對象,窗口
    self.window = Tk()
    # 窗口名稱
    self.window.title('五子棋游戲')
    # 窗口大小
    self.window.geometry('660x470')
    # 設(shè)置窗口不可縮放
    self.window.resizable(0,0)
    # 定義窗口的畫布
    self.canvas = Canvas(self.window, bg="#EEE8AC", width=470, height=470)
    # 畫出畫布內(nèi)容
    self.paint_board()
    # 定義畫布所在的網(wǎng)格
    self.canvas.grid(row=0, column=0)
 
  def paint_board(self):
    # 畫橫線
    for row in range(0, 15):
      if row == 0 or row == 14:
        self.canvas.create_line(25, 25 + row * 30, 25 + 14 * 30, 25 + row * 30, width=2)
    else:
      self.canvas.create_line(25, 25 + row * 30, 25 + 14 * 30, 25 + row * 30, width=1)
 
    # 畫豎線
    for column in range(0, 15):
      if column == 0 or column == 14:
        self.canvas.create_line(25 + column * 30, 25, 25 + column * 30, 25 + 14 * 30, width=2)
    else:
      self.canvas.create_line(25 + column * 30, 25, 25 + column * 30, 25 + 14 * 30, width=1)
 
    # 畫圓
    self.canvas.create_oval(112, 112, 118, 118, fill="black")
    self.canvas.create_oval(352, 112, 358, 118, fill="black")
    self.canvas.create_oval(112, 352, 118, 358, fill="black")
    self.canvas.create_oval(232, 232, 238, 238, fill="black")
    self.canvas.create_oval(352, 352, 358, 358, fill="black")
 
 
 
 
#定義五子棋游戲類
#0為黑子 , 1為白子 , 2為空位
class Gobang() :
  #初始化
  def __init__(self) :
    self.board = chessBoard()
    self.game_print = StringVar()
    self.game_print.set("")
    # 16*16的二維列表,保證不會out of index
    self.db = [([2] * 16) for i in range(16)]
    # 悔棋用的順序列表
    self.order = []
    # 棋子顏色
    self.color_count = 0
    self.color = 'black'
    # 清空與贏的初始化,已贏為1,已清空為1
    self.flag_win = 1
    self.flag_empty = 1
    self.options()
 
    # 黑白互換
  def change_color(self):
    self.color_count = (self.color_count + 1) % 2
    if self.color_count == 0:
      self.color = "black"
    elif self.color_count == 1:
      self.color = "white"
 
  # 落子
  def chess_moving(self,event):
    # 不點擊“開始”與“清空”無法再次開始落子
    if self.flag_win == 1 or self.flag_empty == 0:
      return
    # 坐標(biāo)轉(zhuǎn)化為下標(biāo)
    x, y = event.x - 25, event.y - 25
    x = round(x / 30)
    y = round(y / 30)
    # 點擊位置沒用落子,且沒有在棋盤線外,可以落子
    while self.db[y][x] == 2 and self.limit_boarder(y, x):
      self.db[y][x] = self.color_count
    self.order.append(x + 15 * y)
    self.board.canvas.create_oval(25 + 30 * x - 12, 25 + 30 * y - 12, 25 + 30 * x + 12, 25 + 30 * y + 12,fill=self.color, tags="chessman")
    if self.game_win(y, x, self.color_count):
      print(self.color, "獲勝")
      self.game_print.set(self.color + "獲勝")
    else:
      self.change_color()
      self.game_print.set("請" + self.color + "落子")
 
  # 保證棋子落在棋盤上
  def limit_boarder(self, y, x):
    if x < 0 or x > 14 or y < 0 or y > 14:
      return False
    else:
      return True
 
  # 計算連子的數(shù)目,并返回最大連子數(shù)目
  def chessman_count(self, y, x, color_count):
    count1, count2, count3, count4 = 1, 1, 1, 1
    # 橫計算
    for i in range(-1, -5, -1):
      if self.db[y][x + i] == color_count:
        count1 += 1
      else:
        break
 
    for i in range(1, 5, 1):
      if self.db[y][x + i] == color_count:
        count1 += 1
      else:
        break
    # 豎計算
    for i in range(-1, -5, -1):
      if self.db[y + i][x] == color_count:
        count2 += 1
      else:
        break
    for i in range(1, 5, 1):
      if self.db[y + i][x] == color_count:
        count2 += 1
      else:
        break
    # /計算
    for i in range(-1, -5, -1):
      if self.db[y + i][x + i] == color_count:
        count3 += 1
      else:
        break
    for i in range(1, 5, 1):
      if self.db[y + i][x + i] == color_count:
        count3 += 1
      else:
        break
 
    # \計算
    for i in range(-1, -5, -1):
      if self.db[y + i][x - i] == color_count:
        count4 += 1
      else:
        break
    for i in range(1, 5, 1):
      if self.db[y + i][x - i] == color_count:
        count4 += 1
      else:
        break
 
    return max(count1, count2, count3, count4)
 
 
  # 判斷輸贏
  def game_win(self , y , x , color_count ):
    if self.chessman_count(y, x, color_count) >= 5:
      self.flag_win = 1
      self.flag_empty = 0
      return True
    else:
      return False
 
  #悔棋,清空棋盤,再畫剩下的n-1個棋子
  def withdraw(self):
    if len(self.order) == 0 or self.flag_win == 1:
      return
    self.board.canvas.delete("chessman")
    z = self.order.pop()
    x = z % 15
    y = z // 15
    self.db[y][x] = 2
    self.color_count = 1
    for i in self.order:
      ix = i % 15
    iy = i // 15
    self.change_color()
    self.board.canvas.create_oval(25 + 30 * ix - 12, 25 + 30 * iy - 12, 25 + 30 * ix + 12, 25 + 30 * iy + 12,
                   fill=self.color, tags="chessman")
    self.change_color()
    self.game_print.set("請" + self.color + "落子")
 
  # 清空
  def empty_all(self) :
    self.board.canvas.delete("chessman")
    # 還原初始化
    self.db = [([2] * 16) for i in range(16)]
    self.order = []
    self.color_count = 0
    self.color = 'black'
    self.flag_win = 1
    self.flag_empty = 1
    self.game_print.set("")
 
  #將self.flag_win置0才能在棋盤上落子
  def game_start(self):
    # 沒有清空棋子不能置0開始
    if self.flag_empty == 0:
      return
    self.flag_win = 0
    self.game_print.set("請" + self.color + "落子")
 
  def options(self):
    self.board.canvas.bind("<Button-1>", self.chess_moving)
    Label(self.board.window, textvariable=self.game_print, font=("Arial", 20)).place(relx=0, rely=0, x=495, y=200)
    Button(self.board.window, text="開始游戲", command=self.game_start, width=13, font=("Verdana", 12)).place(relx=0,rely=0,x=495,y=15)
    Button(self.board.window, text="我要悔棋", command=self.withdraw, width=13, font=("Verdana", 12)).place(relx=0,rely=0,x=495, y=60)
    Button(self.board.window, text="清空棋局", command=self.empty_all, width=13, font=("Verdana", 12)).place(relx=0,rely=0,x=495,y=105)
    Button(self.board.window, text="結(jié)束游戲", command=self.board.window.destroy, width=13, font=("Verdana", 12)).place(relx=0, rely=0, x=495, y=420)
    self.board.window.mainloop()
 
 
if __name__ == '__main__':
  chess_game = Gobang()

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://blog.csdn.net/assasin0308/article/details/108292206

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲精品无码不卡在线观看 | 亚洲免费闲人蜜桃 | 超级乱淫伦小说1女多男 | 久久精品18 | 久久精品国产色蜜蜜麻豆国语版 | 99色在线观看 | 91亚洲精品第一综合不卡播放 | 热99这里有精品综合久久 | 欧美日韩一品道 | 3x免费高清视频 | 99久久精品免费看国产 | 九九精品国产兔费观看久久 | 美女扒开胸罩露出奶了无遮挡免费 | 日本最新伦中文字幕 | 欧美午夜视频一区二区三区 | 美女扒开腿让男生桶爽漫画 | 日韩欧美一区二区三区免费观看 | 水多多凹凸福利视频导航 | 亚洲福利精品电影在线观看 | 操老逼视频 | 成人精品一区久久久久 | 亚洲精品午夜视频 | 欧式午夜理伦三级在线观看 | 色小孩导航 | 99re热精品这里精品 | 四虎影视入口 | 高清一区高清二区视频 | 亚洲高清影院 | 国产精品夜夜爽张柏芝 | 日韩黄色影视 | 亚洲AV综合99一二三四区 | 四虎在线观看 | 国产精品久久久久毛片真精品 | 亚洲aⅴ天堂| 久久99国产综合精品AV蜜桃 | 69热精品视频在线看影院 | 10个免费货源网站 | 女同变态 中文字幕 | 四虎影免看黄 | 国产亚洲精品激情一区二区三区 | 免费日本视频 |