jupyter五子棋博弈代码
时间: 2023-12-03 10:40:59 浏览: 129
以下是使用Jupyter Notebook实现的五子棋博弈代码:
```python
# 导入必要的库
import numpy as np
import random
import time
# 定义棋盘大小
BOARD_SIZE = 15
# 定义棋子类型
EMPTY = 0
BLACK = 1
WHITE = 2
# 定义搜索深度
SEARCH_DEPTH = 3
# 定义估值函数
def evaluate(board, player):
# 初始化估值
value = 0
# 统计棋盘上每个位置的得分
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
# 如果该位置为空,则跳过
if board[i][j] == EMPTY:
continue
# 如果该位置为己方棋子,则加上得分
if board[i][j] == player:
value += get_score(board, i, j, player)
# 如果该位置为对方棋子,则减去得分
else:
value -= get_score(board, i, j, player)
return value
# 定义得分函数
def get_score(board, x, y, player):
# 初始化得分
score = 0
# 统计横向得分
left, right = y, y
while left >= 0 and board[x][left] == player:
left -= 1
while right < BOARD_SIZE and board[x][right] == player:
right += 1
if right - left - 1 >= 5:
score += 10000
elif right - left - 1 == 4:
score += 1000
elif right - left - 1 == 3:
score += 100
elif right - left - 1 == 2:
score += 10
elif right - left - 1 == 1:
score += 1
# 统计纵向得分
up, down = x, x
while up >= 0 and board[up][y] == player:
up -= 1
while down < BOARD_SIZE and board[down][y] == player:
down += 1
if down - up - 1 >= 5:
score += 10000
elif down - up - 1 == 4:
score += 1000
elif down - up - 1 == 3:
score += 100
elif down - up - 1 == 2:
score += 10
elif down - up - 1 == 1:
score += 1
# 统计左上到右下得分
left, up, right, down = y, x, y, x
while left >= 0 and up >= 0 and board[up][left] == player:
left -= 1
up -= 1
while right < BOARD_SIZE and down < BOARD_SIZE and board[down][right] == player:
right += 1
down += 1
if right - left - 1 >= 5:
score += 10000
elif right - left - 1 == 4:
score += 1000
elif right - left - 1 == 3:
score += 100
elif right - left - 1 == 2:
score += 10
elif right - left - 1 == 1:
score += 1
# 统计左下到右上得分
left, down, right, up = y, x, y, x
while left >= 0 and down < BOARD_SIZE and board[down][left] == player:
left -= 1
down += 1
while right < BOARD_SIZE and up >= 0 and board[up][right] == player:
right += 1
up -= 1
if right - left - 1 >= 5:
score += 10000
elif right - left - 1 == 4:
score += 1000
elif right - left - 1 == 3:
score += 100
elif right - left - 1 == 2:
score += 10
elif right - left - 1 == 1:
score += 1
return score
# 定义搜索函数
def search(board, depth, alpha, beta, player):
# 如果搜索到最大深度或者棋局已经结束,则返回估值
if depth == 0 or is_game_over(board):
return evaluate(board, player)
# 如果是己方回合,则进行最大化搜索
if player == BLACK:
value = -np.inf
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
if board[i][j] == EMPTY:
board[i][j] = BLACK
value = max(value, search(board, depth - 1, alpha, beta, WHITE))
board[i][j] = EMPTY
alpha = max(alpha, value)
if alpha >= beta:
return value
return value
# 如果是对方回合,则进行最小化搜索
else:
value = np.inf
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
if board[i][j] == EMPTY:
board[i][j] = WHITE
value = min(value, search(board, depth - 1, alpha, beta, BLACK))
board[i][j] = EMPTY
beta = min(beta, value)
if alpha >= beta:
return value
return value
# 定义落子函数
def make_move(board, player):
# 如果是己方回合,则进行最大化搜索
if player == BLACK:
value = -np.inf
moves = []
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
if board[i][j] == EMPTY:
board[i][j] = BLACK
new_value = search(board, SEARCH_DEPTH, -np.inf, np.inf, WHITE)
board[i][j] = EMPTY
if new_value > value:
value = new_value
moves = [(i, j)]
elif new_value == value:
moves.append((i, j))
x, y = random.choice(moves)
board[x][y] = BLACK
# 如果是对方回合,则等待对方落子
else:
print("请等待对方落子...")
time.sleep(2)
x, y = map(int, input("请输入落子位置,以逗号分隔:").split(","))
while board[x][y] != EMPTY:
print("该位置已经有棋子,请重新输入!")
x, y = map(int, input("请输入落子位置,以逗号分隔:").split(","))
board[x][y] = WHITE
# 定义判断游戏是否结束的函数
def is_game_over(board):
# 判断横向是否有五子连珠
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE - 4):
if board[i][j] != EMPTY and board[i][j] == board[i][j + 1] == board[i][j + 2] == board[i][j + 3] == board[i][j + 4]:
return True
# 判断纵向是否有五子连珠
for i in range(BOARD_SIZE - 4):
for j in range(BOARD_SIZE):
if board[i][j] != EMPTY and board[i][j] == board[i + 1][j] == board[i + 2][j] == board[i + 3][j] == board[i + 4][j]:
return True
# 判断左上到右下是否有五子连珠
for i in range(BOARD_SIZE - 4):
for j in range(BOARD_SIZE - 4):
if board[i][j] != EMPTY and board[i][j] == board[i + 1][j + 1] == board[i + 2][j + 2] == board[i + 3][j + 3] == board[i + 4][j + 4]:
return True
# 判断左下到右上是否有五子连珠
for i in range(4, BOARD_SIZE):
for j in range(BOARD_SIZE - 4):
if board[i][j] != EMPTY and board[i][j] == board[i - 1][j + 1] == board[i - 2][j + 2] == board[i - 3][j + 3] == board[i - 4][j + 4]:
return True
# 如果棋盘已满,则游戏结束
if np.all(board != EMPTY):
return True
# 否则游戏未结束
return False
# 定义主函数
def main():
# 初始化棋盘
board = np.zeros((BOARD_SIZE, BOARD_SIZE), dtype=int)
# 随机决定先手
player = random.choice([BLACK, WHITE])
# 开始游戏
while not is_game_over(board):
# 打印棋盘
print(board)
# 如果是己方回合,则落子
if player == BLACK:
make_move(board, player)
# 如果是对方回合,则等待对方落子
else:
make_move(board, player)
# 切换回合
player = BLACK if player == WHITE else WHITE
# 打印最终棋盘
print(board)
# 判断胜负
if evaluate(board, BLACK) > 0:
print("黑方获胜!")
elif evaluate(board, WHITE) < 0:
print("白方获胜!")
else:
print("平局!")
# 运行主函数
main()
```
阅读全文