以python语言编写一个五子棋人机对战程序
时间: 2023-06-16 19:03:09 浏览: 76
好的,下面是一个简单的五子棋人机对战程序。这个程序使用了一个简单的启发式算法来评估每个空位的分数,并选择分数最高的空位下棋。
```python
import random
# 定义棋盘大小和空格符号
BOARD_SIZE = 15
EMPTY = '.'
# 定义玩家和AI的棋子形状
player = 'O'
ai = 'X'
# 初始化棋盘
def init_board():
board = []
for i in range(BOARD_SIZE):
row = [EMPTY] * BOARD_SIZE
board.append(row)
return board
# 打印棋盘
def print_board(board):
print(" ", end="")
for x in range(BOARD_SIZE):
print("{0:5}".format(x+1), end="")
print()
for i in range(BOARD_SIZE):
print("{0:2d}".format(i+1), end="")
for j in range(BOARD_SIZE):
print("{0:5}".format(board[i][j]), end="")
print()
# 判断是否有五子连珠
def is_win(board, player):
# 水平方向
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE - 4):
if (board[i][j] == player and
board[i][j+1] == player and
board[i][j+2] == player and
board[i][j+3] == player and
board[i][j+4] == player):
return True
# 垂直方向
for i in range(BOARD_SIZE - 4):
for j in range(BOARD_SIZE):
if (board[i][j] == player and
board[i+1][j] == player and
board[i+2][j] == player and
board[i+3][j] == player and
board[i+4][j] == player):
return True
# 左上到右下方向
for i in range(BOARD_SIZE - 4):
for j in range(BOARD_SIZE - 4):
if (board[i][j] == player and
board[i+1][j+1] == player and
board[i+2][j+2] == player and
board[i+3][j+3] == player and
board[i+4][j+4] == player):
return True
# 右上到左下方向
for i in range(4, BOARD_SIZE):
for j in range(BOARD_SIZE - 4):
if (board[i][j] == player and
board[i-1][j+1] == player and
board[i-2][j+2] == player and
board[i-3][j+3] == player and
board[i-4][j+4] == player):
return True
return False
# 计算空位的分数
def evaluate(board):
score = 0
# 计算每个空位的分数
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
if board[i][j] != EMPTY:
continue
# 计算在该位置下棋对玩家的影响
board[i][j] = player
player_score = get_score(board)
board[i][j] = ai
# 计算在该位置下棋对AI的影响
ai_score = get_score(board)
# 计算分数并加权平衡
score += ai_score - player_score
board[i][j] = EMPTY
return score
# 获取当前棋盘状态的分数
def get_score(board):
score = 0
# 水平方向
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE - 4):
score += get_score_by_row(board[i][j:j+5])
# 垂直方向
for i in range(BOARD_SIZE - 4):
for j in range(BOARD_SIZE):
col = [board[i+k][j] for k in range(5)]
score += get_score_by_row(col)
# 左上到右下方向
for i in range(BOARD_SIZE - 4):
for j in range(BOARD_SIZE - 4):
diagonal = [board[i+k][j+k] for k in range(5)]
score += get_score_by_row(diagonal)
# 右上到左下方向
for i in range(4, BOARD_SIZE):
for j in range(BOARD_SIZE - 4):
diagonal = [board[i-k][j+k] for k in range(5)]
score += get_score_by_row(diagonal)
return score
# 根据一行棋子计算分数
def get_score_by_row(row):
score = 0
ai_count = row.count(ai)
player_count = row.count(player)
empty_count = row.count(EMPTY)
if ai_count == 5:
score += 100000
elif ai_count == 4 and empty_count == 1:
score += 10000
elif ai_count == 3 and empty_count == 2:
score += 1000
elif ai_count == 2 and empty_count == 3:
score += 100
if player_count == 4 and empty_count == 1:
score -= 10000
elif player_count == 3 and empty_count == 2:
score -= 1000
elif player_count == 2 and empty_count == 3:
score -= 100
return score
# AI下棋
def ai_move(board):
best_score = -100000
best_move = None
# 遍历所有空位
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
if board[i][j] != EMPTY:
continue
# 在该位置下棋,并计算分数
board[i][j] = ai
score = evaluate(board)
board[i][j] = EMPTY
# 如果分数最高,就记录该位置和分数
if score > best_score:
best_score = score
best_move = (i, j)
# 在最优位置下棋
board[best_move[0]][best_move[1]] = ai
print("AI在 ({0}, {1}) 下棋".format(best_move[0]+1, best_move[1]+1))
# 玩家下棋
def player_move(board):
while True:
try:
# 输入坐标
x, y = input("请输入坐标(如:3,4):").split(",")
x = int(x) - 1
y = int(y) - 1
# 检查坐标是否合法
if x < 0 or x >= BOARD_SIZE or y < 0 or y >= BOARD_SIZE:
print("坐标不合法,请重新输入")
continue
if board[x][y] != EMPTY:
print("该位置已经有棋子,请重新输入")
continue
# 在该位置下棋
board[x][y] = player
break
except ValueError:
print("坐标不合法,请重新输入")
# 主函数
def main():
board = init_board()
print_board(board)
while True:
player_move(board)
print_board(board)
if is_win(board, player):
print("您赢了!")
break
ai_move(board)
print_board(board)
if is_win(board, ai):
print("AI赢了!")
break
if __name__ == "__main__":
main()
```
这个程序还有很多可以改进的地方,比如增加难度、改进评估函数等等。但是,希望这个简单的实现可以帮助您入门五子棋人机对战的编程!
阅读全文