【Python】Python实现简易中国象棋(非客户端)

本文介绍了使用Python实现的一个非客户端中国象棋程序,目前包含了部分象棋规则,但check函数仍有待完善。作者是一名高三学生,此项目作为练习,仅供娱乐和探讨。
摘要由CSDN通过智能技术生成

        目前已经加入部分象棋规则(check函数待完善

        附源代码,在读高三生小练习,权当把玩,抛砖引玉

        ------------------------------------------------------------------

        ####游玩时需使用象棋棋谱术语####

        ------------------------------------------------------------------

class ChineseChess:#中国象棋类
    def __init__(self,table = [[]for i in range(10)],cache=[],color='红'):
        self.table=table
        self.cache=cache
        self.color=color
    def new(self):#建立新棋盘
        for j in range(10):
            if j == 0:
                self.table[j]=['黑车','黑马','黑象','黑士','黑将','黑士','黑象','黑马','黑车']
            elif j in [1,4,5,8]:
                self.table[j]=['一'*2 for k in range(9)]
            elif j == 2:
                self.table[j]=['一'*2 for k in range(9)]
                self.table[j][1]='黑炮'
                self.table[j][7]='黑炮'
            elif j == 3:
                self.table[j]=['黑卒' for k in range(9)]
                for k in [1,3,5,7]:
                    self.table[j][k]='一'*2
            elif j == 9:
                self.table[j]=['红车','红马','红相','红仕','红帅','红仕','红相','红马','红车']
            elif j == 7:
                self.table[j]=['一'*2 for k in range(9)]
                self.table[j][1]='红炮'
                self.table[j][7]='红炮'
            elif j == 6:
                self.table[j]=['红兵' for k in range(9)]
                for k in [1,3,5,7]:
                    self.table[j][k]='一'*2
    def move(self,name,location,direction,step,color):#移动棋子
        if color == '红':#红棋行动
            for m in range(10):
                if self.table[m][location] == color + name:
                    if name == '马':
                        if abs(step - location) == 2:
                            if direction == '进':
                                self.cache.append([[self.table[m-1][step],[m-1,step]],[self.table[m][location],[m,location]]])
                                self.table[m-1][step]=color+name
                                self.table[m][location]='一'*2
                            else:
                                self.cache.append([[self.table[m+1][step],[m+1,step]],[self.table[m][location],[m,location]]])
                                self.table[m+1][step]=color+name
                                self.table[m][location]='一'*2
                        else:
                            if direction == '进':
                                self.cache.append([[self.table[m-2][step],[m-2,step]],[self.table[m][location],[m,location]]])
                                self.table[m-2][step]=color+name
                                self.table[m][location]='一'*2
                            else:
                                self.cache.append([[self.table[m+2][step],[m+2,step]],[self.table[m][location],[m,location]]])
                                self.table[m+2][step]=color+name
                                self.table[m][location]='一'*2
                    elif name == '相':
                        if direction == '进':
                            self.cache.append([[self.table[m-2][step],[m-2,step]],[self.table[m][location],[m,location]]])
                            self.table[m-2][step]=color+name
                            self.table[m][location]='一'*2
                        else:
                            self.cache.append([[self.table[m+2][step],[m+2,step]],[self.table[m][location],[m,location]]])
                            self.table[m+2][step]=color+name
                            self.table[m][location]='一'*2
                    elif name == '仕':
                        if direction == '进':
                            self.cache.append([[self.table[m-1][step],[m-1,step]],[self.table[m][location],[m,location]]])
                            self.table[m-1][step]=color+name
                            self.table[m][location]='一'*2
                        else:
                            self.cache.append([[self.table[m+1][step],[m+1,step]],[self.table[m][location],[m,location]]])
                            self.table[m+1][step]=color+name
                            self.table[m][location]='一'*2
                    else:
                        dic={8:1,7:2,6:3,5:4,4:5,3:6,2:7,1:8,0:9}
                        if direction == '进':                          
                            step=dic[step]
                            self.cache.append([[self.table[m-step][location],[m-step,location]],[self.table[m][location],[m,location]]])
                            self.table[m-step][location]&#
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值