用python编写一个字符界面的五子棋人机对战程序,游戏难度中等,代码简单
时间: 2024-05-13 09:15:05 浏览: 102
好的,我可以给你提供一个简单的五子棋人机对战程序。这个程序使用Python的Tkinter库来创建字符界面,并使用Minimax算法作为AI。这个程序的难度可以被调整为中等。
代码如下:
```python
import tkinter as tk
import random
# 创建棋盘
def create_board():
board = []
for i in range(15):
row = []
for j in range(15):
row.append(0)
board.append(row)
return board
# 判断胜利
def check_win(board, player):
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:
return True
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:
return True
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:
return True
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:
return True
return False
# 计算得分
def calculate_score(board, player):
score = 0
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] == 0:
score += 100
if board[i][j] == 0 and board[i][j+1] == player and board[i][j+2] == player and board[i][j+3] == player and board[i][j+4] == player:
score += 100
if board[i][j] == player and board[i][j+1] == 0 and board[i][j+2] == player and board[i][j+3] == player and board[i][j+4] == player:
score += 100
if board[i][j] == player and board[i][j+1] == player and board[i][j+2] == 0 and board[i][j+3] == player and board[i][j+4] == player:
score += 100
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:
score += 1000
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] == 0:
score += 100
if board[i][j] == 0 and board[i+1][j] == player and board[i+2][j] == player and board[i+3][j] == player and board[i+4][j] == player:
score += 100
if board[i][j] == player and board[i+1][j] == 0 and board[i+2][j] == player and board[i+3][j] == player and board[i+4][j] == player:
score += 100
if board[i][j] == player and board[i+1][j] == player and board[i+2][j] == 0 and board[i+3][j] == player and board[i+4][j] == player:
score += 100
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:
score += 1000
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] == 0:
score += 100
if board[i][j] == 0 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:
score += 100
if board[i][j] == player and board[i+1][j+1] == 0 and board[i+2][j+2] == player and board[i+3][j+3] == player and board[i+4][j+4] == player:
score += 100
if board[i][j] == player and board[i+1][j+1] == player and board[i+2][j+2] == 0 and board[i+3][j+3] == player and board[i+4][j+4] == player:
score += 100
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:
score += 1000
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] == 0:
score += 100
if board[i][j] == 0 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:
score += 100
if board[i][j] == player and board[i+1][j-1] == 0 and board[i+2][j-2] == player and board[i+3][j-3] == player and board[i+4][j-4] == player:
score += 100
if board[i][j] == player and board[i+1][j-1] == player and board[i+2][j-2] == 0 and board[i+3][j-3] == player and board[i+4][j-4] == player:
score += 100
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:
score += 1000
return score
# AI
def minimax(board, depth, player):
if depth == 0:
return None, calculate_score(board, 2)
if player == 2:
best_score = float('-inf')
best_move = None
for i in range(15):
for j in range(15):
if board[i][j] == 0:
board[i][j] = 2
score = minimax(board, depth-1, 1)[1]
board[i][j] = 0
if score > best_score:
best_score = score
best_move = (i, j)
return best_move, best_score
else:
best_score = float('inf')
best_move = None
for i in range(15):
for j in range(15):
if board[i][j] == 0:
board[i][j] = 1
score = minimax(board, depth-1, 2)[1]
board[i][j] = 0
if score < best_score:
best_score = score
best_move = (i, j)
return best_move, best_score
# 让AI下棋
def ai_turn(board):
move = minimax(board, 3, 2)[0]
board[move[0]][move[1]] = 2
return move[0], move[1]
# 棋盘点击事件
def board_click(event):
global board, player_turn, game_over
if game_over:
return
x = event.x // 30
y = event.y // 30
if board[y][x] != 0:
return
board[y][x] = 1
player_turn = False
if check_win(board, 1):
game_over = True
canvas.create_text(225, 225, text='You win!', font=('Helvetica', 32), fill='red')
return
if not any(0 in row for row in board):
game_over = True
canvas.create_text(225, 225, text='Tie game!', font=('Helvetica', 32), fill='red')
return
x, y = ai_turn(board)
canvas.create_oval(x*30+5, y*30+5, x*30+25, y*30+25, fill='black')
if check_win(board, 2):
game_over = True
canvas.create_text(225, 225, text='You lose!', font=('Helvetica', 32), fill='red')
return
if not any(0 in row for row in board):
game_over = True
canvas.create_text(225, 225, text='Tie game!', font=('Helvetica', 32), fill='red')
return
# 创建窗口
window = tk.Tk()
window.title('Five in a Row')
window.geometry('450x450')
# 创建棋盘
board = create_board()
# 创建画布
canvas = tk.Canvas(window, width=450, height=450, bg='white')
canvas.pack()
# 画棋盘
for i in range(15):
canvas.create_line(15+i*30, 15, 15+i*30, 435)
canvas.create_line(15, 15+i*30, 435, 15+i*30)
# 点击事件
canvas.bind('<Button-1>', board_click)
# 游戏变量
player_turn = bool(random.getrandbits(1))
game_over = False
# 让AI先下
if not player_turn:
x, y = ai_turn(board)
canvas.create_oval(x*30+5, y*30+5, x*30+25, y*30+25, fill='black')
# 运行程序
window.mainloop()
```
运行程序后将会弹出一个棋盘界面。玩家可以用鼠标点击棋盘上的空格来下棋,黑色表示AI下的棋。当一方获得五个棋子连成一条线时,游戏结束并打印胜利者。如果棋盘被下满,游戏将以平局结束。
阅读全文