def TestMove(self, command): if command[0] == 0: #平 try: lie = Chess.row[command[1]] except KeyError: raise ChessError('不明指令') if lie != self.pos[1] + 1 and lie != self.pos[1] - 1: return (False, ) if self.pos[0] <= 4: return (False, ) pos = (self.pos[0], lie) elif command[0] == 1: #进 try: step = Chess.step[command[1]] except KeyError: raise ChessError('不明指令') if step != 1: return (False, ) pos = (self.pos[0] + step, self.pos[1]) if pos[0] > 9: return (False, ) elif command[0] == -1: #退 raise ChessError('不明指令') if not self.board.isBlank(pos) and not self.board[pos].isRed(): return (False, ) return (True, pos)
def TestMove(self, command): try: lie = Chess.row[command[1]] except KeyError: raise ChessError('不明指令') if command[0] == -1: #退 l = { self.pos[1] - 1: (self.pos[0] - 1, self.pos[1] - 1), self.pos[1] + 1: (self.pos[0] - 1, self.pos[1] + 1) } elif command[0] == 1: #进 l = { self.pos[1] - 1: (self.pos[0] + 1, self.pos[1] - 1), self.pos[1] + 1: (self.pos[0] + 1, self.pos[1] + 1) } elif command[0] == 0: #平 raise ChessError('不明指令') try: pos = l[lie] except KeyError: return (False, ) if pos[0] < 0 or pos[0] > 2 or pos[1] < 3 or pos[1] > 5: return (False, ) if not self.board.isBlank(pos) and not self.board[pos].isRed(): return (False, ) return (True, pos)
def TestMove(self, command): try: lie = Chess.row[command[1]] except KeyError: raise ChessError('不明指令') if command[0] == 1: #进 l = { self.pos[1] - 2: ((self.pos[0] - 2, self.pos[1] - 2), (self.pos[0] - 1, self.pos[1] - 1)), self.pos[1] + 2: ((self.pos[0] - 2, self.pos[1] + 2), (self.pos[0] - 1, self.pos[1] + 1)) } elif command[0] == -1: #退 l = { self.pos[1] - 2: ((self.pos[0] + 2, self.pos[1] - 2), (self.pos[0] + 1, self.pos[1] - 1)), self.pos[1] + 2: ((self.pos[0] + 2, self.pos[1] + 2), (self.pos[0] + 1, self.pos[1] + 1)) } elif command[0] == 0: #平 raise ChessError('不明指令') try: pos, tui = l[lie] except KeyError: return (False, ) if pos[0] < 5 or pos[0] > 9 or pos[1] < 0 or pos[1] > 8: return (False, ) if not self.board.isBlank(tui): return (False, ) if not self.board.isBlank(pos) and self.board[pos].isRed(): return (False, ) return (True, pos)
def redo(self): if len(self.Moves) == 0: raise ChessError('已回到最初') (to_add, to_delete) = self.Moves.pop() self.pop(to_add[1]) for i in to_delete: self.insert(i[1], i[0])
def TestMove(self, command): if command[0] == 1: #进 try: step = Chess.step[command[1]] except KeyError: raise ChessError('不明指令') if self.pos[0] - step < 0: return (False, ) for i in range(step - 1): if not self.board.isBlank((self.pos[0] - i - 1, self.pos[1])): return (False, ) pos = (self.pos[0] - step, self.pos[1]) if not self.board.isBlank(pos): if self.board[pos].isRed(): return (False, ) return (True, pos) elif command[0] == -1: #退 try: step = Chess.step[command[1]] except KeyError: raise ChessError('不明指令') if self.pos[0] + step > 9: return (False, ) for i in range(step - 1): if not self.board.isBlank((self.pos[0] + i + 1, self.pos[1])): return (False, ) pos = (self.pos[0] + step, self.pos[1]) if not self.board.isBlank(pos): if self.board[pos].isRed(): return (False, ) return (True, pos) elif command[0] == 0: #平 try: lie = Chess.row[command[1]] except KeyError: raise ChessError('不明指令') if lie == self.pos[1]: return (False, ) small, big = sorted((lie, self.pos[1])) for i in range(small + 1, big): if not self.board.isBlank((self.pos[0], i)): return (False, ) pos = (self.pos[0], lie) if not self.board.isBlank(pos) and self.board[(self.pos[0], lie)].isRed(): return (False, ) return (True, pos)
def insert(self, pos, Name): if pos in self.board: raise ChessError('已有棋子') chess = Name(pos, self) self.board[pos] = chess key = ChessBoard.insert_dict[Name] for s in key: if s not in self.find: self.find[s] = [] self.find[s].append(chess)
def TestMove(self, command): if command[0] == 0: #平 try: lie = Chess.row[command[1]] except KeyError: raise ChessError('不明指令') if lie != self.pos[1] + 1 and lie != self.pos[1] - 1: raise ChessError('不明指令') pos = (self.pos[0], lie) if lie < 3 or lie > 5: return (False, ) else: #进退 try: step = Chess.step[command[1]] except KeyError: raise ChessError('不明指令') if step != 1: raise ChessError('不明指令') pos = (self.pos[0] + step * command[0], self.pos[1]) if pos[0] < 0 or pos[0] > 2: return (False, ) if not self.board.isBlank(pos) and self.board[pos].isRed(): return (False, ) return (True, pos)
def process(self, i, j, isBlack: bool): if self.board[i][j] != 0: raise ChessError('此处已有棋子') todo_all = [(i, j)] black_need = 2 - int(isBlack) for (di, dj) in ((-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)): i2, j2 = i + di, j + dj todo = [] while 0 <= i2 <= self.height - 1 and 0 <= j2 <= self.height - 1: if self.board[i2][j2] == 0: break if self.board[i2][j2] != black_need: todo.append((i2, j2)) else: todo_all.extend(todo) break i2 += di j2 += dj if len(todo_all) == 1: raise ChessError('行棋必须吃掉对方的子') for i2, j2 in todo_all: self.board[i2][j2] = black_need white_need = 3 - black_need safe = [False, False] for i, x in enumerate(self.board): for j, y in enumerate(x): if y == white_need: for (di, dj) in ((-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)): i2, j2 = i + di, j + dj todo = [] while 0 <= i2 <= self.height - 1 and 0 <= j2 <= self.height - 1: if self.board[i2][j2] == 0: if len(todo) >= 1: safe[1] = True break if self.board[i2][j2] == black_need: todo.append((i2, j2)) else: break i2 += di j2 += dj elif y == black_need: for (di, dj) in ((-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)): i2, j2 = i + di, j + dj todo = [] while 0 <= i2 <= self.height - 1 and 0 <= j2 <= self.height - 1: if self.board[i2][j2] == 0: if len(todo) >= 1: safe[0] = True break if self.board[i2][j2] == white_need: todo.append((i2, j2)) else: break i2 += di j2 += dj if safe == [True, True]: return #unsafe! if safe == [False, False]: num = [0, 0, 0] for x in self.board: for y in x: num[y] += 1 if num[1] > num[2]: raise ChessWin("黑方胜出") elif num[1] < num[2]: raise ChessWin("白方胜出") else: raise ChessWin("平局!") if not safe[black_need - 1]: raise ChessCantMove("黑白"[2 - black_need] + "方无法移动," + "黑白"[black_need - 1] + "方继续移动")
def process(self, command, isRed): """command: 4 char""" if command[0] in ['前', '中', '后']: if len(command) != 4 and len(command) != 5: raise ChessError("需要四/五个汉字作为指令") try: chess_list = self.find[command[1]] except KeyError: raise ChessError("已无该棋子") chess_list = list(filter(lambda x: x.isRed() == isRed, chess_list)) if len(command) == 5: #选取指定列数 try: lie = Chess.row[command[2]] except KeyError: raise ChessError("不明指令") chess_list = list(filter(lambda x: x.pos[1] == lie, chess_list)) chess_lie = {} for chess in chess_list: if chess.pos[1] not in chess_lie: chess_lie[chess.pos[1]] = [] chess_lie[chess.pos[1]].append(chess) chess_list = [] for lie, l in chess_lie.items(): l.sort(key=lambda x: x.pos[0], reverse=not isRed) if len(l) == 1: continue elif len(l) == 2: try: chess_list.append({'前': l[0], '后': l[1]}[command[0]]) except KeyError: continue elif len(l) == 3: chess_list.append({ '前': l[0], '中': l[1], '后': l[2] }[command[0]]) elif len(l) == 4: for x in { '前': (l[0], ), '中': (l[1], l[2]), '后': (l[3], ) }[command[0]]: chess_list.append(x) elif len(l) == 5: for x in { '前': (l[0], ), '中': (l[1], l[2], l[3]), '后': (l[3], ) }[command[0]]: chess_list.append(x) else: if len(command) != 4: raise ChessError("需要四个汉字作为指令") try: chess_list = self.find[command[0]] except KeyError: raise ChessError("已无该棋子") chess_list = list(filter(lambda x: x.isRed() == isRed, chess_list)) #选取指定列数 try: lie = Chess.row[command[1]] except KeyError: raise ChessError("不明指令") chess_list = list(filter(lambda x: x.pos[1] == lie, chess_list)) try: move_command = ({ '进': 1, '平': 0, '退': -1 }[command[-2]], command[-1]) except KeyError: raise ChessError("不明指令") if len(chess_list) != 1: #TestMove returns (True, pos_mubiao) or (False, ) chess_list2 = list(filter(lambda x: x[1], list(map(lambda x: (x,) + \ x.TestMove(move_command), chess_list)))) if len(chess_list2) > 1: raise ChessError("棋子不唯一") if len(chess_list2) == 0: raise ChessError("未找到棋子") duijiang = self.checkDuiJiang() if duijiang is not None and chess_list2[0][ 0] is duijiang and duijiang.pos[1] != chess_list2[0][2][1]: raise ChessError("对将") chess_list2[0][0].Move(chess_list2[0][2]) else: #TestMove returns (True, pos_mubiao) or (False, ) p = chess_list[0].TestMove(move_command) if not p[0]: raise ChessError("无法移动") duijiang = self.checkDuiJiang() if duijiang is not None and chess_list[ 0] is duijiang and duijiang.pos[1] != p[1][1]: raise ChessError("对将") chess_list[0].Move(p[1])