目录

前言

一、游戏说明及效果展示

1. 游戏说明

2. 效果展示

二、实现逻辑

1. 实时打印棋盘

2. 棋子移动

三、总结与完整源代码


前言

本文介绍一个基于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 % 9x = int((position - y) / 9)global rounds_countchoice = '00'while True:if choice == '10':continuetoward = 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':# 不能走, 退出循环breaktarget -= 1ava_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]))breakelif choice == '10':rounds_count -= 1break# 车向右行if toward == 1:# 此处省略, 具体在源码里# 车向上行if toward == 2:# 此处省略, 具体在源码里# 车向下行if toward == 3:# 此处省略, 具体在源码里# 当棋手输入错误字符, 提示错误并跳出本次循环, 让棋手重新输入字符else:print("请输入0-3!!!")continuebreak

②. 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_pieceglobal board_infoglobal winner_count# 判断被吃掉的是否是 帅(将)if board_info[destination].content == ' 将 ':# 将被吃, 红军胜winner_count = 1elif board_info[destination].content == ' 帅 ':# 帅被吃, 黑军胜winner_count = 2# 替换棋子坐标board_info[destination] = board_info[source]board_info[source] = obj_empty_piece

④. init()用于棋盘初始化, 按中国象棋规则来摆上对应的棋子( 将board_info中各点替换为对应初始对象 )。


三、总结与完整源代码

程序实现了简单的命令行《中国象棋》, 但暂时没有“将军”和“悔棋”等功能,读者可以自行按需添加实现。

谢谢你看到最后!!!

源码如下:chessGame.py

