示例#1
0
 def boss(self, active: ChessPiece, dest_piece: ChessPiece, origin: {}, dest: {}):
     # 判断是否将帅见面,这种情况下可以移动到对方大本营里吃对方主将
     if active.get_type() == constants.CHESS_BOSS and dest_piece.get_type() == constants.CHESS_BOSS and origin['x'] == dest['x']:
         middle_count = 0
         for idx in range(min(origin['y'], dest['y']) + 1, max(origin['y'], dest['y'])):
             if self.pieces[idx][dest['x']].get_type() != constants.CHESS_PIECE:
                 middle_count += 1
                 break
         if middle_count == 0:
             return True
     # 判断是否走的直线且距离为1
     if abs(dest['x'] - origin['x']) + abs(dest['y'] - origin['y']) != 1:
         return False
     # 判断是否移出左右边界
     if dest['x'] < self._boss_x_left or dest['x'] > self._boss_x_right:
         return False
     # 判断是否移出Y轴边界
     if self._red_up:
         if active.get_camp() == constants.CAMP_RED:
             if dest['y'] > self._boss_y_red:
                 return False
         else:
             if dest['y'] < self._boss_y_white:
                 return False
     else:  # 红方在下面
         if active.get_camp() == constants.CAMP_RED:
             if dest['y'] < self._boss_y_red:
                 return False
         else:
             if dest['y'] > self._boss_y_white:
                 return False
     return True
示例#2
0
 def xiang(self, active: ChessPiece, origin: {}, dest: {}):
     # 判断是否是田字走法
     if abs(origin['x'] - dest['x']) != 2 or abs(origin['y'] - dest['y']) != 2:
         return False
     # 判断是否拗象脚
     tmpx = (dest['x'] - origin['x'])//2 + origin['x']
     tmpy = (dest['y'] - origin['y'])//2 + origin['y']
     if self.pieces[tmpy][tmpx].get_camp() != constants.CAMP_NONE:
         return False
     # 象不能过河的判断
     if self._red_up:
         if active.get_camp() == constants.CAMP_RED:
             if dest['y'] > self._red_line:
                 return False
         else:
             if dest['y'] < self._white_line:
                 return False
     else:  # 红方在下面
         if active.get_camp() == constants.CAMP_RED:
             if dest['y'] < self._red_line:
                 return False
         else:
             if dest['y'] > self._white_line:
                 return False
     return True
 def do_release(self):
     # 判定是否是取消,是取消则不翻转当前选手
     if self._cursor_loc['x'] == self._checked_loc[
             'x'] and self._cursor_loc['y'] == self._checked_loc['y']:
         self._checked = ~self._checked
         self._pieces[self._cursor_loc['y']][self._cursor_loc['x']].set_pic(
             self.cursor_pic())
         self.clear_map()
         self.show_map()
         return
     if self.can_release():
         self._checked = ~self._checked
         # 判断游戏是否结束
         if self._cursor_original_backup.get_camp(
         ) != constants.CAMP_NONE and self._cursor_original_backup.get_type(
         ) == constants.CHESS_BOSS:
             self.game_over = True
             self.winner = self.firster
         # 切换释放棋子后的布局
         self._cursor_original_backup = self._pieces[
             self._checked_loc['y']][self._checked_loc['x']]
         tmp = ChessPiece(self.PIECE_NONE, constants.CAMP_NONE)
         tmp.set_type(constants.CHESS_PIECE)
         self._pieces[self._checked_loc['y']][self._checked_loc['x']] = tmp
         self._pieces[self._cursor_loc['y']][self._cursor_loc['x']].set_pic(
             self.cursor_pic())
         self.clear_map()
         self.show_map()
         # 切换选手
         self.reverse_role()
 def cursor_move(self, p_target):
     self._pieces[self._cursor_loc['y']][
         self._cursor_loc['x']] = self._cursor_original_backup
     self._cursor_original_backup = self._pieces[p_target['y']][
         p_target['x']]
     tmp = ChessPiece(self.cursor_pic(), constants.CAMP_CURSOR)
     tmp.set_type(constants.CHESS_CURSOR)
     self._pieces[p_target['y']][p_target['x']] = tmp
     self._cursor_loc = p_target
     # 刷新地图
     self.clear_map()
     self.show_map()
示例#5
0
 def test_init(self):
     # test valido
     xs = [chr(ord('a') + p) for p in range(0, 8)]  # a,b,c,d,e,f,g,h
     ys = [p for p in range(1, 9)]  # 1,2,3,4,5,6,7,8
     for x, y in product(xs, ys):
         try:
             p = ChessPiece(x, y)
         except:
             self.fail(f'{x}{y} seems invalid...')
     xs = [chr(ord('i') + p) for p in range(0, 8)]
     ys = [i for i in range(9, 100)] + [i for i in range(-1, -30)]
     for x, y in product(xs, ys):
         try:
             p = ChessPiece(x, y)
             self.fail(f'{x}{y} seems valid but is not valid...')
         except:
             pass
