python字符型五子棋人机对战
时间: 2023-10-01 08:03:39 浏览: 154
基于python实现五子棋人机对战
5星 · 资源好评率100%
好的,我可以给你提供一个简单的Python字符型五子棋人机对战程序的思路,供你参考。
思路如下:
1. 创建一个 15x15 的二维数组,用来表示棋盘。用 0 表示没有棋子,用 1 表示黑棋,用 2 表示白棋。
2. 实现一个函数,用来在控制台上打印出当前的棋盘。
3. 实现一个函数,用来判断当前棋局是否已经结束。判断方式可以是:检查每个位置的上下左右、左上右下、右上左下三个方向上是否有五颗相同颜色的棋子。
4. 实现一个函数,用来让人类玩家下棋。该函数可以从控制台读取玩家输入的坐标,并将其放到棋盘上。
5. 实现一个函数,用来让 AI 玩家下棋。该函数可以使用一些简单的算法,如随机下棋、简单的搜索等方式来下棋。
6. 编写一个主函数,用来调用以上函数,实现人机对战的游戏流程。在每次下棋后,需要判断是否有一方已经胜利,如果是,游戏结束并输出胜利方。如果没有胜利方,则交换下棋顺序。
这里提供一个简单的代码示例,可以让你参考一下:
```python
import random
# 定义棋盘大小
BOARD_SIZE = 15
# 定义棋子颜色
EMPTY = 0
BLACK = 1
WHITE = 2
# 定义游戏结束状态
NOT_OVER = 0
BLACK_WIN = 1
WHITE_WIN = 2
DRAW = 3
# 定义棋盘
board = [[EMPTY for j in range(BOARD_SIZE)] for i in range(BOARD_SIZE)]
# 打印棋盘
def print_board():
print(" ", end="")
for i in range(BOARD_SIZE):
print(chr(ord('a') + i), end="")
print()
for i in range(BOARD_SIZE):
print(i+1, end="")
for j in range(BOARD_SIZE):
if board[i][j] == EMPTY:
print(".", end="")
elif board[i][j] == BLACK:
print("X", end="")
else:
print("O", end="")
print()
# 判断游戏是否结束
def is_over():
# 判断行
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE-4):
if board[i][j] != EMPTY and board[i][j] == board[i][j+1] and board[i][j] == board[i][j+2] and board[i][j] == board[i][j+3] and board[i][j] == board[i][j+4]:
return BLACK_WIN if board[i][j] == BLACK else WHITE_WIN
# 判断列
for i in range(BOARD_SIZE-4):
for j in range(BOARD_SIZE):
if board[i][j] != EMPTY and board[i][j] == board[i+1][j] and board[i][j] == board[i+2][j] and board[i][j] == board[i+3][j] and board[i][j] == board[i+4][j]:
return BLACK_WIN if board[i][j] == BLACK else WHITE_WIN
# 判断左上右下斜线
for i in range(4, BOARD_SIZE):
for j in range(4, BOARD_SIZE):
if board[i][j] != EMPTY and board[i][j] == board[i-1][j-1] and board[i][j] == board[i-2][j-2] and board[i][j] == board[i-3][j-3] and board[i][j] == board[i-4][j-4]:
return BLACK_WIN if board[i][j] == BLACK else WHITE_WIN
# 判断左下右上斜线
for i in range(BOARD_SIZE-4):
for j in range(4, BOARD_SIZE):
if board[i][j] != EMPTY and board[i][j] == board[i+1][j-1] and board[i][j] == board[i+2][j-2] and board[i][j] == board[i+3][j-3] and board[i][j] == board[i+4][j-4]:
return BLACK_WIN if board[i][j] == BLACK else WHITE_WIN
# 判断是否平局
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
if board[i][j] == EMPTY:
return NOT_OVER
return DRAW
# 人类玩家下棋
def human_player(color):
while True:
try:
pos = input("请下棋,例如a1:")
x = ord(pos[0]) - ord('a')
y = int(pos[1:]) - 1
if x < 0 or x >= BOARD_SIZE or y < 0 or y >= BOARD_SIZE or board[y][x] != EMPTY:
raise Exception()
board[y][x] = color
break
except:
print("请重新输入")
# AI 玩家下棋
def ai_player(color):
while True:
x = random.randint(0, BOARD_SIZE-1)
y = random.randint(0, BOARD_SIZE-1)
if board[y][x] == EMPTY:
board[y][x] = color
break
# 主函数
def main():
print("欢迎来到五子棋游戏!")
print_board()
# 交替下棋,直到游戏结束
turn = BLACK
while True:
if turn == BLACK:
print("黑方下棋")
human_player(BLACK)
else:
print("白方下棋")
ai_player(WHITE)
print_board()
result = is_over()
if result != NOT_OVER:
break
turn = WHITE if turn == BLACK else BLACK
# 输出游戏结果
if result == BLACK_WIN:
print("黑方获胜!")
elif result == WHITE_WIN:
print("白方获胜!")
else:
print("平局!")
# 运行主函数
if __name__ == "__main__":
main()
```
这个程序使用了一个简单的随机下棋的算法来实现 AI 玩家。你可以根据自己的需要来实现更复杂的算法,以提高 AI 的水平。
阅读全文