写一个黑白翻转棋 有ai 有人工对战
- User
写一个黑白翻转棋 有ai 有人工对战
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
写一个黑白翻转棋 有ai 有人工对战
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
黑白翻转棋(也称为奥赛罗棋,Othello)是一种经典的棋盘游戏。它在8x8的棋盘上进行,两名玩家轮流下棋,每次下棋时必须翻转对方的棋子。以下是实现一个支持AI对战和人工对战的黑白翻转棋程序的完整代码。
0 表示空位,1 表示黑棋,-1 表示白棋。import random
# 初始化棋盘
def init_board():
board = [[0] * 8 for _ in range(8)]
board[3][3], board[4][4] = 1, 1 # 黑棋
board[3][4], board[4][3] = -1, -1 # 白棋
return board
# 打印棋盘
def print_board(board):
print(" " + " ".join(map(str, range(8))))
for i, row in enumerate(board):
print(f"{i} " + " ".join(["●" if x == 1 else "○" if x == -1 else "." for x in row]))
print()
# 检查某个位置是否合法
def is_valid_move(board, player, x, y):
if board[x][y] != 0:
return False
opponent = -player
directions = [(-1, 0), (1, 0), (0, -1), (0, 1), (-1, -1), (-1, 1), (1, -1), (1, 1)]
for dx, dy in directions:
nx, ny = x + dx, y + dy
if 0 <= nx < 8 and 0 <= ny < 8 and board[nx][ny] == opponent:
while 0 <= nx < 8 and 0 <= ny < 8 and board[nx][ny] == opponent:
nx += dx
ny += dy
if 0 <= nx < 8 and 0 <= ny < 8 and board[nx][ny] == player:
return True
return False
# 获取所有合法移动
def get_valid_moves(board, player):
moves = []
for x in range(8):
for y in range(8):
if is_valid_move(board, player, x, y):
moves.append((x, y))
return moves
# 执行一步棋
def make_move(board, player, x, y):
board[x][y] = player
opponent = -player
directions = [(-1, 0), (1, 0), (0, -1), (0, 1), (-1, -1), (-1, 1), (1, -1), (1, 1)]
flipped = []
for dx, dy in directions:
nx, ny = x + dx, y + dy
to_flip = []
while 0 <= nx < 8 and 0 <= ny < 8 and board[nx][ny] == opponent:
to_flip.append((nx, ny))
nx += dx
ny += dy
if 0 <= nx < 8 and 0 <= ny < 8 and board[nx][ny] == player:
flipped.extend(to_flip)
for fx, fy in flipped:
board[fx][fy] = player
return len(flipped) > 0
# AI选择最佳移动
def ai_move(board, player):
valid_moves = get_valid_moves(board, player)
if not valid_moves:
return None
# 简单策略:优先占角落,其次随机选择
corners = [(0, 0), (0, 7), (7, 0), (7, 7)]
for move in corners:
if move in valid_moves:
return move
return random.choice(valid_moves)
# 判断游戏是否结束
def is_game_over(board):
return not get_valid_moves(board, 1) and not get_valid_moves(board, -1)
# 统计棋子数量
def count_pieces(board):
black = sum(row.count(1) for row in board)
white = sum(row.count(-1) for row in board)
return black, white
# 主游戏循环
def play_game():
board = init_board()
players = {1: "玩家 (黑棋)", -1: "AI (白棋)"}
current_player = 1 # 黑棋先手
while not is_game_over(board):
print_board(board)
print(f"{players[current_player]} 的回合")
valid_moves = get_valid_moves(board, current_player)
if not valid_moves:
print(f"{players[current_player]} 无法落子,跳过回合\n")
current_player = -current_player
continue
if current_player == 1: # 玩家回合
print("可落子位置:", valid_moves)
while True:
try:
x, y = map(int, input("请输入落子位置 (格式: x y): ").split())
if (x, y) in valid_moves:
break
else:
print("非法位置,请重新输入!")
except ValueError:
print("输入格式错误,请重新输入!")
else: # AI回合
x, y = ai_move(board, current_player)
print(f"AI 落子位置: ({x}, {y})")
make_move(board, current_player, x, y)
current_player = -current_player
# 游戏结束,统计结果
print_board(board)
black, white = count_pieces(board)
print(f"游戏结束!最终比分:黑棋 {black} : 白棋 {white}")
if black > white:
print("黑棋胜利!")
elif black < white:
print("白棋胜利!")
else:
print("平局!")
if __name__ == "__main__":
play_game()
● 表示黑棋,○ 表示白棋,. 表示空位。(x, y) 来选择落子位置。运行程序后,玩家可以与AI对战。例如:
0 1 2 3 4 5 6 7
0 . . . . . . . .
1 . . . . . . . .
2 . . . . . . . .
3 . . . ● ○ . . .
4 . . . ○ ● . . .
5 . . . . . . . .
6 . . . . . . . .
7 . . . . . . . .
玩家 (黑棋) 的回合
可落子位置: [(2, 3), (3, 2), (4, 5), (5, 4)]
请输入落子位置 (格式: x y): 2 3
tkinter 或 pygame 实现更直观的图形化界面。希望这个程序能满足你的需求!如果有其他问题,欢迎继续提问。