用Python写人机对战五子棋
时间: 2023-10-11 07:07:15 浏览: 88
好的,以下是一个简单的人机对战五子棋的Python代码,使用了基本的算法和GUI库Tkinter:
```python
from tkinter import *
import random
# 初始化棋盘
def init_board():
global board, player, winner, end
board = []
for i in range(15):
row = []
for j in range(15):
row.append(0)
board.append(row)
player = 1
winner = 0
end = False
# 判断游戏是否结束
def check_win():
global board, player, winner, end
for i in range(15):
for j in range(11):
if board[i][j] == player and board[i][j+1] == player and board[i][j+2] == player and board[i][j+3] == player and board[i][j+4] == player:
winner = player
end = True
return
for i in range(11):
for j in range(15):
if board[i][j] == player and board[i+1][j] == player and board[i+2][j] == player and board[i+3][j] == player and board[i+4][j] == player:
winner = player
end = True
return
for i in range(11):
for j in range(11):
if board[i][j] == player and board[i+1][j+1] == player and board[i+2][j+2] == player and board[i+3][j+3] == player and board[i+4][j+4] == player:
winner = player
end = True
return
for i in range(11):
for j in range(4, 15):
if board[i][j] == player and board[i+1][j-1] == player and board[i+2][j-2] == player and board[i+3][j-3] == player and board[i+4][j-4] == player:
winner = player
end = True
return
for i in range(15):
for j in range(15):
if board[i][j] == 0:
return
winner = 0
end = True
# 画棋盘
def draw_board():
global board_canvas
for i in range(15):
board_canvas.create_line(30, 30+i*30, 450, 30+i*30)
board_canvas.create_line(30+i*30, 30, 30+i*30, 450)
# 画棋子
def draw_piece(x, y):
global board_canvas, player
if player == 1:
color = 'black'
else:
color = 'white'
board_canvas.create_oval(x*30-15, y*30-15, x*30+15, y*30+15, fill=color, outline=color)
# 玩家落子
def player_move(event):
global board, player, end
if end:
return
x = (event.x-15)//30+1
y = (event.y-15)//30+1
if board[y-1][x-1] != 0:
return
board[y-1][x-1] = player
draw_piece(x, y)
check_win()
if not end:
player = 3 - player
computer_move()
# 电脑落子
def computer_move():
global board, player, end
if end:
return
scores = []
for i in range(15):
row = []
for j in range(15):
if board[i][j] == 0:
score = 0
for m in range(max(0, i-4), min(15, i+5)):
for n in range(max(0, j-4), min(15, j+5)):
if board[m][n] != 0:
if board[m][n] == player:
score += 10
else:
score += 100
row.append(score)
else:
row.append(-1)
scores.append(row)
max_score = -1
max_positions = []
for i in range(15):
for j in range(15):
if scores[i][j] > max_score:
max_score = scores[i][j]
max_positions = [(i, j)]
elif scores[i][j] == max_score:
max_positions.append((i, j))
position = random.choice(max_positions)
x = position[1] + 1
y = position[0] + 1
board[y-1][x-1] = player
draw_piece(x, y)
check_win()
if not end:
player = 3 - player
# 主程序
def main():
global board_canvas
root = Tk()
root.title('五子棋')
root.resizable(False, False)
board_canvas = Canvas(root, width=480, height=480)
board_canvas.pack()
board_canvas.bind('<Button-1>', player_move)
init_board()
draw_board()
root.mainloop()
if __name__ == '__main__':
main()
```
运行代码后会弹出一个窗口,你可以通过鼠标点击棋盘上的空白位置进行落子。电脑会根据当前棋局情况计算分数,选择分数最高的位置落子。注意,这个算法非常简单,可能会有明显的漏洞,但足以体验五子棋人机对战的基本操作。
阅读全文