示例#6
0
    def pao(self, dest_piece: ChessPiece, origin: {}, dest: {}):
        if not self.is_straight(origin, dest):
            return False

        middle_count = 0
        if origin['x'] - dest['x'] != 0:  # 横着走
            for idx in range(min(origin['x'], dest['x']) + 1, max(origin['x'], dest['x'])):
                if self.pieces[dest['y']][idx].get_type() != constants.CHESS_PIECE:
                    middle_count += 1
        else:  # 竖着走
            for idx in range(min(origin['y'], dest['y']) + 1, max(origin['y'], dest['y'])):
                if self.pieces[idx][dest['x']].get_type() != constants.CHESS_PIECE:
                    middle_count += 1
        if middle_count > 1:
            return False
        if middle_count == 1 and dest_piece.get_camp() == constants.CAMP_NONE:
            return False
        if middle_count == 0 and dest_piece.get_camp() != constants.CAMP_NONE:
            return False
        return True
示例#7
0
 def bing(self, active: ChessPiece, origin: {}, dest: {}):
     # 存在斜着走判定
     if not self.is_straight(origin, dest):
         return False
     # 存在移动超过多步判定
     if abs(origin['x'] - dest['x']) > 1 or abs(origin['y'] - dest['y']) > 1:
         return False
     # 不能后退判定和过了河才能左右移动判定
     if self._red_up:
         if active.get_camp() == constants.CAMP_RED:
             if dest['y'] - origin['y'] < 0:
                 return False
             # 过河才能左右移动
             if abs(dest['x'] - origin['x']) > 0:
                 if origin['y'] <= self._red_line:
                     return False
         else:
             if dest['y'] - origin['y'] > 0:
                 return False
             # 过了河才能左右移动
             if abs(dest['x'] - origin['x']) > 0:
                 if origin['y'] >= self._white_line:
                     return False
     else:  # 红方在下面
         if active.get_camp() == constants.CAMP_RED:
             if dest['y'] - origin['y'] > 0:
                 return False
             # 过了河才能左右移动
             if abs(dest['x'] - origin['x']) > 0:
                 if origin['y'] >= self._white_line:
                     return False
         else:
             if dest['y'] - origin['y'] < 0:
                 return False
             # 过了河才能左右移动
             if abs(dest['x'] - origin['x']) > 0:
                 if origin['y'] <= self._white_line:
                     return False
     return True
