目前已经加入部分象棋规则(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]&#