python实现五子棋人机对战
时间: 2023-08-26 07:04:52 浏览: 157
五子棋是一种非常经典的棋类游戏,人机对战的实现可以用Python语言来完成。
首先,需要安装Python的GUI库Tkinter。然后,我们可以通过编写一个五子棋游戏的类来实现人机对战的功能。以下是一个简单的五子棋游戏类的实现:
```python
import tkinter as tk
import tkinter.messagebox
class Gobang:
def __init__(self, master):
self.master = master
self.master.title("五子棋人机对战")
self.master.geometry("550x550")
self.color = "black"
self.checkerboard = [[0 for i in range(15)] for j in range(15)]
self.create_widgets()
def create_widgets(self):
self.canvas = tk.Canvas(self.master, bg="white", width=450, height=450)
self.canvas.pack(side="left", padx=20, pady=20)
for i in range(15):
self.canvas.create_line(25, 25 + i * 30, 425, 25 + i * 30)
self.canvas.create_line(25 + i * 30, 25, 25 + i * 30, 425)
self.canvas.bind("<Button-1>", self.put_chess)
self.restart_button = tk.Button(self.master, text="重新开始", command=self.restart_game)
self.restart_button.pack(side="top", padx=10, pady=10)
def put_chess(self, event):
x = (event.x - 25) // 30
y = (event.y - 25) // 30
if x >= 0 and x <= 14 and y >= 0 and y <= 14 and self.checkerboard[y][x] == 0:
if self.color == "black":
self.canvas.create_oval(x * 30 + 5, y * 30 + 5, x * 30 + 25, y * 30 + 25, fill="black")
self.checkerboard[y][x] = 1
self.color = "white"
if self.check_win(x, y, 1):
self.show_win("black")
return
self.computer_put_chess()
else:
self.canvas.create_oval(x * 30 + 5, y * 30 + 5, x * 30 + 25, y * 30 + 25, fill="white")
self.checkerboard[y][x] = 2
self.color = "black"
if self.check_win(x, y, 2):
self.show_win("white")
def computer_put_chess(self):
x = 0
y = 0
max_score = -1000000
for i in range(15):
for j in range(15):
if self.checkerboard[i][j] == 0:
score = self.evaluate(j, i)
if score > max_score:
max_score = score
x = j
y = i
self.canvas.create_oval(x * 30 + 5, y * 30 + 5, x * 30 + 25, y * 30 + 25, fill="white")
self.checkerboard[y][x] = 2
self.color = "black"
if self.check_win(x, y, 2):
self.show_win("white")
def evaluate(self, x, y):
score = 0
score += self.evaluate_direction(x, y, 1, 0)
score += self.evaluate_direction(x, y, 0, 1)
score += self.evaluate_direction(x, y, 1, 1)
score += self.evaluate_direction(x, y, 1, -1)
return score
def evaluate_direction(self, x, y, dx, dy):
score = 0
if x < 0 or x > 14 or y < 0 or y > 14:
return 0
if self.checkerboard[y][x] == 0:
score += 10
elif self.checkerboard[y][x] == 1:
score += 100
else:
score += 500
for i in range(1, 5):
nx = x + i * dx
ny = y + i * dy
if nx < 0 or nx > 14 or ny < 0 or ny > 14:
break
if self.checkerboard[ny][nx] == 0:
score += 10
elif self.checkerboard[ny][nx] == 1:
score += 100
break
else:
score += 500
break
for i in range(1, 5):
nx = x - i * dx
ny = y - i * dy
if nx < 0 or nx > 14 or ny < 0 or ny > 14:
break
if self.checkerboard[ny][nx] == 0:
score += 10
elif self.checkerboard[ny][nx] == 1:
score += 100
break
else:
score += 500
break
return score
def check_win(self, x, y, color):
count = 1
for i in range(1, 5):
if x + i < 15 and self.checkerboard[y][x + i] == color:
count += 1
else:
break
for i in range(1, 5):
if x - i >= 0 and self.checkerboard[y][x - i] == color:
count += 1
else:
break
if count >= 5:
return True
count = 1
for i in range(1, 5):
if y + i < 15 and self.checkerboard[y + i][x] == color:
count += 1
else:
break
for i in range(1, 5):
if y - i >= 0 and self.checkerboard[y - i][x] == color:
count += 1
else:
break
if count >= 5:
return True
count = 1
for i in range(1, 5):
if x + i < 15 and y + i < 15 and self.checkerboard[y + i][x + i] == color:
count += 1
else:
break
for i in range(1, 5):
if x - i >= 0 and y - i >= 0 and self.checkerboard[y - i][x - i] == color:
count += 1
else:
break
if count >= 5:
return True
count = 1
for i in range(1, 5):
if x + i < 15 and y - i >= 0 and self.checkerboard[y - i][x + i] == color:
count += 1
else:
break
for i in range(1, 5):
if x - i >= 0 and y + i < 15 and self.checkerboard[y + i][x - i] == color:
count += 1
else:
break
if count >= 5:
return True
return False
def show_win(self, color):
tkinter.messagebox.showinfo("游戏结束", color + "赢了!")
self.restart_game()
def restart_game(self):
self.color = "black"
self.checkerboard = [[0 for i in range(15)] for j in range(15)]
self.canvas.delete("all")
for i in range(15):
self.canvas.create_line(25, 25 + i * 30, 425, 25 + i * 30)
self.canvas.create_line(25 + i * 30, 25, 25 + i * 30, 425)
if __name__ == "__main__":
root = tk.Tk()
app = Gobang(root)
root.mainloop()
```
以上代码实现了一个基本的五子棋游戏,包括棋盘的绘制、棋子的下放、判断胜负等功能。其中,计算机下棋的算法使用了简单的评估函数,可以通过不断优化算法来提高计算机的水平。
阅读全文