示例#8
0
 def shi(self, active: ChessPiece, origin: {}, dest: {}):
     # 判断是否走的斜线且距离为1
     if abs((dest['x'] - origin['x']) * (dest['y'] - origin['y'])) != 1:
         return False
     # 判断是否移出左右边界
     if dest['x'] < self._boss_x_left or dest['x'] > self._boss_x_right:
         return False
     # 判断是否移出Y轴边界
     if self._red_up:
         if active.get_camp() == constants.CAMP_RED:
             if dest['y'] > self._boss_y_red:
                 return False
         else:
             if dest['y'] < self._boss_y_white:
                 return False
     else:  # 红方在下面
         if active.get_camp() == constants.CAMP_RED:
             if dest['y'] < self._boss_y_red:
                 return False
         else:
             if dest['y'] > self._boss_y_white:
                 return False
     return True
    def __init__(self, camp=constants.CAMP_RED):
        if camp == constants.CAMP_WHITE:
            self._cursor_loc['y'] = 8
        elif camp != constants.CAMP_RED:
            print('阵营参数错误,游戏退出')
            exit()
        self.firster = camp
        # init pieces
        # init NONE Camp
        for y in range(self._height):
            line = []
            for x in range(self._width):
                tmp = ChessPiece(self.PIECE_NONE, constants.CAMP_NONE)
                tmp.set_type(constants.CHESS_PIECE)
                line.append(tmp)
            self._pieces.append(line)
        del line
        # init RED Camp and Pic
        for idx in range(self._width):
            self._pieces[0][idx].set_camp(constants.CAMP_RED)
        self._pieces[0][0].set_pic(' 車 ')
        self._pieces[0][0].set_type(constants.CHESS_CHE)
        self._pieces[0][1].set_pic(' 馬 ')
        self._pieces[0][1].set_type(constants.CHESS_MA)
        self._pieces[0][2].set_pic(' 象 ')
        self._pieces[0][2].set_type(constants.CHESS_XIANG)
        self._pieces[0][3].set_pic(' 士 ')
        self._pieces[0][3].set_type(constants.CHESS_SHI)
        self._pieces[0][4].set_pic(' 將 ')
        self._pieces[0][4].set_type(constants.CHESS_BOSS)
        self._pieces[0][5].set_pic(' 士 ')
        self._pieces[0][5].set_type(constants.CHESS_SHI)
        self._pieces[0][6].set_pic(' 象 ')
        self._pieces[0][6].set_type(constants.CHESS_XIANG)
        self._pieces[0][7].set_pic(' 馬 ')
        self._pieces[0][7].set_type(constants.CHESS_MA)
        self._pieces[0][8].set_pic(' 車 ')
        self._pieces[0][8].set_type(constants.CHESS_CHE)
        # 上面的camp已经通过循环统一设置过了
        self._pieces[2][1].set_pic(' 炮 ')
        self._pieces[2][1].set_type(constants.CHESS_PAO)
        self._pieces[2][1].set_camp(constants.CAMP_RED)
        self._pieces[2][7].set_pic(' 炮 ')
        self._pieces[2][7].set_type(constants.CHESS_PAO)
        self._pieces[2][7].set_camp(constants.CAMP_RED)
        self._pieces[3][0].set_pic(' 卒 ')
        self._pieces[3][0].set_type(constants.CHESS_BING)
        self._pieces[3][0].set_camp(constants.CAMP_RED)
        self._pieces[3][2].set_pic(' 卒 ')
        self._pieces[3][2].set_type(constants.CHESS_BING)
        self._pieces[3][2].set_camp(constants.CAMP_RED)
        self._pieces[3][4].set_pic(' 卒 ')
        self._pieces[3][4].set_type(constants.CHESS_BING)
        self._pieces[3][4].set_camp(constants.CAMP_RED)
        self._pieces[3][6].set_pic(' 卒 ')
        self._pieces[3][6].set_type(constants.CHESS_BING)
        self._pieces[3][6].set_camp(constants.CAMP_RED)
        self._pieces[3][-1].set_pic(' 卒 ')
        self._pieces[3][-1].set_type(constants.CHESS_BING)
        self._pieces[3][-1].set_camp(constants.CAMP_RED)

        # init WHITE Camp and Pic
        for idx in range(self._width):
            self._pieces[-1][idx].set_camp(constants.CAMP_WHITE)
        self._pieces[-1][0].set_pic(' 車 ')
        self._pieces[-1][0].set_type(constants.CHESS_CHE)
        self._pieces[-1][1].set_pic(' 馬 ')
        self._pieces[-1][1].set_type(constants.CHESS_MA)
        self._pieces[-1][2].set_pic(' 相 ')
        self._pieces[-1][2].set_type(constants.CHESS_XIANG)
        self._pieces[-1][3].set_pic(' 仕 ')
        self._pieces[-1][3].set_type(constants.CHESS_SHI)
        self._pieces[-1][4].set_pic(' 帥 ')
        self._pieces[-1][4].set_type(constants.CHESS_BOSS)
        self._pieces[-1][5].set_pic(' 仕 ')
        self._pieces[-1][5].set_type(constants.CHESS_SHI)
        self._pieces[-1][6].set_pic(' 相 ')
        self._pieces[-1][6].set_type(constants.CHESS_XIANG)
        self._pieces[-1][7].set_pic(' 馬 ')
        self._pieces[-1][7].set_type(constants.CHESS_MA)
        self._pieces[-1][8].set_pic(' 車 ')
        self._pieces[-1][8].set_type(constants.CHESS_CHE)
        # 上面的camp已经通过循环统一设置过了
        self._pieces[-3][1].set_pic(' 砲 ')
        self._pieces[-3][1].set_type(constants.CHESS_PAO)
        self._pieces[-3][1].set_camp(constants.CAMP_WHITE)
        self._pieces[-3][7].set_pic(' 砲 ')
        self._pieces[-3][7].set_type(constants.CHESS_PAO)
        self._pieces[-3][7].set_camp(constants.CAMP_WHITE)
        self._pieces[-4][0].set_pic(' 兵 ')
        self._pieces[-4][0].set_type(constants.CHESS_BING)
        self._pieces[-4][0].set_camp(constants.CAMP_WHITE)
        self._pieces[-4][2].set_pic(' 兵 ')
        self._pieces[-4][2].set_type(constants.CHESS_BING)
        self._pieces[-4][2].set_camp(constants.CAMP_WHITE)
        self._pieces[-4][4].set_pic(' 兵 ')
        self._pieces[-4][4].set_type(constants.CHESS_BING)
        self._pieces[-4][4].set_camp(constants.CAMP_WHITE)
        self._pieces[-4][6].set_pic(' 兵 ')
        self._pieces[-4][6].set_type(constants.CHESS_BING)
        self._pieces[-4][6].set_camp(constants.CAMP_WHITE)
        self._pieces[-4][8].set_pic(' 兵 ')
        self._pieces[-4][8].set_type(constants.CHESS_BING)
        self._pieces[-4][8].set_camp(constants.CAMP_WHITE)

        # init cursor 〄 〠
        self._cursor_original_backup: ChessPiece = self._pieces[
            self._cursor_loc['y']][self._cursor_loc['x']]
        tmp = ChessPiece(self.cursor_pic(), constants.CAMP_CURSOR)
        tmp.set_type(constants.CHESS_CURSOR)
        self._pieces[self._cursor_loc['y']][self._cursor_loc['x']] = tmp
        del tmp

        self.strategies = chess_strategy.ChessStrategy(self._pieces, True)
示例#10
0
 def promotion(self, chess_piece: ChessPiece):  # 升变
     self.drop_a_chess_piece(chess_piece)
     self.join_a_chess_piece(
         chess_piece.promotion_next_chess_piece(chess_piece=chess_piece))
示例#11
0
 def move(self, chess_piece: ChessPiece, position: Point):  # 移子
     chess_piece.position = position