用python编写一个字符界面的五子棋人机对战程序。
时间: 2023-10-13 21:04:58 浏览: 89
基于python实现五子棋人机对战
5星 · 资源好评率100%
好的,接下来我将给您提供一个简单的五子棋人机对战程序的示例代码,仅供参考。
```python
import os
# 定义棋盘大小
BOARD_SIZE = 15
# 定义棋子类型
PLAYER_CHESS = '●'
AI_CHESS = '○'
EMPTY_CHESS = ' '
# 创建一个空棋盘
def create_board():
board = []
for i in range(BOARD_SIZE):
row = [EMPTY_CHESS] * BOARD_SIZE
board.append(row)
return board
# 打印当前棋盘
def print_board(board):
# 打印列标记
print(' ', end='')
for i in range(BOARD_SIZE):
print(chr(i+ord('A')), end=' ')
print()
# 打印行标记和棋盘
for i in range(BOARD_SIZE):
print(' %2d' % (i+1), end=' ')
for j in range(BOARD_SIZE):
print(board[i][j], end=' ')
print()
# 判断某个位置是否可以下棋
def is_valid_move(board, row, col):
return board[row][col] == EMPTY_CHESS
# 下棋
def make_move(board, row, col, chess):
board[row][col] = chess
# 悔棋
def undo_move(board, row, col):
board[row][col] = EMPTY_CHESS
# 判断某个位置是否连成五子
def is_five(board, row, col):
chess = board[row][col]
if chess == EMPTY_CHESS:
return False
# 水平方向
count = 1
for i in range(1, 5):
if col-i < 0 or board[row][col-i] != chess:
break
count += 1
for i in range(1, 5):
if col+i >= BOARD_SIZE or board[row][col+i] != chess:
break
count += 1
if count >= 5:
return True
# 垂直方向
count = 1
for i in range(1, 5):
if row-i < 0 or board[row-i][col] != chess:
break
count += 1
for i in range(1, 5):
if row+i >= BOARD_SIZE or board[row+i][col] != chess:
break
count += 1
if count >= 5:
return True
# 左上到右下方向
count = 1
for i in range(1, 5):
if row-i < 0 or col-i < 0 or board[row-i][col-i] != chess:
break
count += 1
for i in range(1, 5):
if row+i >= BOARD_SIZE or col+i >= BOARD_SIZE or board[row+i][col+i] != chess:
break
count += 1
if count >= 5:
return True
# 左下到右上方向
count = 1
for i in range(1, 5):
if row+i >= BOARD_SIZE or col-i < 0 or board[row+i][col-i] != chess:
break
count += 1
for i in range(1, 5):
if row-i < 0 or col+i >= BOARD_SIZE or board[row-i][col+i] != chess:
break
count += 1
if count >= 5:
return True
return False
# 判断棋局是否结束
def is_game_over(board):
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
if is_valid_move(board, i, j) and (is_five(board, i, j, PLAYER_CHESS) or is_five(board, i, j, AI_CHESS)):
return True
return False
# AI下棋
def ai_move(board):
# 找到所有可以下棋的位置
candidates = []
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
if is_valid_move(board, i, j):
candidates.append((i, j))
# 评估每个位置的得分,并选出最高分的位置作为AI下棋的位置
best_score = -1
best_move = None
for move in candidates:
row, col = move
score = evaluate_move(board, row, col, AI_CHESS)
if score > best_score:
best_score = score
best_move = move
return best_move
# 评估某个位置的得分
def evaluate_move(board, row, col, chess):
# 评估在该位置下棋后,横向、竖向、左上到右下、右上到左下四个方向上的连子数量
count1 = count2 = count3 = count4 = 0
for i in range(1, 5):
if col-i < 0 or board[row][col-i] != chess:
break
count1 += 1
for i in range(1, 5):
if col+i >= BOARD_SIZE or board[row][col+i] != chess:
break
count1 += 1
for i in range(1, 5):
if row-i < 0 or board[row-i][col] != chess:
break
count2 += 1
for i in range(1, 5):
if row+i >= BOARD_SIZE or board[row+i][col] != chess:
break
count2 += 1
for i in range(1, 5):
if row-i < 0 or col-i < 0 or board[row-i][col-i] != chess:
break
count3 += 1
for i in range(1, 5):
if row+i >= BOARD_SIZE or col+i >= BOARD_SIZE or board[row+i][col+i] != chess:
break
count3 += 1
for i in range(1, 5):
if row+i >= BOARD_SIZE or col-i < 0 or board[row+i][col-i] != chess:
break
count4 += 1
for i in range(1, 5):
if row-i < 0 or col+i >= BOARD_SIZE or board[row-i][col+i] != chess:
break
count4 += 1
# 根据连子数量计算得分
score = max(count1, count2, count3, count4)
return score
# 主函数
def main():
# 创建一个空棋盘
board = create_board()
# 打印棋盘
print_board(board)
# 循环下棋
while True:
# 玩家下棋
while True:
move = input('请下棋(例如:A3):')
if len(move) == 2 and move[0].isalpha() and move[1].isdigit():
col = ord(move[0]) - ord('A')
row = int(move[1:]) - 1
if is_valid_move(board, row, col):
make_move(board, row, col, PLAYER_CHESS)
break
print('无效的位置,请重新输入!')
# 打印棋盘
os.system('cls' if os.name == 'nt' else 'clear')
print_board(board)
# 判断游戏是否结束
if is_game_over(board):
print('游戏结束,你赢了!')
break
# AI下棋
row, col = ai_move(board)
make_move(board, row, col, AI_CHESS)
# 打印棋盘
os.system('cls' if os.name == 'nt' else 'clear')
print_board(board)
# 判断游戏是否结束
if is_game_over(board):
print('游戏结束,你输了!')
break
# 启动主函数
if __name__ == '__main__':
main()
```
该程序采用了简单的极大极小算法来实现AI下棋的策略,如果您需要更高级的算法,可以参考其他资料。
阅读全文