写一个基于python的五子棋人机对战游戏代码
时间: 2023-10-09 22:09:22 浏览: 80
好的,非常感谢您的提问。以下是基于 Python 的五子棋人机对战游戏代码:
```
import numpy as np
def create_board():
return np.zeros((15,15), dtype=int)
def is_valid_move(board, row, col):
return board[row][col] == 0
def place_piece(board, row, col, piece):
board[row][col] = piece
def is_win(board, piece):
# Check horizontal locations
for c in range(11):
for r in range(15):
if board[r][c] == piece and board[r][c+1] == piece and board[r][c+2] == piece and board[r][c+3] == piece and board[r][c+4] == piece:
return True
# Check vertical locations
for c in range(15):
for r in range(11):
if board[r][c] == piece and board[r+1][c] == piece and board[r+2][c] == piece and board[r+3][c] == piece and board[r+4][c] == piece:
return True
# Check diagonal locations
for c in range(11):
for r in range(11):
if board[r][c] == piece and board[r+1][c+1] == piece and board[r+2][c+2] == piece and board[r+3][c+3] == piece and board[r+4][c+4] == piece:
return True
for c in range(11):
for r in range(4, 15):
if board[r][c] == piece and board[r-1][c+1] == piece and board[r-2][c+2] == piece and board[r-3][c+3] == piece and board[r-4][c+4] == piece:
return True
return False
def minimax(board, depth, alpha, beta, maximizingPlayer, piece):
if depth == 0:
return None, evaluate(board, piece)
if maximizingPlayer:
value = float('-inf')
best_move = None
for col in range(15):
for row in range(15):
if is_valid_move(board, row, col):
board_copy = board.copy()
place_piece(board_copy, row, col, piece)
new_score = minimax(board_copy, depth-1, alpha, beta, False, piece)
if new_score[1] > value:
value = new_score[1]
best_move = (row, col)
alpha = max(alpha, value)
if alpha >= beta:
break
return best_move, value
else:
value = float('inf')
best_move = None
for col in range(15):
for row in range(15):
if is_valid_move(board, row, col):
board_copy = board.copy()
place_piece(board_copy, row, col, 3 - piece)
new_score = minimax(board_copy, depth-1, alpha, beta, True, piece)
if new_score[1] < value:
value = new_score[1]
best_move = (row, col)
beta = min(beta, value)
if beta <= alpha:
break
return best_move, value
def evaluate(window, piece):
score = 0
opp_piece = 3 - piece
if np.count_nonzero(window == piece) == 5:
score += 100
elif np.count_nonzero(window == piece) == 4 and np.count_nonzero(window == 0) == 1:
score += 10
elif np.count_nonzero(window == piece) == 3 and np.count_nonzero(window == 0) == 2:
score += 5
if np.count_nonzero(window == opp_piece) == 4 and np.count_nonzero(window == 0) == 1:
score -= 6
return score
board = create_board()
game_over = False
turn = 1
depth = 4
while not game_over:
if turn == 1:
row, col = input("请输入你的下棋坐标(如:0,0):").split(",")
row = int(row)
col = int(col)
if is_valid_move(board, row, col):
place_piece(board, row, col, turn)
turn = 3 - turn
else:
print("该坐标已有棋子,请重新输入")
continue
else:
best_move, value = minimax(board, depth, float('-inf'), float('inf'), True, turn)
print("AI下棋坐标:", best_move)
place_piece(board, best_move[0], best_move[1], turn)
turn = 3 - turn
if is_win(board, 1):
print("恭喜你,你赢了!")
game_over = True
elif is_win(board, 2):
print("AI获胜!")
game_over = True
elif np.count_nonzero(board == 0) == 0:
print("平局!")
game_over = True
```
请确认是否满足您的需求。如果您有任何疑问,请随时联系我。
阅读全文