目录
前言
本文介绍一个基于Python3实现的命令行版《中国象棋》小游戏(PvP)。
代码均由本文作者撰写,无其他参考,欢迎下载使用, 转载请注明出处。
本文仅展示了一部分典型代码,其余代码及注释请读者在文末源码中自行对照查看。
一、游戏说明及效果展示
1. 游戏说明
① 游戏开始后,红黑双方轮流通过输入终端提示的字符来控制游戏进行。
② 在选择一枚棋子后,程序会让你选择想要对其操作的方式( 选择棋子移动的方向 ),然后会打印棋子在这个“ 方向 ”上可移动的步数,棋手再次输入对应字符即可;
③ 若误选了棋子,你可以输入“ 10 ”来重新选择棋子;
④ 可以在选择棋子时,输入“ 666 ”来投降。
2. 效果展示
如下图,游戏开始, 红黑双方轮流走子
如下图,显示了游戏的部分操作提示页面( 包含向棋手打印棋盘上可用的棋子、当前所操作棋子的可移动方式,以及错误输入提示信息等 )
实际情景 ①:红方炮击黑将,胜利!
实际情景 ②:棋子“马-1”面前有“绊脚石”,无法向上方跨“日字”。
实际情景 ③:黑方在走棋过程中选择投降( 选取棋子时输入"666" )
二、实现逻辑
1. 实时打印棋盘
① 用一维列表board_info, 来存储棋盘上各点位的信息, 如下:
board_info = [ obj_0, obj_1, boj_2, boj_3, ...... , obj_86, obj_87, obj_88, obj_89]
因为棋盘上交叉的线(横10纵9)组成了90个二维坐标, 每一个坐标点上都应该有一个棋子( 包括"空子" )对象
而作为列表board_info的元素, 每一个棋子对象都在board_info中有对应的索引值( 即一个一维坐标 ), 可将其化为二维坐标(x, y), 也更便于后续移动棋子时的计算。
② 分别用二维列表red_survial/black_survial, 来存储目前双方剩余的棋子(每个子列表包含了一个棋子对象和其在board_info上的坐标信息), 如下:
red_survial/black_survial = [ [ obj_a, position_a ] , ..., [ obj_z, position_z ] ]
③ playing()用于实时打印游戏内容, 包括棋盘和操作提示界面。
每次当一方走完棋子后, 我们遍历board_info列表, 打印每个坐标上棋子对象的content属性(是棋子图标的字符串), 打印时根据根据棋子对象的camp属性(代表棋子的阵营)来为其“上色 ”。
这里, “上色 ”是使用 ANSI 转义序列,在终端打印出彩色的命令行字符( 打印出红黑棋子 )。
2. 棋子移动
①. 定义了各种棋子的类( 帅/将 士 象 马 车 炮 兵/卒 空子 )。
它们有自己的属性( 阵营camp、对应的能打印出的图标字符串content ) 和 方法( 各自的行子方式move_way(), 方法的具体实现以注释形式写在类中 )
举个栗子,棋子“车”向左移动的相关代码如下:
def move_way(self, position):
# x表示棋子在第几行(从上到下递增)
# y表示棋子在本行的第几列(从左到右递增)
y = position % 9
x = int((position - y) / 9)
global rounds_count
choice = '00'
while True:
if choice == '10':
continue
toward = int(input("请选择移动方向:(0-向左行 1-向右行 2-向上行 3-向下行)"))
# 车向左行的逻辑----向左依次判断 ①遇空白可走,直至到最左边 ②遇第一个敌子可吃,退出循环 ③遇到友军,退出循环
if toward == 0:
# target是车的目的点的水平坐标, 表示在本水平行移动
target = y - 1
# ava_steps存放可走的步数(即车的水平坐标y减去目的点水平坐标target)
ava_steps = []
# 在target左移到左边界及之前
while target >= 0:
# 判定目的点位上是否为空子
if judge_ava(self.camp, (x * 9 + target)) == 'empty':
# 可走, 存入
ava_steps.append(y - target)
# 判定目的点位上是否为敌方棋子
elif judge_ava(self.camp, (x * 9 + target)) == 'eatable':
# 可走, 存入, 退出循环
ava_steps.append(y - target)
break
# 判定目的点位上是否为友方棋子
elif judge_ava(self.camp, x * 9 + target) == 'friendly':
# 不能走, 退出循环
break
target -= 1
ava_dict = {}
for i in range(len(ava_steps)):
ava_dict[str(ava_steps[i])] = ava_steps[i]
while True:
print("请输入要行的步数:", ava_steps, " ***若待选步数为[空],则表明该棋子不能这样走 | 你可以输入 10 来返回重新选择走棋***)")
choice = input()
if (choice not in ava_dict) and (choice != '10'):
print("不能那样走!!!")
elif choice in ava_dict:
motivation(position, x * 9 + (y - ava_dict[choice]))
break
elif choice == '10':
rounds_count -= 1
break
# 车向右行
if toward == 1:
# 此处省略, 具体在源码里
# 车向上行
if toward == 2:
# 此处省略, 具体在源码里
# 车向下行
if toward == 3:
# 此处省略, 具体在源码里
# 当棋手输入错误字符, 提示错误并跳出本次循环, 让棋手重新输入字符
else:
print("请输入0-3!!!")
continue
break
②. judge_ava()用于判断棋盘上某点对于当前被操作的棋子而言,是否可以交互。
(i) 空子点——>可走
(ii) 敌子点——>可吃
(iii)友子点——>无法交互
def judge_ava(camp, posti):
position = int(posti)
if board_info[position].content == ' ○ ':
# 目的点上是空白,可走
return 'empty'
elif board_info[position].camp != camp:
# 目的点上是敌军,可吃
return 'eatable'
elif board_info[position].camp == camp:
# 目的点上是友军,不能与之交互
return 'friendly'
③. motivation()用于移动棋子的实现。
(i) 将目的落子点上的对象替换为当前被操作的棋子对象
(ii) 并将被操作棋子的出发点替换为空子对象
def motivation(source, destination):
global obj_empty_piece
global board_info
global winner_count
# 判断被吃掉的是否是 帅(将)
if board_info[destination].content == ' 将 ':
# 将被吃, 红军胜
winner_count = 1
elif board_info[destination].content == ' 帅 ':
# 帅被吃, 黑军胜
winner_count = 2
# 替换棋子坐标
board_info[destination] = board_info[source]
board_info[source] = obj_empty_piece
④. init()用于棋盘初始化, 按中国象棋规则来摆上对应的棋子( 将board_info中各点替换为对应初始对象 )。
三、总结与完整源代码
程序实现了简单的命令行《中国象棋》, 但暂时没有“将军”和“悔棋”等功能,读者可以自行按需添加实现。(比如穷举所有棋子可走的落点并检测是否有某一项可以导致游戏结束、用一个数据结构来记录前n步的棋盘摆放情况以实现悔棋时的棋盘回溯)
谢谢你看到最后!!!
源码如下:chessGame.py
# 帅的类
class Shuai:
def __init__(self):
self.content = ' 帅 '
self.camp = '红'
def move_way(self, position):
y = position % 9
x = int((position - y) / 9)
global rounds_count
choice = '00'
while True:
if choice == '10':
continue
toward = int(input("请选择移动方向:(0-向左行 1-向右行 2-向上行 3-向下行)"))
# 帅向左行的逻辑----最多走一步,若左边是敌子,可吃,若是友军,则不能走,且不能向左走出营地(y' >= 3)
if toward == 0:
target = y - 1
ava_steps = []
if target >= 3:
if judge_ava(self.camp, (x * 9 + target)) == 'empty':
ava_steps.append(y - target)
elif judge_ava(self.camp, (x * 9 + target)) == 'eatable':
ava_steps.append(y - target)
elif judge_ava(self.camp, x * 9 + target) == 'friendly':
pass
target -= 1
ava_dict = {}
for i in range(len(ava_steps)):
ava_dict[str(ava_steps[i])] = ava_steps[i]
while True:
print("请输入要行的步数:", ava_steps, " ***若待选步数为[空],则表明该棋子不能这样走 | 你可以输入 10 来返回重新选择走棋***)")
choice = input()
if (choice not in ava_dict) and (choice != '10'):
print("不能那样走!!!")
elif choice in ava_dict:
motivation(position, x * 9 + (y - ava_dict[choice]))
break
elif choice == '10':
rounds_count -= 1
break
# 帅向右行逻辑与向左行相同(y' <= 5)
elif toward == 1:
target = y + 1
ava_steps = []
if target <= 5:
if judge_ava(self.camp, x * 9 + target) == 'empty':
ava_steps.append(target - y)
elif judge_ava(self.camp, x * 9 + target) == 'eatable':
ava_steps.append(target - y)
elif judge_ava(self.camp, x * 9 + target) == 'friendly':
pass
target += 1
ava_dict = {}
for i in range(len(ava_steps)):
ava_dict[str(ava_steps[i])] = ava_steps[i]
while True:
print("请输入要行的步数:", ava_steps, " ***若待选步数为[空],则表明该棋子不能这样走 | 你可以输入 10 来返回重新选择走棋***)")
choice = input()
if (choice not in ava_dict) and (choice != '10'):
print("不能那样走!!!")
elif choice in ava_dict:
motivation(position, x * 9 + (y + ava_dict[choice]))
break
elif choice == '10':
rounds_count -= 1
break
# 帅向上行的逻辑----最多走一步,若上边边是敌子,可吃,若是友军,则不能走,且不能向上走出营地(x' >= 7)
elif toward == 2:
target = x - 1
ava_steps = []
if target >= 7:
if judge_ava(self.camp, target * 9 + y) == 'empty':
ava_steps.append(x - target)
elif judge_ava(self.camp, target * 9 + y) == 'eatable':
ava_steps.append(x - target)
elif judge_ava(self.camp, target * 9 + y) == 'friendly':
pass
target -= 1
ava_dict = {}
for i in range(len(ava_steps)):
ava_dict[str(ava_steps[i])] = ava_steps[i]
while True:
print("请输入要行的步数:", ava_steps, " ***若待选步数为[空],则表明该棋子不能这样走 | 你可以输入 10 来返回重新选择走棋***)")
choice = input()
if (choice not in ava_dict) and (choice != '10'):
print("不能那样走!!!")
elif choice in ava_dict:
motivation(position, (x - ava_dict[choice]) * 9 + y)
break
elif choice == '10':
rounds_count -= 1
break
# 帅向下行逻辑与上行相同(x' <= 9)
elif toward == 3:
target = x + 1
ava_steps = []
if target <= 9:
if judge_ava(self.camp, target * 9 + y) == 'empty':
ava_steps.append(target - x)
elif judge_ava(self.camp, target * 9 + y) == 'eatable':
ava_steps.append(target - x)
elif judge_ava(self.camp, target * 9 + y) == 'friendly':
pass
target += 1
ava_dict = {}
for i in range(len(ava_steps)):
ava_dict[str(ava_steps[i])] = ava_steps[i]
while True:
print("请输入要行的步数:", ava_steps, " ***若待选步数为[空],则表明该棋子不能这样走 | 你可以输入 10 来返回重新选择走棋***)")
choice = input()
if (choice not in ava_dict) and (choice != '10'):
print("不能那样走!!!")
elif choice in ava_dict:
motivation(position, (x + ava_dict[choice]) * 9 + y)
break
elif choice == '10':
rounds_count -= 1
break
else:
print("请输入0-3!!!")
continue
break
# 将的类
class Jiang:
def __init__(self):
self.content = ' 将 '
self.camp = '黑'
def move_way(self, position):
y = position % 9
x = int((position - y) / 9)
global rounds_count
choice = '00'
while True:
if choice == '10':
continue
toward = int(input("请选择移动方向:(0-向左行 1-向右行 2-向上行 3-向下行)"))
# 将向左行的逻辑----最多走一步,若左边是敌子,可吃,若是友军,则不能走,且不能向左走出营地(y' >= 3)
if toward == 0:
target = y - 1
ava_steps = []
if target >= 3:
if judge_ava(self.camp, (x * 9 + target)) == 'empty':
ava_steps.append(y - target)
elif judge_ava(self.camp, (x * 9 + target)) == 'eatable':
ava_steps.append(y - target)
elif judge_ava(self.camp, x * 9 + target) == 'friendly':
pass
target -= 1
ava_dict = {}
for i in range(len(ava_steps)):
ava_dict[str(ava_steps[i])] = ava_steps[i]
while True:
print("请输入要行的步数:", ava_steps, " ***若待选步数为[空],则表明该棋子不能这样走 | 你可以输入 10 来返回重新选择走棋***)")
choice = input()
if (choice not in ava_dict) and (choice != '10'):
print("不能那样走!!!")
elif choice in ava_dict:
motivation(position, x * 9 + (y - ava_dict[choice]))
break
elif choice == '10':
rounds_count -= 1
break
# 将向右行逻辑与向左行相同(y' <= 5)
elif toward == 1:
target = y + 1
ava_steps = []
if target <= 5:
if judge_ava(self.camp, x * 9 + target) == 'empty':
ava_steps.append(target - y)
elif judge_ava(self.camp, x * 9 + target) == 'eatable':
ava_steps.append(target - y)
elif judge_ava(self.camp, x * 9 + target) == 'friendly':
pass
target += 1
ava_dict = {}
for i in range(len(ava_steps)):
ava_dict[str(ava_steps[i])] = ava_steps[i]
while True:
print("请输入要行的步数:", ava_steps, " ***若待选步数为[空],则表明该棋子不能这样走 | 你可以输入 10 来返回重新选择走棋***)")
choice = input()
if (choice not in ava_dict) and (choice != '10'):
print("不能那样走!!!")
elif choice in ava_dict:
motivation(position, x * 9 + (y + ava_dict[choice]))
break
elif choice == '10':
rounds_count -= 1
break
# 将向上行的逻辑----最多走一步,若上边边是敌子,可吃,若是友军,则不能走,且不能向上走出营地(x' >= 0)
elif toward == 2:
target = x - 1
ava_steps = []
if target >= 0:
if judge_ava(self.camp, target * 9 + y) == 'empty':
ava_steps.append(x - target)
elif judge_ava(self.camp, target * 9 + y) == 'eatable':
ava_steps.append(x - target)
elif judge_ava(self.camp, target * 9 + y) == 'friendly':
pass
target -= 1
ava_dict = {}
for i in range(len(ava_steps)):
ava_dict[str(ava_steps[i])] = ava_steps[i]
while True:
print("请输入要行的步数:", ava_steps, " ***若待选步数为[空],则表明该棋子不能这样走 | 你可以输入 10 来返回重新选择走棋***)")
choice = input()
if (choice not in ava_dict) and (choice != '10'):
print("不能那样走!!!")
elif choice in ava_dict:
motivation(position, (x - ava_dict[choice]) * 9 + y)
break
elif choice == '10':
rounds_count -= 1
break
# 将向下行逻辑与上行相同(x' <= 2)
elif toward == 3:
target = x + 1
ava_steps = []
if target <= 2:
if judge_ava(self.camp, target * 9 + y) == 'empty':
ava_steps.append(target - x)
elif judge_ava(self.camp, target * 9 + y) == 'eatable':
ava_steps.append(target - x)
elif judge_ava(self.camp, target * 9 + y) == 'friendly':
pass
target += 1
ava_dict = {}
for i in range(len(ava_steps)):
ava_dict[str(ava_steps[i])] = ava_steps[i]
while True:
print("请输入要行的步数:", ava_steps, " ***若待选步数为[空],则表明该棋子不能这样走 | 你可以输入 10 来返回重新选择走棋***)")
choice = input()
if (choice not in ava_dict) and (choice != '10'):
print("不能那样走!!!")
elif choice in ava_dict:
motivation(position, (x + ava_dict[choice]) * 9 + y)
break
elif choice == '10':
rounds_count -= 1
break
else:
print("请输入0-3!!!")
continue
break
# 士的类
class Shi:
def __init__(self, camp, num):
self.content = '士-' + str(num)
self.camp = camp
def move_way(self, position):
y = position % 9
x = int((position - y) / 9)
global rounds_count
choice = '00'
while True:
if choice == '10':
continue
# 判断是否是是黑方的士
if x <= 2:
toward = int(input("请选择移动方向:(0-向左上行 1-向左下行 2-向右上行 3-向右下行)"))
# 士向左上行的逻辑----最多走一步(走口子),若左上是敌子,可吃,若是友军,则不能走,且不能向左走出营地(y' >= 3, x' >= 0)
if toward == 0:
target = y - 1
target_x = x - 1
ava_steps = []
if target >= 3 and target_x >= 0:
if judge_ava(self.camp, (target_x * 9 + target)) == 'empty':
ava_steps.append(1)
elif judge_ava(self.camp, (target_x * 9 + target)) == 'eatable':
ava_steps.append(1)
elif judge_ava(self.camp, target_x * 9 + target) == 'friendly':
pass
target -= 1
target_x -= 1
ava_dict = {}
for i in range(len(ava_steps)):
ava_dict[str(ava_steps[i])] = ava_steps[i]
while True:
print("请输入要行的步数:", ava_steps, " ***若待选步数为[空],则表明该棋子不能这样走 | 你可以输入 10 来返回重新选择走棋***)")
choice = input()
if (choice not in ava_dict) and (choice != '10'):
print("不能那样走!!!")
elif choice in ava_dict:
motivation(position, (x - 1) * 9 + (y - 1))
break
elif choice == '10':
rounds_count -= 1
break
# 士向左下行逻辑与向左上行相同(y' >= 3, x' <= 2)
elif toward == 1:
target = y - 1
target_x = x + 1
ava_steps = []
if target >= 3 and target_x <= 2:
if judge_ava(self.camp, target_x * 9 + target) == 'empty':
ava_steps.append(1)
elif judge_ava(self.camp, target_x * 9 + target) == 'eatable':
ava_steps.append(1)
elif judge_ava(self.camp, target_x * 9 + target) == 'friendly':
pass
target -= 1
target_x += 1
ava_dict = {}
for i in range(len(ava_steps)):
ava_dict[str(ava_steps[i])] = ava_steps[i]
while True:
print("请输入要行的步数:", ava_steps, " ***若待选步数为[空],则表明该棋子不能这样走 | 你可以输入 10 来返回重新选择走棋***)")
choice = input()
if (choice not in ava_dict) and (choice != '10'):
print("不能那样走!!!")
elif choice in ava_dict:
motivation(position, (x + 1) * 9 + (y - 1))
break
elif choice == '10':
rounds_count -= 1
break
# 士向右上行逻辑与向左上行相同(y' <= 5, x' >= 0)
elif toward == 2:
target = y + 1
target_x = x - 1
ava_steps = []
if target <= 5 and target_x >= 0:
if judge_ava(self.camp, target_x * 9 + target) == 'empty':
ava_steps.append(1)
elif judge_ava(self.camp, target_x * 9 + target) == 'eatable':
ava_steps.append(1)
elif judge_ava(self.camp, target_x * 9 + target) == 'friendly':
pass
target += 1
target_x -= 1
ava_dict = {}
for i in range(len(ava_steps)):
ava_dict[str(ava_steps[i])] = ava_steps[i]
while True:
print("请输入要行的步数:", ava_steps, " ***若待选步数为[空],则表明该棋子不能这样走 | 你可以输入 10 来返回重新选择走棋***)")
choice = input()
if (choice not in ava_dict) and (choice != '10'):
print("不能那样走!!!")
elif choice in ava_dict:
motivation(position, (x - 1) * 9 + (y + 1))
break
elif choice == '10':
rounds_count -= 1
break
# 士向右下行逻辑与向左上行相同(y' <= 5, x' <= 2)
elif toward == 3:
target = y + 1
target_x = x + 1
ava_steps = []
if target <= 5 and target_x <= 2:
if judge_ava(self.camp, target_x * 9 + target) == 'empty':
ava_steps.append(1)
elif judge_ava(self.camp, target_x * 9 + target) == 'eatable':
ava_steps.append(1)
elif judge_ava(self.camp, target_x * 9 + target) == 'friendly':
pass
target += 1
target_x += 1
ava_dict = {}
for i in range(len(ava_steps)):
ava_dict[str(ava_steps[i])] = ava_steps[i]
while True:
print("请输入要行的步数:", ava_steps, " ***若待选步数为[空],则表明该棋子不能这样走 | 你可以输入 10 来返回重新选择走棋***)")
choice = input()
if (choice not in ava_dict) and (choice != '10'):
print("不能那样走!!!")
elif choice in ava_dict:
motivation(position, (x + 1) * 9 + (y + 1))
break
elif choice == '10':
rounds_count -= 1
break
else:
print("请输入0-3!!!")
continue
break
# 判断是否是红方的士
elif x >= 7:
toward = int(input("请选择移动方向:(0-向左上行 1-向左下行 2-向右上行 3-向右下行)"))
# 士向左上行的逻辑----最多走一步(走口字),若左上是敌子,可吃,若是友军,则不能走,且不能向左走出营地(y' >= 3, x' >= 7)
if toward == 0:
target = y - 1
target_x = x - 1
ava_steps = []
if target >= 3 and target_x >= 7:
if judge_ava(self.camp, (target_x * 9 + target)) == 'empty':
ava_steps.append(1)
elif judge_ava(self.camp, (target_x * 9 + target)) == 'eatable':
ava_steps.append(1)
elif judge_ava(self.camp, target_x * 9 + target) == 'friendly':
pass
target -= 1
target_x -= 1
ava_dict = {}
for i in range(len(ava_steps)):
ava_dict[str(ava_steps[i])] = ava_steps[i]
while True:
print("请输入要行的步数:", ava_steps, " ***若待选步数为[空],则表明该棋子不能这样走 | 你可以输入 10 来返回重新选择走棋***)")
choice = input()
if (choice not in ava_dict) and (choice != '10'):
print("不能那样走!!!")
elif choice in ava_dict:
motivation(position, (x - 1) * 9 + (y - 1))
break
elif choice == '10':
rounds_count -= 1
break
# 士向左下行逻辑与向左上行相同(y' >= 3, x' <= 9)
elif toward == 1:
target = y - 1
target_x = x + 1
ava_steps = []
if target >= 3 and target_x <= 9:
if judge_ava(self.camp, target_x * 9 + target) == 'empty':
ava_steps.append(1)
elif judge_ava(self.camp, target_x * 9 + target) == 'eatable':
ava_steps.append(1)
elif judge_ava(self.camp, target_x * 9 + target) == 'friendly':
pass
target -= 1
target_x += 1
ava_dict = {}
for i in range(len(ava_steps)):
ava_dict[str(ava_steps[i])] = ava_steps[i]
while True:
print("请输入要行的步数:", ava_steps, " ***若待选步数为[空],则表明该棋子不能这样走 | 你可以输入 10 来返回重新选择走棋***)")
choice = input()
if (choice not in ava_dict) and (choice != '10'):
print("不能那样走!!!")
elif choice in ava_dict:
motivation(position, (x + 1) * 9 + (y - 1))
break
elif choice == '10':
rounds_count -= 1
break
# 士向右上行逻辑与向左上行相同(y' <= 5, x' >= 7)
elif toward == 2:
target = y + 1
target_x = x - 1
ava_steps = []
if target <= 5 and target_x >= 7:
if judge_ava(self.camp, target_x * 9 + target) == 'empty':
ava_steps.append(1)
elif judge_ava(self.camp, target_x * 9 + target) == 'eatable':
ava_steps.append(1)
elif judge_ava(self.camp, target_x * 9 + target) == 'friendly':
pass
target += 1
target_x -= 1
ava_dict = {}
for i in range(len(ava_steps)):
ava_dict[str(ava_steps[i])] = ava_steps[i]
while True:
print("请输入要行的步数:", ava_steps, " ***若待选步数为[空],则表明该棋子不能这样走 | 你可以输入 10 来返回重新选择走棋***)")
choice = input()
if (choice not in ava_dict) and (choice != '10'):
print("不能那样走!!!")
elif choice in ava_dict:
motivation(position, (x - 1) * 9 + (y + 1))
break
elif choice == '10':
rounds_count -= 1
break
# 士向右下行逻辑与向左上行相同(y' <= 5, x' <= 9)
elif toward == 3:
target = y + 1
target_x = x + 1
ava_steps = []
if target <= 5 and target_x <= 9:
if judge_ava(self.camp, target_x * 9 + target) == 'empty':
ava_steps.append(1)
elif judge_ava(self.camp, target_x * 9 + target) == 'eatable':
ava_steps.append(1)
elif judge_ava(self.camp, target_x * 9 + target) == 'friendly':
pass
target += 1
target_x += 1
ava_dict = {}
for i in range(len(ava_steps)):
ava_dict[str(ava_steps[i])] = ava_steps[i]
while True:
print("请输入要行的步数:", ava_steps, " ***若待选步数为[空],则表明该棋子不能这样走 | 你可以输入 10 来返回重新选择走棋***)")
choice = input()
if (choice not in ava_dict) and (choice != '10'):
print("不能那样走!!!")
elif choice in ava_dict:
motivation(position, (x + 1) * 9 + (y + 1))
break
elif choice == '10':
rounds_count -= 1
break
else:
print("请输入0-3!!!")
continue
break
# 象的类
class Xiang:
def __init__(self, camp, num):
self.content = '象-' + str(num)
self.camp = camp
def move_way(self, position):
y = position % 9
x = int((position - y) / 9)
global rounds_count
choice = '00'
while True:
if choice == '10':
continue
# 判断是否是是黑方的象
if x <= 4:
toward = int(input("请选择移动方向:(0-向左上行 1-向左下行 2-向右上行 3-向右下行)"))
# 黑象向左上行的逻辑----最多走一步(走田字),若左上是敌子,可吃,若是友军,则不能走
# ("塞象眼":若田字中心有子,则无法走象)
if toward == 0:
target = y - 2
target_x = x - 2
center = (x - 1) * 9 + (y - 1)
ava_steps = []
if target >= 0 and target_x >= 0 and judge_ava(self.camp, center) == 'empty':
if judge_ava(self.camp, (target_x * 9 + target)) == 'empty':
ava_steps.append(1)
elif judge_ava(self.camp, (target_x * 9 + target)) == 'eatable':
ava_steps.append(1)
elif judge_ava(self.camp, target_x * 9 + target) == 'friendly':
pass
target -= 2
target_x -= 2
ava_dict = {}
for i in range(len(ava_steps)):
ava_dict[str(ava_steps[i])] = ava_steps[i]
while True:
print("请输入要行的步数:", ava_steps, " ***若待选步数为[空],则表明该棋子不能这样走 | 你可以输入 10 来返回重新选择走棋***)")
choice = input()
if (choice not in ava_dict) and (choice != '10'):
print("不能那样走!!!")
elif choice in ava_dict:
motivation(position, (x - 2) * 9 + (y - 2))
break
elif choice == '10':
rounds_count -= 1
break
# 黑象向左下行逻辑与向左上行相同,且不能向下渡过河界(x' <= 4)
elif toward == 1:
target = y - 2
target_x = x + 2
center = (x + 1) * 9 + (y - 1)
ava_steps = []
if target >= 0 and target_x <= 4 and judge_ava(self.camp, center) == 'empty':
if judge_ava(self.camp, target_x * 9 + target) == 'empty':
ava_steps.append(1)
elif judge_ava(self.camp, target_x * 9 + target) == 'eatable':
ava_steps.append(1)
elif judge_ava(self.camp, target_x * 9 + target) == 'friendly':
pass
target -= 2
target_x += 2
ava_dict = {}
for i in range(len(ava_steps)):
ava_dict[str(ava_steps[i])] = ava_steps[i]
while True:
print("请输入要行的步数:", ava_steps, " ***若待选步数为[空],则表明该棋子不能这样走 | 你可以输入 10 来返回重新选择走棋***)")
choice = input()
if (choice not in ava_dict) and (choice != '10'):
print("不能那样走!!!")
elif choice in ava_dict:
motivation(position, (x + 2) * 9 + (y - 2))
break
elif choice == '10':
rounds_count -= 1
break
# 黑象向右上行逻辑与向左上行相同
elif toward == 2:
target = y + 2
target_x = x - 2
center = (x - 1) * 9 + (y + 1)
ava_steps = []
if target <= 8 and target_x >= 0 and judge_ava(self.camp, center) == 'empty':
if judge_ava(self.camp, target_x * 9 + target) == 'empty':
ava_steps.append(1)
elif judge_ava(self.camp, target_x * 9 + target) == 'eatable':
ava_steps.append(1)
elif judge_ava(self.camp, target_x * 9 + target) == 'friendly':
pass
target += 2
target_x -= 2
ava_dict = {}
for i in range(len(ava_steps)):
ava_dict[str(ava_steps[i])] = ava_steps[i]
while True:
print("请输入要行的步数:", ava_steps, " ***若待选步数为[空],则表明该棋子不能这样走 | 你可以输入 10 来返回重新选择走棋***)")
choice = input()
if (choice not in ava_dict) and (choice != '10'):
print("不能那样走!!!")
elif choice in ava_dict:
motivation(position, (x - 2) * 9 + (y + 2))
break
elif choice == '10':
rounds_count -= 1
break
# 黑象向右下行逻辑与向左上行相同,且不能向下渡过河界(x' <= 4)
elif toward == 3:
target = y + 2
target_x = x + 2
center = (x + 1) * 9 + (y + 1)
ava_steps = []
if target <= 8 and target_x <= 4 and judge_ava(self.camp, center) == 'empty':
if judge_ava(self.camp, target_x * 9 + target) == 'empty':
ava_steps.append(1)
elif judge_ava(self.camp, target_x * 9 + target) == 'eatable':
ava_steps.append(1)
elif judge_ava(self.camp, target_x * 9 + target) == 'friendly':
pass
target += 2
target_x += 2
ava_dict = {}
for i in range(len(ava_steps)):
ava_dict[str(ava_steps[i])] = ava_steps[i]
while True:
print("请输入要行的步数:", ava_steps, " ***若待选步数为[空],则表明该棋子不能这样走 | 你可以输入 10 来返回重新选择走棋***)")
choice = input()
if (choice not in ava_dict) and (choice != '10'):
print("不能那样走!!!")
elif choice in ava_dict:
motivation(position, (x + 2) * 9 + (y + 2))
break
elif choice == '10':
rounds_count -= 1
break
else:
print("请输入0-3!!!")
continue
break
# 判断是否是红方的象
elif x >= 5:
toward = int(input("请选择移动方向:(0-向左上行 1-向左下行 2-向右上行 3-向右下行)"))
# 红象向左上行的逻辑----最多走一步(走田字),若左上是敌子,可吃,若是友军,则不能走,且不能向上渡过河界(x' >= 5)
# ("塞象眼":若田字中心有子,则无法走象)
if toward == 0:
target = y - 2
target_x = x - 2
center = (x - 1) * 9 + (y - 1)
ava_steps = []
if target >= 0 and target_x >= 5 and judge_ava(self.camp, center) == 'empty':
if judge_ava(self.camp, (target_x * 9 + target)) == 'empty':
ava_steps.append(1)
elif judge_ava(self.camp, (target_x * 9 + target)) == 'eatable':
ava_steps.append(1)
elif judge_ava(self.camp, target_x * 9 + target) == 'friendly':
pass
target -= 2
target_x -= 2
ava_dict = {}
for i in range(len(ava_steps)):
ava_dict[str(ava_steps[i])] = ava_steps[i]
while True:
print("请输入要行的步数:", ava_steps, " ***若待选步数为[空],则表明该棋子不能这样走 | 你可以输入 10 来返回重新选择走棋***)")
choice = input()
if (choice not in ava_dict) and (choice != '10'):
print("不能那样走!!!")
elif choice in ava_dict:
motivation(position, (x - 2) * 9 + (y - 2))
break
elif choice == '10':
rounds_count -= 1
break
# 红象向左下行逻辑与向左上行相同
elif toward == 1:
target = y - 2
target_x = x + 2
center = (x + 1) * 9 + (y - 1)
ava_steps = []
if target >= 0 and target_x <= 9 and judge_ava(self.camp, center) == 'empty':
if judge_ava(self.camp, target_x * 9 + target) == 'empty':
ava_steps.append(1)
elif judge_ava(self.camp, target_x * 9 + target) == 'eatable':
ava_steps.append(1)
elif judge_ava(self.camp, target_x * 9 + target) == 'friendly':
pass
target -= 2
target_x += 2
ava_dict = {}
for i in range(len(ava_steps)):
ava_dict[str(ava_steps[i])] = ava_steps[i]
while True:
print("请输入要行的步数:", ava_steps, " ***若待选步数为[空],则表明该棋子不能这样走 | 你可以输入 10 来返回重新选择走棋***)")
choice = input()
if (choice not in ava_dict) and (choice != '10'):
print("不能那样走!!!")
elif choice in ava_dict:
motivation(position, (x + 2) * 9 + (y - 2))
break
elif choice == '10':
rounds_count -= 1
break
# 红象向右上行逻辑与向左上行相同,且不能向上渡过河界(x' >= 5)
elif toward == 2:
target = y + 2
target_x = x - 2
center = (x - 1) * 9 + (y + 1)
ava_steps = []
if target <= 8 and target_x >= 5 and judge_ava(self.camp, center) == 'empty':
if judge_ava(self.camp, target_x * 9 + target) == 'empty':
ava_steps.append(1)
elif judge_ava(self.camp, target_x * 9 + target) == 'eatable':
ava_steps.append(1)
elif judge_ava(self.camp, target_x * 9 + target) == 'friendly':
pass
target += 2
target_x -= 2
ava_dict = {}
for i in range(len(ava_steps)):
ava_dict[str(ava_steps[i])] = ava_steps[i]
while True:
print("请输入要行的步数:", ava_steps, " ***若待选步数为[空],则表明该棋子不能这样走 | 你可以输入 10 来返回重新选择走棋***)")
choice = input()
if (choice not in ava_dict) and (choice != '10'):
print("不能那样走!!!")
elif choice in ava_dict:
motivation(position, (x - 2) * 9 + (y + 2))
break
elif choice == '10':
rounds_count -= 1
break
# 红象向右下行逻辑与向左上行相同
elif toward == 3:
target = y + 2
target_x = x + 2
center = (x + 1) * 9 + (y + 1)
ava_steps = []
if target <= 8 and target_x <= 9 and judge_ava(self.camp, center) == 'empty':
if judge_ava(self.camp, target_x * 9 + target) == 'empty':
ava_steps.append(1)
elif judge_ava(self.camp, target_x * 9 + target) == 'eatable':
ava_steps.append(1)
elif judge_ava(self.camp, target_x * 9 + target) == 'friendly':
pass
target += 2
target_x += 2
ava_dict = {}
for i in range(len(ava_steps)):
ava_dict[str(ava_steps[i])] = ava_steps[i]
while True:
print("请输入要行的步数:", ava_steps, " ***若待选步数为[空],则表明该棋子不能这样走 | 你可以输入 10 来返回重新选择走棋***)")
choice = input()
if (choice not in ava_dict) and (choice != '10'):
print("不能那样走!!!")
elif choice in ava_dict:
motivation(position, (x + 2) * 9 + (y + 2))
break
elif choice == '10':
rounds_count -= 1
break
else:
print("请输入0-3!!!")
continue
break
# 马的类
class Ma:
def __init__(self, camp, num):
self.content = '马-' + str(num)
self.camp = camp
def move_way(self, position):
y = position % 9
x = int((position - y) / 9)
global rounds_count
choice = '00'
# 定义“挡马块”
left_block = x * 9 + (y - 1)
right_block = x * 9 + (y + 1)
up_block = (x - 1) * 9 + y
down_block = (x + 1) * 9 + y
while True:
if choice == '10':
continue
toward = int(input("请选择移动方向(0=>朝左跨日 1=>朝右跨日 2=>朝上跨日 3=>朝下跨日):"))
# 朝左跨日
if toward == 0:
ava_steps = ['不能左上跨', '不能左下跨']
if (y - 2) >= 0 and ((x - 1) >= 0 or (x + 1) <= 8) and judge_ava(self.camp, left_block) == 'empty':
left_up = judge_ava(self.camp, (x - 1) * 9 + (y - 2))
left_down = judge_ava(self.camp, (x + 1) * 9 + (y - 2))
if (x - 1) >= 0 and (left_up == 'empty' or left_up == 'eatable'):
ava_steps[0] = '0 - 左上跨'
if (x + 1) <= 8 and (left_down == 'empty' or left_down == 'eatable'):
ava_steps[1] = '1 - 左下跨'
ava_dict = {}
for i in range(len(ava_steps)):
ava_dict[str(i)] = ava_steps[i]
while True:
print("请输入要行的步数:", ava_steps, " ***你可以输入 10 来返回重新选择走棋***)")
choice = input()
if (choice not in ava_dict) and (choice != '10'):
print("不能那样走!!!")
elif choice in ava_dict:
if ava_dict[choice] == '0 - 左上跨':
motivation(position, (x - 1) * 9 + (y - 2))
elif ava_dict[choice] == '1 - 左下跨':
motivation(position, (x + 1) * 9 + (y - 2))
elif ava_dict[choice] == '不能左上跨':
print("不能那样走!!!")
continue
elif ava_dict[choice] == '不能左下跨':
print("不能那样走!!!")
continue
break
elif choice == '10':
rounds_count -= 1
break
# 朝右跨日
elif toward == 1:
ava_steps = ['不能右上跨', '不能右下跨']
if (y + 2) <= 8 and ((x - 1) >= 0 or (x + 1) <= 8) and judge_ava(self.camp, right_block) == 'empty':
right_up = judge_ava(self.camp, (x - 1) * 9 + (y + 2))
right_down = judge_ava(self.camp, (x + 1) * 9 + (y + 2))
if (x - 1) >= 0 and (right_up == 'empty' or right_up == 'eatable'):
ava_steps[0] = '0 - 右上跨'
if (x + 1) <= 8 and (right_down == 'empty' or right_down == 'eatable'):
ava_steps[1] = '1 - 右下跨'
ava_dict = {}
for i in range(len(ava_steps)):
ava_dict[str(i)] = ava_steps[i]
while True:
print("请输入要行的步数:", ava_steps, " ***你可以输入 10 来返回重新选择走棋***)")
choice = input()
if (choice not in ava_dict) and (choice != '10'):
print("不能那样走!!!")
elif choice in ava_dict:
if ava_dict[choice] == '0 - 右上跨':
motivation(position, (x - 1) * 9 + (y + 2))
elif ava_dict[choice] == '1 - 右下跨':
motivation(position, (x + 1) * 9 + (y + 2))
elif ava_dict[choice] == '不能右上跨':
print("不能那样走!!!")
continue
elif ava_dict[choice] == '不能右下跨':
print("不能那样走!!!")
continue
break
elif choice == '10':
rounds_count -= 1
break
# 朝上跨日
elif toward == 2:
ava_steps = ['不能上左跨', '不能上右跨']
if (x - 2) >= 0 and ((y - 1) >= 0 or (y + 1) <= 8) and judge_ava(self.camp, up_block) == 'empty':
up_left = judge_ava(self.camp, (x - 2) * 9 + (y - 1))
up_right = judge_ava(self.camp, (x - 2) * 9 + (y + 1))
if (y - 1) >= 0 and (up_left == 'empty' or up_left == 'eatable'):
ava_steps[0] = '0 - 上左跨'
if (y + 1) <= 8 and (up_right == 'empty' or up_right == 'eatable'):
ava_steps[1] = '1 - 上右跨'
ava_dict = {}
for i in range(len(ava_steps)):
ava_dict[str(i)] = ava_steps[i]
while True:
print("请输入要行的步数:", ava_steps, " ***你可以输入 10 来返回重新选择走棋***)")
choice = input()
if (choice not in ava_dict) and (choice != '10'):
print("不能那样走!!!")
elif choice in ava_dict:
if ava_dict[choice] == '0 - 上左跨':
motivation(position, (x - 2) * 9 + (y - 1))
elif ava_dict[choice] == '1 - 上右跨':
motivation(position, (x - 2) * 9 + (y + 1))
elif ava_dict[choice] == '不能上左跨':
print("不能那样走!!!")
continue
elif ava_dict[choice] == '不能上右跨':
print("不能那样走!!!")
continue
break
elif choice == '10':
rounds_count -= 1
break
# 朝下跨日
elif toward == 3:
ava_steps = ['不能下左跨', '不能下右跨']
if (x + 2) <= 9 and ((y - 1) >= 0 or (y + 1) <= 8) and judge_ava(self.camp, down_block) == 'empty':
down_left = judge_ava(self.camp, (x + 2) * 9 + (y - 1))
down_right = judge_ava(self.camp, (x + 2) * 9 + (y + 1))
if (y - 1) >= 0 and (down_left == 'empty' or down_left == 'eatable'):
ava_steps[0] = '0 - 下左跨'
if (y + 1) <= 8 and (down_right == 'empty' or down_right == 'eatable'):
ava_steps[1] = '1 - 下右跨'
ava_dict = {}
for i in range(len(ava_steps)):
ava_dict[str(i)] = ava_steps[i]
while True:
print("请输入要行的步数:", ava_steps, " ***你可以输入 10 来返回重新选择走棋***)")
choice = input()
if (choice not in ava_dict) and (choice != '10'):
print("不能那样走!!!")
elif choice in ava_dict:
if ava_dict[choice] == '0 - 下左跨':
motivation(position, (x + 2) * 9 + (y - 1))
elif ava_dict[choice] == '1 - 下右跨':
motivation(position, (x + 2) * 9 + (y + 1))
elif ava_dict[choice] == '不能下左跨':
print("不能那样走!!!")
continue
elif ava_dict[choice] == '不能下右跨':
print("不能那样走!!!")
continue
break
elif choice == '10':
rounds_count -= 1
break
else:
print("请输入0-3!!!")
continue
break
# 车的类
class Che:
def __init__(self, camp, num):
self.content = '车-' + str(num)
self.camp = camp
def move_way(self, position):
y = position % 9
x = int((position - y) / 9)
global rounds_count
choice = '00'
while True:
if choice == '10':
continue
toward = int(input("请选择移动方向:(0-向左行 1-向右行 2-向上行 3-向下行)"))
# 车向左行的逻辑----向左依次判断 ①遇空白可走,直至到最左边 ②遇第一个敌子可吃,退出循环 ③遇到友军,退出循环
if toward == 0:
# target是车的目的点的水平坐标, 表示在本水平行移动
target = y - 1
# ava_steps存放可走的步数(即车的水平坐标y减去目的点水平坐标target)
ava_steps = []
# 在target左移到左边界及之前
while target >= 0:
# 判定目的点位上是否为空子
if judge_ava(self.camp, (x * 9 + target)) == 'empty':
# 可走, 存入
ava_steps.append(y - target)
# 判定目的点位上是否为敌方棋子
elif judge_ava(self.camp, (x * 9 + target)) == 'eatable':
# 可走, 存入, 退出循环
ava_steps.append(y - target)
break
# 判定目的点位上是否为友方棋子
elif judge_ava(self.camp, x * 9 + target) == 'friendly':
# 不能走, 退出循环
break
target -= 1
ava_dict = {}
for i in range(len(ava_steps)):
ava_dict[str(ava_steps[i])] = ava_steps[i]
while True:
print("请输入要行的步数:", ava_steps, " ***若待选步数为[空],则表明该棋子不能这样走 | 你可以输入 10 来返回重新选择走棋***)")
choice = input()
if (choice not in ava_dict) and (choice != '10'):
print("不能那样走!!!")
elif choice in ava_dict:
motivation(position, x * 9 + (y - ava_dict[choice]))
break
elif choice == '10':
rounds_count -= 1
break
# 车向右行逻辑与向左行相同
elif toward == 1:
target = y + 1
ava_steps = []
while target <= 8:
if judge_ava(self.camp, x * 9 + target) == 'empty':
ava_steps.append(target - y)
elif judge_ava(self.camp, x * 9 + target) == 'eatable':
ava_steps.append(target - y)
break
elif judge_ava(self.camp, x * 9 + target) == 'friendly':
break
target += 1
ava_dict = {}
for i in range(len(ava_steps)):
ava_dict[str(ava_steps[i])] = ava_steps[i]
while True:
print("请输入要行的步数:", ava_steps, " ***若待选步数为[空],则表明该棋子不能这样走 | 你可以输入 10 来返回重新选择走棋***)")
choice = input()
if (choice not in ava_dict) and (choice != '10'):
print("不能那样走!!!")
elif choice in ava_dict:
motivation(position, x * 9 + (y + ava_dict[choice]))
break
elif choice == '10':
rounds_count -= 1
break
# 车向上行的逻辑----向上依次判断 ①遇空白可走,直至最上面 ②遇到敌子可吃,退出循环 ③遇到友军,退出循环
elif toward == 2:
target = x - 1
ava_steps = []
while target >= 0:
if judge_ava(self.camp, target * 9 + y) == 'empty':
ava_steps.append(x - target)
elif judge_ava(self.camp, target * 9 + y) == 'eatable':
ava_steps.append(x - target)
break
elif judge_ava(self.camp, target * 9 + y) == 'friendly':
break
target -= 1
ava_dict = {}
for i in range(len(ava_steps)):
ava_dict[str(ava_steps[i])] = ava_steps[i]
while True:
print("请输入要行的步数:", ava_steps, " ***若待选步数为[空],则表明该棋子不能这样走 | 你可以输入 10 来返回重新选择走棋***)")
choice = input()
if (choice not in ava_dict) and (choice != '10'):
print("不能那样走!!!")
elif choice in ava_dict:
motivation(position, (x - ava_dict[choice]) * 9 + y)
break
elif choice == '10':
rounds_count -= 1
break
# 车向下行逻辑与上行相同
elif toward == 3:
target = x + 1
ava_steps = []
while target <= 9:
if judge_ava(self.camp, target * 9 + y) == 'empty':
ava_steps.append(target - x)
elif judge_ava(self.camp, target * 9 + y) == 'eatable':
ava_steps.append(target - x)
break
elif judge_ava(self.camp, target * 9 + y) == 'friendly':
break
target += 1
ava_dict = {}
for i in range(len(ava_steps)):
ava_dict[str(ava_steps[i])] = ava_steps[i]
while True:
print("请输入要行的步数:", ava_steps, " ***若待选步数为[空],则表明该棋子不能这样走 | 你可以输入 10 来返回重新选择走棋***)")
choice = input()
if (choice not in ava_dict) and (choice != '10'):
print("不能那样走!!!")
elif choice in ava_dict:
motivation(position, (x + ava_dict[choice]) * 9 + y)
break
elif choice == '10':
rounds_count -= 1
break
else:
print("请输入0-3!!!")
continue
break
# 炮的类
class Pao:
def __init__(self, camp, num):
self.content = '炮-' + str(num)
self.camp = camp
def move_way(self, position):
y = position % 9
x = int((position - y) / 9)
global rounds_count
choice = '00'
while True:
if choice == '10':
continue
toward = int(input("请选择移动方向:(0-向左行 1-向右行 2-向上行 3-向下行)"))
# 炮向左行的逻辑----向左依次判断 ①遇空白可走,直至到最左边 ②遇第一个敌子或友军,可再向左走(中间子左边第一个若是敌子,可吃)
if toward == 0:
target = y - 1
ava_steps = []
while target >= 0:
if judge_ava(self.camp, x * 9 + target) == 'empty':
ava_steps.append(y - target)
elif judge_ava(self.camp, x * 9 + target) == 'eatable' or 'friendly':
# 排除中间子在最左边的情况
if target != 0:
# 此处shell指炮击(v.)的目的点的横坐标
shell = target - 1
while True:
# 只要遇到中间子左边的第一个棋子,就判断是敌是友,敌子可吃,友方不操作,退出循环
if judge_ava(self.camp, x * 9 + shell) != 'empty':
if judge_ava(self.camp, x * 9 + shell) == 'eatable':
ava_steps.append(y - shell)
elif judge_ava(self.camp, x * 9 + shell) == 'friendly':
pass
break
shell -= 1
break
target -= 1
ava_dict = {}
for i in range(len(ava_steps)):
ava_dict[str(ava_steps[i])] = ava_steps[i]
while True:
print("请输入要行的步数:", ava_steps, " ***若待选步数为[空],则表明该棋子不能这样走 | 你可以输入 10 来返回重新选择走棋***)")
choice = input()
if (choice not in ava_dict) and (choice != '10'):
print("不能那样走!!!")
elif choice in ava_dict:
motivation(position, x * 9 + (y - ava_dict[choice]))
break
elif choice == '10':
rounds_count -= 1
break
# 炮向右行逻辑与向左行相同
elif toward == 1:
target = y + 1
ava_steps = []
while target <= 8:
if judge_ava(self.camp, x * 9 + target) == 'empty':
ava_steps.append(target - y)
elif judge_ava(self.camp, x * 9 + target) == 'eatable' or 'friendly':
# 排除中间子在最右边的情况
if target != 8:
# 此处shell指炮击(v.)的目的点的横坐标
shell = target + 1
while True:
# 只要遇到中间子右边的第一个棋子,就判断是敌是友,敌子可吃,友方不操作,退出循环
if judge_ava(self.camp, x * 9 + shell) != 'empty':
if judge_ava(self.camp, x * 9 + shell) == 'eatable':
ava_steps.append(shell - y)
elif judge_ava(self.camp, x * 9 + shell) == 'friendly':
pass
break
shell += 1
break
target += 1
ava_dict = {}
for i in range(len(ava_steps)):
ava_dict[str(ava_steps[i])] = ava_steps[i]
while True:
print("请输入要行的步数:", ava_steps, " ***若待选步数为[空],则表明该棋子不能这样走 | 你可以输入 10 来返回重新选择走棋***)")
choice = input()
if (choice not in ava_dict) and (choice != '10'):
print("不能那样走!!!")
elif choice in ava_dict:
motivation(position, x * 9 + (y + ava_dict[choice]))
break
elif choice == '10':
rounds_count -= 1
break
# 炮向上行的逻辑----向上依次判断 ①遇空白可走,直至最上面 ②遇到遇到第一个敌子或友军,可再向上走(中间子上边第一个若是敌子,可吃)
elif toward == 2:
target = x - 1
ava_steps = []
while target >= 0:
if judge_ava(self.camp, target * 9 + y) == 'empty':
ava_steps.append(x - target)
elif judge_ava(self.camp, target * 9 + y) == 'eatable' or 'friendly':
# 排除中间子在最上边的情况
if target != 0:
# 此处shell指炮击(v.)的目的点的纵坐标
shell = target - 1
while True:
# 只要遇到中间子右边的第一个棋子,就判断是敌是友,敌子可吃,友方不操作,退出循环
if judge_ava(self.camp, shell * 9 + y) != 'empty':
if judge_ava(self.camp, shell * 9 + y) == 'eatable':
ava_steps.append(x - shell)
elif judge_ava(self.camp, shell * 9 + y) == 'friendly':
pass
break
shell -= 1
break
target -= 1
ava_dict = {}
for i in range(len(ava_steps)):
ava_dict[str(ava_steps[i])] = ava_steps[i]
while True:
print("请输入要行的步数:", ava_steps, " ***若待选步数为[空],则表明该棋子不能这样走 | 你可以输入 10 来返回重新选择走棋***)")
choice = input()
if (choice not in ava_dict) and (choice != '10'):
print("不能那样走!!!")
elif choice in ava_dict:
motivation(position, (x - ava_dict[choice]) * 9 + y)
break
elif choice == '10':
rounds_count -= 1
break
# 炮向下行逻辑与上行相同
elif toward == 3:
target = x + 1
ava_steps = []
while target <= 9:
if judge_ava(self.camp, target * 9 + y) == 'empty':
ava_steps.append(target - x)
elif judge_ava(self.camp, target * 9 + y) == 'eatable' or 'friendly':
# 排除中间子在最下边的情况
if target != 9:
# 此处shell指炮击(v.)的目的点的纵坐标
shell = target + 1
while True:
# 只要遇到中间子右边的第一个棋子,就判断是敌是友,敌子可吃,友方不操作,退出循环
if judge_ava(self.camp, shell * 9 + y) != 'empty':
if judge_ava(self.camp, shell * 9 + y) == 'eatable':
ava_steps.append(shell - x)
elif judge_ava(self.camp, shell * 9 + y) == 'friendly':
pass
break
shell += 1
break
target += 1
ava_dict = {}
for i in range(len(ava_steps)):
ava_dict[str(ava_steps[i])] = ava_steps[i]
while True:
print("请输入要行的步数:", ava_steps, " ***若待选步数为[空],则表明该棋子不能这样走 | 你可以输入 10 来返回重新选择走棋***)")
choice = input()
if (choice not in ava_dict) and (choice != '10'):
print("不能那样走!!!")
elif choice in ava_dict:
motivation(position, (ava_dict[choice] + x) * 9 + y)
break
elif choice == '10':
rounds_count -= 1
break
else:
print("请输入0-3!!!")
continue
break
# 兵的类
class Bing:
def __init__(self, num):
self.content = '兵-' + str(num)
self.camp = '红'
def move_way(self, position):
y = position % 9
x = int((position - y) / 9)
global rounds_count
choice = '00'
while True:
if choice == '10':
continue
# 兵只有在向上渡过楚河汉界后(x <= 4),才可左右移动
if x <= 4:
toward = int(input("请选择移动方向:(0-向上行 1-向左行 2-向右行)"))
# 兵向上行逻辑 一次只走一步 ---- ①空可走 ②遇到敌子可吃 ③遇到友军不可走
if toward == 0:
target = x - 1
ava_steps = []
if target >= 0:
if judge_ava(self.camp, target * 9 + y) == 'empty':
ava_steps.append(x - target)
elif judge_ava(self.camp, target * 9 + y) == 'eatable':
ava_steps.append(x - target)
elif judge_ava(self.camp, target * 9 + y) == 'friendly':
pass
ava_dict = {}
for i in range(len(ava_steps)):
ava_dict[str(ava_steps[i])] = ava_steps[i]
while True:
print("请输入要行的步数:", ava_steps, " ***若待选步数为[空],则表明该棋子不能这样走 | 你可以输入 10 来返回重新选择走棋***)")
choice = input()
if (choice not in ava_dict) and (choice != '10'):
print("不能那样走!!!")
elif choice in ava_dict:
motivation(position, (x - ava_dict[choice]) * 9 + y)
break
elif choice == '10':
rounds_count -= 1
break
break
# 兵向左行逻辑 一次只走一步 ---- ①空可走 ②遇到敌子可吃 ③遇到友军不可走
elif toward == 1:
target = y - 1
ava_steps = []
if target >= 0:
if judge_ava(self.camp, x * 9 + target) == 'empty':
ava_steps.append(y - target)
elif judge_ava(self.camp, x * 9 + target) == 'eatable':
ava_steps.append(y - target)
elif judge_ava(self.camp, x * 9 + target) == 'friendly':
pass
ava_dict = {}
for i in range(len(ava_steps)):
ava_dict[str(ava_steps[i])] = ava_steps[i]
while True:
print("请输入要行的步数:", ava_steps, " ***若待选步数为[空],则表明该棋子不能这样走 | 你可以输入 10 来返回重新选择走棋***)")
choice = input()
if (choice not in ava_dict) and (choice != '10'):
print("不能那样走!!!")
elif choice in ava_dict:
motivation(position, x * 9 + (y - ava_dict[choice]))
break
elif choice == '10':
rounds_count -= 1
break
break
# 兵向右行逻辑同向左行
elif toward == 2:
target = y + 1
ava_steps = []
if target <= 8:
if judge_ava(self.camp, int(x * 9 + target)) == 'empty':
ava_steps.append(target - y)
elif judge_ava(self.camp, x * 9 + target) == 'eatable':
ava_steps.append(target - y)
elif judge_ava(self.camp, x * 9 + target) == 'friendly':
pass
ava_dict = {}
for i in range(len(ava_steps)):
ava_dict[str(ava_steps[i])] = ava_steps[i]
while True:
print("请输入要行的步数:", ava_steps, " ***若待选步数为[空],则表明该棋子不能这样走 | 你可以输入 10 来返回重新选择走棋***)")
choice = input()
if (choice not in ava_dict) and (choice != '10'):
print("不能那样走!!!")
elif choice in ava_dict:
motivation(position, x * 9 + (y + ava_dict[choice]))
break
elif choice == '10':
rounds_count -= 1
break
break
else:
print("请输入0-2!!!")
continue
elif x >= 5:
print("<兵未渡河,只能向上行>")
# 兵向上行逻辑 一次只走一步 ---- ①空可走 ②遇到敌子可吃 ③遇到友军不可走
target = x - 1
ava_steps = []
if judge_ava(self.camp, target * 9 + y) == 'empty':
ava_steps.append(x - target)
elif judge_ava(self.camp, target * 9 + y) == 'eatable':
ava_steps.append(x - target)
elif judge_ava(self.camp, target * 9 + y) == 'friendly':
pass
ava_dict = {}
for i in range(len(ava_steps)):
ava_dict[str(ava_steps[i])] = ava_steps[i]
while True:
print("请输入要行的步数:", ava_steps, " ***若待选步数为[空],则表明该棋子不能这样走 | 你可以输入 10 来返回重新选择走棋***)")
choice = input()
if (choice not in ava_dict) and (choice != '10'):
print("不能那样走!!!")
elif choice in ava_dict:
motivation(position, (x - ava_dict[choice]) * 9 + y)
break
elif choice == '10':
rounds_count -= 1
break
break
# 卒的类
class Zu:
def __init__(self, num):
self.content = '卒-' + str(num)
self.camp = '黑'
def move_way(self, position):
y = position % 9
x = int((position - y) / 9)
global rounds_count
choice = '00'
while True:
if choice == '10':
continue
# 卒只有在向上渡过楚河汉界后(x >= 5),才可左右移动
if x >= 5:
toward = int(input("请选择移动方向:(0-向下行 1-向左行 2-向右行)"))
# 卒向下行逻辑 一次只走一步 ---- ①空可走 ②遇到敌子可吃 ③遇到友军不可走
if toward == 0:
target = x + 1
ava_steps = []
if target <= 9:
if judge_ava(self.camp, target * 9 + y) == 'empty':
ava_steps.append(target - x)
elif judge_ava(self.camp, target * 9 + y) == 'eatable':
ava_steps.append(target - x)
elif judge_ava(self.camp, target * 9 + y) == 'friendly':
pass
ava_dict = {}
for i in range(len(ava_steps)):
ava_dict[str(ava_steps[i])] = ava_steps[i]
while True:
print("请输入要行的步数:", ava_steps, " ***若待选步数为[空],则表明该棋子不能这样走 | 你可以输入 10 来返回重新选择走棋***)")
choice = input()
if (choice not in ava_dict) and (choice != '10'):
print("不能那样走!!!")
elif choice in ava_dict:
motivation(position, (x + ava_dict[choice]) * 9 + y)
break
elif choice == '10':
rounds_count -= 1
break
break
# 卒向左行逻辑 一次只走一步 ---- ①空可走 ②遇到敌子可吃 ③遇到友军不可走
elif toward == 1:
target = y - 1
ava_steps = []
if target <= 0:
if judge_ava(self.camp, x * 9 + target) == 'empty':
ava_steps.append(y - target)
elif judge_ava(self.camp, x * 9 + target) == 'eatable':
ava_steps.append(y - target)
elif judge_ava(self.camp, x * 9 + target) == 'friendly':
pass
ava_dict = {}
for i in range(len(ava_steps)):
ava_dict[str(ava_steps[i])] = ava_steps[i]
while True:
print("请输入要行的步数:", ava_steps, " ***若待选步数为[空],则表明该棋子不能这样走 | 你可以输入 10 来返回重新选择走棋***)")
choice = input()
if (choice not in ava_dict) and (choice != '10'):
print("不能那样走!!!")
elif choice in ava_dict:
motivation(position, x * 9 + (y - ava_dict[choice]))
break
elif choice == '10':
rounds_count -= 1
break
break
# 卒向右行逻辑同向左行
elif toward == 2:
target = y + 1
ava_steps = []
if target <= 8:
if judge_ava(self.camp, int(x * 9 + target)) == 'empty':
ava_steps.append(target - y)
elif judge_ava(self.camp, x * 9 + target) == 'eatable':
ava_steps.append(target - y)
elif judge_ava(self.camp, x * 9 + target) == 'friendly':
pass
ava_dict = {}
for i in range(len(ava_steps)):
ava_dict[str(ava_steps[i])] = ava_steps[i]
while True:
print("请输入要行的步数:", ava_steps, " ***若待选步数为[空],则表明该棋子不能这样走 | 你可以输入 10 来返回重新选择走棋***)")
choice = input()
if (choice not in ava_dict) and (choice != '10'):
print("不能那样走!!!")
elif choice in ava_dict:
motivation(position, x * 9 + (y + ava_dict[choice]))
break
elif choice == '10':
rounds_count -= 1
break
break
else:
print("请输入0-2!!!")
continue
elif x <= 4:
print("<卒未渡河,只能向下行>")
# 卒向下行逻辑 一次只走一步 ---- ①空可走 ②遇到敌子可吃 ③遇到友军不可走
target = x + 1
ava_steps = []
if judge_ava(self.camp, target * 9 + y) == 'empty':
ava_steps.append(target - x)
elif judge_ava(self.camp, target * 9 + y) == 'eatable':
ava_steps.append(target - x)
elif judge_ava(self.camp, target * 9 + y) == 'friendly':
pass
ava_dict = {}
for i in range(len(ava_steps)):
ava_dict[str(ava_steps[i])] = ava_steps[i]
while True:
print("请输入要行的步数:", ava_steps, " ***若待选步数为[空],则表明该棋子不能这样走 | 你可以输入 10 来返回重新选择棋子***")
choice = input()
if (choice not in ava_dict) and (choice != '10'):
print("不能那样走!!!")
elif choice in ava_dict:
motivation(position, (x + ava_dict[choice]) * 9 + y)
break
elif choice == '10':
rounds_count -= 1
break
break
# 空子的类
class EmptyPiece:
def __init__(self):
self.content = ' ○ '
self.camp = '无'
# 判断当前被操作的棋子能否与目的点上的棋子发生交互
def judge_ava(camp, posti):
position = int(posti)
if board_info[position].content == ' ○ ':
# 目的点上是空白,可走
return 'empty'
elif board_info[position].camp != camp:
# 目的点上是敌军,可吃
return 'eatable'
elif board_info[position].camp == camp:
# 目的点上是友军,不能与之交互
return 'friendly'
# 移动棋子
def motivation(source, destination):
global obj_empty_piece
global board_info
global winner_count
# 判断被吃掉的是否是 帅(将)
if board_info[destination].content == ' 将 ':
# 将被吃, 红军胜
winner_count = 1
elif board_info[destination].content == ' 帅 ':
# 帅被吃, 黑军胜
winner_count = 2
# 替换棋子坐标
board_info[destination] = board_info[source]
board_info[source] = obj_empty_piece
# 棋盘初始化(赛前摆好棋子)
def init():
# 初始化黑棋(摆好黑棋)
board_info[0] = Che('黑', 1)
board_info[1] = Ma('黑', 1)
board_info[2] = Xiang('黑', 1)
board_info[3] = Shi('黑', 1)
board_info[4] = Jiang()
board_info[5] = Shi('黑', 2)
board_info[6] = Xiang('黑', 2)
board_info[7] = Ma('黑', 2)
board_info[8] = Che('黑', 2)
board_info[19] = Pao('黑', 1)
board_info[25] = Pao('黑', 2)
board_info[27] = Zu(1)
board_info[29] = Zu(2)
board_info[31] = Zu(3)
board_info[33] = Zu(4)
board_info[35] = Zu(5)
# 初始化红棋(摆好红棋)
board_info[81] = Che('红', 1)
board_info[82] = Ma('红', 1)
board_info[83] = Xiang('红', 1)
board_info[84] = Shi('红', 1)
board_info[85] = Shuai()
board_info[86] = Shi('红', 2)
board_info[87] = Xiang('红', 2)
board_info[88] = Ma('红', 2)
board_info[89] = Che('红', 2)
board_info[64] = Pao('红', 1)
board_info[70] = Pao('红', 2)
board_info[54] = Bing(1)
board_info[56] = Bing(2)
board_info[58] = Bing(3)
board_info[60] = Bing(4)
board_info[62] = Bing(5)
# 实时打印棋盘 和 操作提示界面
def playing(red_survial, black_survial):
global rounds_count
global game_over
while True:
# 判断游戏是否还要继续
if game_over == 1:
break
# 打印棋盘
for i in range(10):
for j in range(9):
if board_info[i * 9 + j].camp == '红':
print("\033[1;30;41m%s\033[0m" % board_info[i * 9 + j].content, end='')
red_survial.append([board_info[i * 9 + j], i * 9 + j])
elif board_info[i * 9 + j].camp == '黑':
print("\033[1;34;40m%s\033[0m" % board_info[i * 9 + j].content, end='')
black_survial.append([board_info[i * 9 + j], i * 9 + j])
elif board_info[i * 9 + j].camp == '无':
print("\033[1;30;47m%s\033[0m" % board_info[i * 9 + j].content, end='')
if j != 8:
print("\033[0;30;47m —— \033[0m", end='')
print("")
if i != 9:
if i != 0 and i != 1 and i != 4 and i != 7 and i != 8:
print("\033[0;30;47m | | | | | | | | | \033[0m")
elif i == 4:
print("\033[0;30;47m | 楚 河 汉 界 | \033[0m")
elif i == 0 or i == 7:
print("\033[0;30;47m | | | \ | / | | | \033[0m")
elif i == 1 or i == 8:
print("\033[0;30;47m | | | / | \ | | | \033[0m")
# 打印操作提示界面
if winner_count == 1:
print("")
print("|双方共走", rounds_count - 1, "步|", "============<将 被 消 灭, 红 方 胜 利>============")
game_over = 1
continue
elif winner_count == 2:
print("")
print("|双方共走", rounds_count - 1, "步|", "============<帅 被 消 灭, 黑 方 胜 利>============")
game_over = 1
continue
if rounds_count % 2 != 0:
print("|第", rounds_count, "步|", " =============红 方 回 合==============")
print("场上剩余红棋:")
for i in range(len(red_survial)):
print(i, '<=', "\033[1;30;41m%s\033[0m" % red_survial[i][0].content, end=' | ')
if i == 7:
print("")
print("")
print("请选择想要移动的棋子:", end='')
chosen_num = int(input())
if chosen_num >= len(red_survial) and (chosen_num != 666):
print("!!!请选择0 ——", len(red_survial) - 1, "以内的序号")
red_survial = []
continue
if chosen_num == 666:
print("")
print("|双方共走", rounds_count - 1, "步|", "============<红 方 投 降, 比 赛 结 束>============")
game_over = 1
else:
print("你选择走:", "\033[1;30;41m%s\033[0m" % red_survial[chosen_num][0].content)
board_info[red_survial[chosen_num][1]].move_way(red_survial[chosen_num][1])
elif rounds_count % 2 == 0:
print("|第", rounds_count, "步|", " =============黑 方 回 合==============")
print("场上剩余黑棋:")
for i in range(len(black_survial)):
print(i, '<=', "\033[1;34;40m%s\033[0m" % black_survial[i][0].content, end=' | ')
if i == 7:
print("")
print("")
print("请选择想要移动的棋子:", end='')
chosen_num = int(input())
if chosen_num >= len(black_survial) and (chosen_num != 666):
print("!!!请选择0 ——", len(black_survial) - 1, "以内的序号")
black_survial = []
continue
if chosen_num == 666:
print("")
print("|双方共走", rounds_count - 1, "步|", "============<黑 方 投 降, 比 赛 结 束>============")
game_over = 1
else:
print("你选择走:", "\033[1;34;40m%s\033[0m" % black_survial[chosen_num][0].content)
board_info[black_survial[chosen_num][1]].move_way(black_survial[chosen_num][1])
# 进行一轮走棋后,清空red_survial, black_survial, 并让rounds_count增加1
red_survial = []
black_survial = []
rounds_count += 1
# 启动
def main():
# 将棋盘清空( 填满空子 )
for i in range(90):
board_info.append(obj_empty_piece)
# 用于实时存放红黑阵营存活的棋子
red_survial = []
black_survial = []
# 开始
init()
playing(red_survial, black_survial)
return 0
# 棋盘信息
board_info = []
# 游戏步数计数器
rounds_count = 1
# 游戏结束控制器
game_over = 0
# 赢家记录器
winner_count = 0
# 实例化空子
obj_empty_piece = EmptyPiece()
# main()入口
main()