# 帅的类
class Shuai:def __init__(self):self.content = ' 帅 'self.camp = '红'def move_way(self, position):y = position % 9x = int((position - y) / 9)global rounds_countchoice = '00'while True:if choice == '10':continuetoward = int(input("请选择移动方向:(0-向左行 1-向右行 2-向上行 3-向下行)"))# 帅向左行的逻辑----最多走一步,若左边是敌子,可吃,若是友军,则不能走,且不能向左走出营地(y' >= 3)if toward == 0:target = y - 1ava_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':passtarget -= 1ava_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]))breakelif choice == '10':rounds_count -= 1break# 帅向右行逻辑与向左行相同(y' <= 5)elif toward == 1:target = y + 1ava_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':passtarget += 1ava_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]))breakelif choice == '10':rounds_count -= 1break# 帅向上行的逻辑----最多走一步,若上边边是敌子,可吃,若是友军,则不能走,且不能向上走出营地(x' >= 7)elif toward == 2:target = x - 1ava_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':passtarget -= 1ava_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)breakelif choice == '10':rounds_count -= 1break# 帅向下行逻辑与上行相同(x' <= 9)elif toward == 3:target = x + 1ava_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':passtarget += 1ava_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)breakelif choice == '10':rounds_count -= 1breakelse:print("请输入0-3!!!")continuebreak# 将的类
class Jiang:def __init__(self):self.content = ' 将 'self.camp = '黑'def move_way(self, position):y = position % 9x = int((position - y) / 9)global rounds_countchoice = '00'while True:if choice == '10':continuetoward = int(input("请选择移动方向:(0-向左行 1-向右行 2-向上行 3-向下行)"))# 将向左行的逻辑----最多走一步,若左边是敌子,可吃,若是友军,则不能走,且不能向左走出营地(y' >= 3)if toward == 0:target = y - 1ava_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':passtarget -= 1ava_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]))breakelif choice == '10':rounds_count -= 1break# 将向右行逻辑与向左行相同(y' <= 5)elif toward == 1:target = y + 1ava_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':passtarget += 1ava_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]))breakelif choice == '10':rounds_count -= 1break# 将向上行的逻辑----最多走一步,若上边边是敌子,可吃,若是友军,则不能走,且不能向上走出营地(x' >= 0)elif toward == 2:target = x - 1ava_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':passtarget -= 1ava_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)breakelif choice == '10':rounds_count -= 1break# 将向下行逻辑与上行相同(x' <= 2)elif toward == 3:target = x + 1ava_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':passtarget += 1ava_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)breakelif choice == '10':rounds_count -= 1breakelse:print("请输入0-3!!!")continuebreak# 士的类
class Shi:def __init__(self, camp, num):self.content = '士-' + str(num)self.camp = campdef move_way(self, position):y = position % 9x = int((position - y) / 9)global rounds_countchoice = '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 - 1target_x = x - 1ava_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':passtarget -= 1target_x -= 1ava_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))breakelif choice == '10':rounds_count -= 1break# 士向左下行逻辑与向左上行相同(y' >= 3, x' <= 2)elif toward == 1:target = y - 1target_x = x + 1ava_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':passtarget -= 1target_x += 1ava_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))breakelif choice == '10':rounds_count -= 1break# 士向右上行逻辑与向左上行相同(y' <= 5, x' >= 0)elif toward == 2:target = y + 1target_x = x - 1ava_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':passtarget += 1target_x -= 1ava_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))breakelif choice == '10':rounds_count -= 1break# 士向右下行逻辑与向左上行相同(y' <= 5, x' <= 2)elif toward == 3:target = y + 1target_x = x + 1ava_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':passtarget += 1target_x += 1ava_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))breakelif choice == '10':rounds_count -= 1breakelse:print("请输入0-3!!!")continuebreak# 判断是否是红方的士elif x >= 7:toward = int(input("请选择移动方向:(0-向左上行 1-向左下行 2-向右上行 3-向右下行)"))# 士向左上行的逻辑----最多走一步(走口字),若左上是敌子,可吃,若是友军,则不能走,且不能向左走出营地(y' >= 3, x' >= 7)if toward == 0:target = y - 1target_x = x - 1ava_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':passtarget -= 1target_x -= 1ava_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))breakelif choice == '10':rounds_count -= 1break# 士向左下行逻辑与向左上行相同(y' >= 3, x' <= 9)elif toward == 1:target = y - 1target_x = x + 1ava_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':passtarget -= 1target_x += 1ava_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))breakelif choice == '10':rounds_count -= 1break# 士向右上行逻辑与向左上行相同(y' <= 5, x' >= 7)elif toward == 2:target = y + 1target_x = x - 1ava_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':passtarget += 1target_x -= 1ava_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))breakelif choice == '10':rounds_count -= 1break# 士向右下行逻辑与向左上行相同(y' <= 5, x' <= 9)elif toward == 3:target = y + 1target_x = x + 1ava_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':passtarget += 1target_x += 1ava_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))breakelif choice == '10':rounds_count -= 1breakelse:print("请输入0-3!!!")continuebreak# 象的类
class Xiang:def __init__(self, camp, num):self.content = '象-' + str(num)self.camp = campdef move_way(self, position):y = position % 9x = int((position - y) / 9)global rounds_countchoice = '00'while True:if choice == '10':continue# 判断是否是是黑方的象if x <= 4:toward = int(input("请选择移动方向:(0-向左上行 1-向左下行 2-向右上行 3-向右下行)"))# 黑象向左上行的逻辑----最多走一步(走田字),若左上是敌子,可吃,若是友军,则不能走# ("塞象眼":若田字中心有子,则无法走象)if toward == 0:target = y - 2target_x = x - 2center = (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':passtarget -= 2target_x -= 2ava_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))breakelif choice == '10':rounds_count -= 1break# 黑象向左下行逻辑与向左上行相同,且不能向下渡过河界(x' <= 4)elif toward == 1:target = y - 2target_x = x + 2center = (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':passtarget -= 2target_x += 2ava_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))breakelif choice == '10':rounds_count -= 1break# 黑象向右上行逻辑与向左上行相同elif toward == 2:target = y + 2target_x = x - 2center = (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':passtarget += 2target_x -= 2ava_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))breakelif choice == '10':rounds_count -= 1break# 黑象向右下行逻辑与向左上行相同,且不能向下渡过河界(x' <= 4)elif toward == 3:target = y + 2target_x = x + 2center = (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':passtarget += 2target_x += 2ava_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))breakelif choice == '10':rounds_count -= 1breakelse:print("请输入0-3!!!")continuebreak# 判断是否是红方的象elif x >= 5:toward = int(input("请选择移动方向:(0-向左上行 1-向左下行 2-向右上行 3-向右下行)"))# 红象向左上行的逻辑----最多走一步(走田字),若左上是敌子,可吃,若是友军,则不能走,且不能向上渡过河界(x' >= 5)# ("塞象眼":若田字中心有子,则无法走象)if toward == 0:target = y - 2target_x = x - 2center = (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':passtarget -= 2target_x -= 2ava_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))breakelif choice == '10':rounds_count -= 1break# 红象向左下行逻辑与向左上行相同elif toward == 1:target = y - 2target_x = x + 2center = (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':passtarget -= 2target_x += 2ava_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))breakelif choice == '10':rounds_count -= 1break# 红象向右上行逻辑与向左上行相同,且不能向上渡过河界(x' >= 5)elif toward == 2:target = y + 2target_x = x - 2center = (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':passtarget += 2target_x -= 2ava_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))breakelif choice == '10':rounds_count -= 1break# 红象向右下行逻辑与向左上行相同elif toward == 3:target = y + 2target_x = x + 2center = (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':passtarget += 2target_x += 2ava_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))breakelif choice == '10':rounds_count -= 1breakelse:print("请输入0-3!!!")continuebreak# 马的类
class Ma:def __init__(self, camp, num):self.content = '马-' + str(num)self.camp = campdef move_way(self, position):y = position % 9x = int((position - y) / 9)global rounds_countchoice = '00'# 定义“挡马块”left_block = x * 9 + (y - 1)right_block = x * 9 + (y + 1)up_block = (x - 1) * 9 + ydown_block = (x + 1) * 9 + ywhile True:if choice == '10':continuetoward = 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("不能那样走!!!")continueelif ava_dict[choice] == '不能左下跨':print("不能那样走!!!")continuebreakelif choice == '10':rounds_count -= 1break# 朝右跨日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("不能那样走!!!")continueelif ava_dict[choice] == '不能右下跨':print("不能那样走!!!")continuebreakelif choice == '10':rounds_count -= 1break# 朝上跨日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("不能那样走!!!")continueelif ava_dict[choice] == '不能上右跨':print("不能那样走!!!")continuebreakelif choice == '10':rounds_count -= 1break# 朝下跨日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("不能那样走!!!")continueelif ava_dict[choice] == '不能下右跨':print("不能那样走!!!")continuebreakelif choice == '10':rounds_count -= 1breakelse:print("请输入0-3!!!")continuebreak# 车的类
class Che:def __init__(self, camp, num):self.content = '车-' + str(num)self.camp = campdef move_way(self, position):y = position % 9x = int((position - y) / 9)global rounds_countchoice = '00'while True:if choice == '10':continuetoward = 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':# 不能走, 退出循环breaktarget -= 1ava_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]))breakelif choice == '10':rounds_count -= 1break# 车向右行逻辑与向左行相同elif toward == 1:target = y + 1ava_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)breakelif judge_ava(self.camp, x * 9 + target) == 'friendly':breaktarget += 1ava_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]))breakelif choice == '10':rounds_count -= 1break# 车向上行的逻辑----向上依次判断 ①遇空白可走,直至最上面 ②遇到敌子可吃,退出循环 ③遇到友军,退出循环elif toward == 2:target = x - 1ava_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)breakelif judge_ava(self.camp, target * 9 + y) == 'friendly':breaktarget -= 1ava_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)breakelif choice == '10':rounds_count -= 1break# 车向下行逻辑与上行相同elif toward == 3:target = x + 1ava_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)breakelif judge_ava(self.camp, target * 9 + y) == 'friendly':breaktarget += 1ava_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)breakelif choice == '10':rounds_count -= 1breakelse:print("请输入0-3!!!")continuebreak# 炮的类
class Pao:def __init__(self, camp, num):self.content = '炮-' + str(num)self.camp = campdef move_way(self, position):y = position % 9x = int((position - y) / 9)global rounds_countchoice = '00'while True:if choice == '10':continuetoward = int(input("请选择移动方向:(0-向左行 1-向右行 2-向上行 3-向下行)"))# 炮向左行的逻辑----向左依次判断 ①遇空白可走,直至到最左边 ②遇第一个敌子或友军,可再向左走(中间子左边第一个若是敌子,可吃)if toward == 0:target = y - 1ava_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 - 1while 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':passbreakshell -= 1breaktarget -= 1ava_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]))breakelif choice == '10':rounds_count -= 1break# 炮向右行逻辑与向左行相同elif toward == 1:target = y + 1ava_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 + 1while 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':passbreakshell += 1breaktarget += 1ava_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]))breakelif choice == '10':rounds_count -= 1break# 炮向上行的逻辑----向上依次判断 ①遇空白可走,直至最上面 ②遇到遇到第一个敌子或友军,可再向上走(中间子上边第一个若是敌子,可吃)elif toward == 2:target = x - 1ava_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 - 1while 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':passbreakshell -= 1breaktarget -= 1ava_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)breakelif choice == '10':rounds_count -= 1break# 炮向下行逻辑与上行相同elif toward == 3:target = x + 1ava_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 + 1while 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':passbreakshell += 1breaktarget += 1ava_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)breakelif choice == '10':rounds_count -= 1breakelse:print("请输入0-3!!!")continuebreak# 兵的类
class Bing:def __init__(self, num):self.content = '兵-' + str(num)self.camp = '红'def move_way(self, position):y = position % 9x = int((position - y) / 9)global rounds_countchoice = '00'while True:if choice == '10':continue# 兵只有在向上渡过楚河汉界后(x <= 4),才可左右移动if x <= 4:toward = int(input("请选择移动方向:(0-向上行 1-向左行 2-向右行)"))# 兵向上行逻辑 一次只走一步 ---- ①空可走 ②遇到敌子可吃 ③遇到友军不可走if toward == 0:target = x - 1ava_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':passava_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)breakelif choice == '10':rounds_count -= 1breakbreak# 兵向左行逻辑 一次只走一步 ---- ①空可走 ②遇到敌子可吃 ③遇到友军不可走elif toward == 1:target = y - 1ava_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':passava_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]))breakelif choice == '10':rounds_count -= 1breakbreak# 兵向右行逻辑同向左行elif toward == 2:target = y + 1ava_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':passava_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]))breakelif choice == '10':rounds_count -= 1breakbreakelse:print("请输入0-2!!!")continueelif x >= 5:print("<兵未渡河,只能向上行>")# 兵向上行逻辑 一次只走一步 ---- ①空可走 ②遇到敌子可吃 ③遇到友军不可走target = x - 1ava_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':passava_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)breakelif choice == '10':rounds_count -= 1breakbreak# 卒的类
class Zu:def __init__(self, num):self.content = '卒-' + str(num)self.camp = '黑'def move_way(self, position):y = position % 9x = int((position - y) / 9)global rounds_countchoice = '00'while True:if choice == '10':continue# 卒只有在向上渡过楚河汉界后(x >= 5),才可左右移动if x >= 5:toward = int(input("请选择移动方向:(0-向下行 1-向左行 2-向右行)"))# 卒向下行逻辑 一次只走一步 ---- ①空可走 ②遇到敌子可吃 ③遇到友军不可走if toward == 0:target = x + 1ava_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':passava_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)breakelif choice == '10':rounds_count -= 1breakbreak# 卒向左行逻辑 一次只走一步 ---- ①空可走 ②遇到敌子可吃 ③遇到友军不可走elif toward == 1:target = y - 1ava_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':passava_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]))breakelif choice == '10':rounds_count -= 1breakbreak# 卒向右行逻辑同向左行elif toward == 2:target = y + 1ava_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':passava_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]))breakelif choice == '10':rounds_count -= 1breakbreakelse:print("请输入0-2!!!")continueelif x <= 4:print("<卒未渡河,只能向下行>")# 卒向下行逻辑 一次只走一步 ---- ①空可走 ②遇到敌子可吃 ③遇到友军不可走target = x + 1ava_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':passava_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)breakelif choice == '10':rounds_count -= 1breakbreak# 空子的类
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_pieceglobal board_infoglobal winner_count# 判断被吃掉的是否是 帅(将)if board_info[destination].content == ' 将 ':# 将被吃, 红军胜winner_count = 1elif 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_countglobal game_overwhile 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 = 1continueelif winner_count == 2:print("")print("|双方共走", rounds_count - 1, "步|", "============<帅 被 消 灭, 黑 方 胜 利>============")game_over = 1continueif 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 = []continueif chosen_num == 666:print("")print("|双方共走", rounds_count - 1, "步|", "============<红 方 投 降, 比 赛 结 束>============")game_over = 1else: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 = []continueif chosen_num == 666:print("")print("|双方共走", rounds_count - 1, "步|", "============<黑 方 投 降, 比 赛 结 束>============")game_over = 1else: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增加1red_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()

