人工智能五子棋代码_chessAI:六子棋人工智能程序实现
时间: 2023-07-01 22:06:03 浏览: 267
以下是一个简单的六子棋人工智能程序实现,采用了极大值极小值算法和α-β剪枝优化。
```
import numpy as np
# 定义棋盘大小
BOARD_SIZE = 15
# 定义棋子类型
WHITE = 1
BLACK = -1
# 定义评估函数
def evaluate(board):
# 统计每个玩家的棋子数
white_count = np.sum(board == WHITE)
black_count = np.sum(board == BLACK)
# 计算得分差
score = white_count - black_count
return score
# 定义极大值极小值算法
def minimax(board, depth, alpha, beta, is_maximizing):
# 判断是否到达搜索深度或者已经有一方胜利
if depth == 0 or check_win(board):
return evaluate(board), None
# 初始化最佳下棋位置
best_move = None
if is_maximizing:
# 最大化玩家下棋
value = float('-inf')
for move in get_moves(board, WHITE):
# 递归调用极大值极小值算法
new_board = make_move(board, WHITE, move)
new_value, _ = minimax(new_board, depth - 1, alpha, beta, False)
if new_value > value:
value = new_value
best_move = move
alpha = max(alpha, value)
if alpha >= beta:
break
else:
# 最小化玩家下棋
value = float('inf')
for move in get_moves(board, BLACK):
# 递归调用极大值极小值算法
new_board = make_move(board, BLACK, move)
new_value, _ = minimax(new_board, depth - 1, alpha, beta, True)
if new_value < value:
value = new_value
best_move = move
beta = min(beta, value)
if alpha >= beta:
break
return value, best_move
# 定义α-β剪枝优化
def minimax_ab(board, depth, alpha, beta, is_maximizing):
# 判断是否到达搜索深度或者已经有一方胜利
if depth == 0 or check_win(board):
return evaluate(board), None
# 初始化最佳下棋位置
best_move = None
if is_maximizing:
# 最大化玩家下棋
value = float('-inf')
for move in get_moves(board, WHITE):
# 递归调用极大值极小值算法
new_board = make_move(board, WHITE, move)
new_value, _ = minimax_ab(new_board, depth - 1, alpha, beta, False)
if new_value > value:
value = new_value
best_move = move
alpha = max(alpha, value)
if alpha >= beta:
break
else:
# 最小化玩家下棋
value = float('inf')
for move in get_moves(board, BLACK):
# 递归调用极大值极小值算法
new_board = make_move(board, BLACK, move)
new_value, _ = minimax_ab(new_board, depth - 1, alpha, beta, True)
if new_value < value:
value = new_value
best_move = move
beta = min(beta, value)
if alpha >= beta:
break
return value, best_move
# 定义获取可落子位置函数
def get_moves(board, player):
moves = []
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
if board[i][j] == 0:
moves.append((i, j))
return moves
# 定义落子函数
def make_move(board, player, move):
new_board = np.copy(board)
new_board[move[0]][move[1]] = player
return new_board
# 定义判断是否有一方胜利函数
def check_win(board):
# 检查行和列
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE - 5):
row = board[i][j:j+6]
col = board[j:j+6,i]
if np.sum(row) == 6 or np.sum(col) == 6:
return True
if np.sum(row) == -6 or np.sum(col) == -6:
return True
# 检查对角线
for i in range(BOARD_SIZE - 5):
for j in range(BOARD_SIZE - 5):
diagonal1 = [board[i+k][j+k] for k in range(6)]
diagonal2 = [board[i+k][j+5-k] for k in range(6)]
if np.sum(diagonal1) == 6 or np.sum(diagonal2) == 6:
return True
if np.sum(diagonal1) == -6 or np.sum(diagonal2) == -6:
return True
return False
# 测试程序
def test():
# 初始化棋盘
board = np.zeros((BOARD_SIZE, BOARD_SIZE), dtype=int)
# 人工智能为白方
ai_player = WHITE
# 游戏循环
while True:
# 人工智能下棋
_, move = minimax_ab(board, 3, float('-inf'), float('inf'), True)
board = make_move(board, ai_player, move)
print("AI move:", move)
print(board)
# 判断是否游戏结束
if check_win(board):
print("AI wins!")
break
# 玩家下棋
row = int(input("Enter row: "))
col = int(input("Enter column: "))
board[row][col] = BLACK
print(board)
# 判断是否游戏结束
if check_win(board):
print("Player wins!")
break
if __name__ == "__main__":
test()
```
在测试程序中,人工智能采用了α-β剪枝算法搜索深度为3,可以根据需要进行调整。玩家通过输入行和列来落子,程序会在控制台打印出当前棋盘状态并判断游戏是否结束。
阅读全文