Python实现简单命令行版《中国象棋》不使用第三方库相关推荐

  1. Python 写一个命令行版的火车票查看器

    用python另一个抢票神器,你get到了吗? 2017年时间飞逝,转眼间距离2018年春节还有不到1个月的时间,还在为抢不到火车票发愁吗?作为程序员的我们撸一个抢票软件可好? 难以想象的数据, 预示 ...

  2. python刷网易云_牛逼了!用Python开发的命令行版网易云音乐,Github获8300颗星!...

    大家好,我是程序员G哥 最近在逛Github发现了一个非常有趣的库musicbox,是用纯Python打造的,收获了8300颗星.Python语言简单易学,好玩有趣,身边越来越多的小伙伴都开始学习Py ...

  3. Java-命令行版中国象棋

    Java-无界面版中国象棋 说明 版权声明 环境 一.效果 二.测试Demo 三.静态内容 四.主要规则 五.棋子移动规则 后记 说明 博客日期:2022年2月5日13点14分星期六 无界面版中国象棋 ...

  4. Python编写的命令行版微信。(已集成自动聊天机器人(通过网址api形式))

    参考资料:该项目的GitHub开源地址 网页版微信API,包含终端版微信及微信机器人 基础版的API. Python登录器整合. 可更换的聊天机器人api. #!/usr/bin/env python ...

  5. 康威生命游戏java_Java Python 康威生命游戏 - 命令行版

    Java & Python 康威生命游戏 - 命令行版(2020年7月23日) 制作背景 高二的时候看霍金的<大设计>最后几页的时候看到里面提到了康威生命游戏,介绍了它的规则,感觉 ...

  6. 用Python写一个命令行火车票查看器

    Linux编程点击右侧关注,免费入门到精通! 作者丨protream https://www.jianshu.com/p/f411d7e10c41 当你想查询一下火车票信息的时候,你还在上12306官 ...

  7. Python 实现有道翻译命令行版

    一.个人需求 由于一直用Linux系统,对于词典的支持特别不好,对于我这英语渣渣的人来说,当看英文文档就一直卡壳,之前用惯了有道词典,感觉很不错,虽然有网页版的但是对于全站英文的网页来说并不支持.索性 ...

  8. csvtk:高效命令行版极简dplyr

    写在前面 什么时候写 csvtk 呀,csvtk 也借鉴了些 datamash 的东西. 之前写 datamash 的使用教程 linux 极简统计分析工具 datamash 必看教程,收到了一位读者 ...

  9. jdk解压版_命令行版的斗地主你玩过没?

    相信大家都玩过斗地主游戏,或在现实中斗地主或在电脑和手机上斗地主,但你想过用命令行界面进行斗地主吗? 先来张图体验一下: 是不是觉得挺有意思,下面就带大家一起玩一下吧~部署命令行版斗地主 1 环境准备 ...

  10. Python教程:命令行参数处理

    sys模块 sys模块代表了Python解释器,主要用于获取和Python解释器相关的信息,其中 sys.argv 可以获取命令行参数 在Python交互式解释器中可以先导入sys模块 import ...

最新文章

  1. iOS应用模块化的思考及落地方案(二)模块化自动构建工具的使用
  2. Android 网络编程系列(5)Volley 网络框架入门
  3. 7——条件判断、三目运算、条件循环、迭代循环
  4. 限时分享:产品经理面试高频考题及答案
  5. 2019年秋计算机管理工作总结,年段工作总结(2019秋高一上)
  6. 用uGUI开发自定义Toggle Slider控件
  7. RocketMQ源码解析-PullConsumer取消息(2)
  8. Java设计模式—工厂方法模式抽象工厂模式
  9. 窃取任意GitHub Actions敏感信息如此简单,只需要分支改个名?
  10. 分享电脑中截图的五种方法(包括截长图)
  11. MAC和WINDOWS链接小米路由器移动硬盘方法
  12. 图的单源最短路径算法
  13. 问题描述:宏代码导致无法打开文件
  14. 纯CSS3实现的纸张褶起动效
  15. python小项目案例-python简单项目实例
  16. Apache Traffic Server 简介
  17. MFC输出普朗克常数
  18. paypal国际支付的对接,使用tp5开发paypal
  19. 【论文精读】As-Projective-As-Possible Image Stitching with Moving DLT
  20. [Shell] awk FS,OFS,RS,ORS

热门文章

  1. 微信小程序地图回到原点
  2. 教你,用java生成验证码(这这太简单了了吧!!!!!!!)
  3. QTableview
  4. 图解机器学习算法(10) | XGBoost模型最全解析(机器学习通关指南·完结)
  5. java 读取换行_Java中的换行字符读取 - java
  6. 怎么把.nii文件转成.dcm文件
  7. APP拉新的五种方法,找准目标用户,转为注册用户
  8. 数据结构 经典面试题 用两个队列实现一个栈
  9. 学习Spring,这篇就够了
  10. 区块链开发区块链架构与应用PPT