├── __init__.py ├── Chess_AI └── __init__.py ├── Chess_Core ├── __init__.py ├── Point.py ├── Test.py ├── Chessboard.py └── Chessman.py ├── Chess_UI ├── __init__.py ├── Img │ ├── red_king.gif │ ├── red_king.jpg │ ├── red_pawn.gif │ ├── red_pawn.jpg │ ├── red_rook.gif │ ├── red_rook.jpg │ ├── black_king.gif │ ├── black_king.jpg │ ├── black_pawn.gif │ ├── black_pawn.jpg │ ├── black_rook.gif │ ├── black_rook.jpg │ ├── boardchess.gif │ ├── boardchess.jpg │ ├── red_cannon.gif │ ├── red_cannon.jpg │ ├── red_knight.gif │ ├── red_knight.jpg │ ├── transparent.gif │ ├── black_cannon.gif │ ├── black_cannon.jpg │ ├── black_elephant.gif │ ├── black_elephant.jpg │ ├── black_knight.gif │ ├── black_knight.jpg │ ├── black_mandarin.gif │ ├── black_mandarin.jpg │ ├── red_elephant.gif │ ├── red_elephant.jpg │ ├── red_mandarin.gif │ └── red_mandarin.jpg ├── sounds │ ├── clock.mp3 │ ├── dong.mp3 │ ├── move.mp3 │ ├── berserk.mp3 │ ├── capture.mp3 │ ├── lowtime.mp3 │ ├── explosion.mp3 │ └── confirmation.mp3 ├── cli_game.py └── win_game.py ├── requirements.txt ├── .gitignore └── Readme.md /__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Chess_AI/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Chess_Core/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Chess_UI/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | pygame -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | .vscode/* 3 | -------------------------------------------------------------------------------- /Chess_UI/Img/red_king.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mm12432/MyChess/HEAD/Chess_UI/Img/red_king.gif -------------------------------------------------------------------------------- /Chess_UI/Img/red_king.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mm12432/MyChess/HEAD/Chess_UI/Img/red_king.jpg -------------------------------------------------------------------------------- /Chess_UI/Img/red_pawn.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mm12432/MyChess/HEAD/Chess_UI/Img/red_pawn.gif -------------------------------------------------------------------------------- /Chess_UI/Img/red_pawn.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mm12432/MyChess/HEAD/Chess_UI/Img/red_pawn.jpg -------------------------------------------------------------------------------- /Chess_UI/Img/red_rook.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mm12432/MyChess/HEAD/Chess_UI/Img/red_rook.gif -------------------------------------------------------------------------------- /Chess_UI/Img/red_rook.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mm12432/MyChess/HEAD/Chess_UI/Img/red_rook.jpg -------------------------------------------------------------------------------- /Chess_UI/sounds/clock.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mm12432/MyChess/HEAD/Chess_UI/sounds/clock.mp3 -------------------------------------------------------------------------------- /Chess_UI/sounds/dong.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mm12432/MyChess/HEAD/Chess_UI/sounds/dong.mp3 -------------------------------------------------------------------------------- /Chess_UI/sounds/move.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mm12432/MyChess/HEAD/Chess_UI/sounds/move.mp3 -------------------------------------------------------------------------------- /Chess_UI/Img/black_king.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mm12432/MyChess/HEAD/Chess_UI/Img/black_king.gif -------------------------------------------------------------------------------- /Chess_UI/Img/black_king.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mm12432/MyChess/HEAD/Chess_UI/Img/black_king.jpg -------------------------------------------------------------------------------- /Chess_UI/Img/black_pawn.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mm12432/MyChess/HEAD/Chess_UI/Img/black_pawn.gif -------------------------------------------------------------------------------- /Chess_UI/Img/black_pawn.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mm12432/MyChess/HEAD/Chess_UI/Img/black_pawn.jpg -------------------------------------------------------------------------------- /Chess_UI/Img/black_rook.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mm12432/MyChess/HEAD/Chess_UI/Img/black_rook.gif -------------------------------------------------------------------------------- /Chess_UI/Img/black_rook.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mm12432/MyChess/HEAD/Chess_UI/Img/black_rook.jpg -------------------------------------------------------------------------------- /Chess_UI/Img/boardchess.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mm12432/MyChess/HEAD/Chess_UI/Img/boardchess.gif -------------------------------------------------------------------------------- /Chess_UI/Img/boardchess.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mm12432/MyChess/HEAD/Chess_UI/Img/boardchess.jpg -------------------------------------------------------------------------------- /Chess_UI/Img/red_cannon.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mm12432/MyChess/HEAD/Chess_UI/Img/red_cannon.gif -------------------------------------------------------------------------------- /Chess_UI/Img/red_cannon.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mm12432/MyChess/HEAD/Chess_UI/Img/red_cannon.jpg -------------------------------------------------------------------------------- /Chess_UI/Img/red_knight.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mm12432/MyChess/HEAD/Chess_UI/Img/red_knight.gif -------------------------------------------------------------------------------- /Chess_UI/Img/red_knight.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mm12432/MyChess/HEAD/Chess_UI/Img/red_knight.jpg -------------------------------------------------------------------------------- /Chess_UI/Img/transparent.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mm12432/MyChess/HEAD/Chess_UI/Img/transparent.gif -------------------------------------------------------------------------------- /Chess_UI/sounds/berserk.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mm12432/MyChess/HEAD/Chess_UI/sounds/berserk.mp3 -------------------------------------------------------------------------------- /Chess_UI/sounds/capture.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mm12432/MyChess/HEAD/Chess_UI/sounds/capture.mp3 -------------------------------------------------------------------------------- /Chess_UI/sounds/lowtime.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mm12432/MyChess/HEAD/Chess_UI/sounds/lowtime.mp3 -------------------------------------------------------------------------------- /Chess_UI/Img/black_cannon.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mm12432/MyChess/HEAD/Chess_UI/Img/black_cannon.gif -------------------------------------------------------------------------------- /Chess_UI/Img/black_cannon.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mm12432/MyChess/HEAD/Chess_UI/Img/black_cannon.jpg -------------------------------------------------------------------------------- /Chess_UI/Img/black_elephant.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mm12432/MyChess/HEAD/Chess_UI/Img/black_elephant.gif -------------------------------------------------------------------------------- /Chess_UI/Img/black_elephant.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mm12432/MyChess/HEAD/Chess_UI/Img/black_elephant.jpg -------------------------------------------------------------------------------- /Chess_UI/Img/black_knight.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mm12432/MyChess/HEAD/Chess_UI/Img/black_knight.gif -------------------------------------------------------------------------------- /Chess_UI/Img/black_knight.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mm12432/MyChess/HEAD/Chess_UI/Img/black_knight.jpg -------------------------------------------------------------------------------- /Chess_UI/Img/black_mandarin.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mm12432/MyChess/HEAD/Chess_UI/Img/black_mandarin.gif -------------------------------------------------------------------------------- /Chess_UI/Img/black_mandarin.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mm12432/MyChess/HEAD/Chess_UI/Img/black_mandarin.jpg -------------------------------------------------------------------------------- /Chess_UI/Img/red_elephant.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mm12432/MyChess/HEAD/Chess_UI/Img/red_elephant.gif -------------------------------------------------------------------------------- /Chess_UI/Img/red_elephant.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mm12432/MyChess/HEAD/Chess_UI/Img/red_elephant.jpg -------------------------------------------------------------------------------- /Chess_UI/Img/red_mandarin.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mm12432/MyChess/HEAD/Chess_UI/Img/red_mandarin.gif -------------------------------------------------------------------------------- /Chess_UI/Img/red_mandarin.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mm12432/MyChess/HEAD/Chess_UI/Img/red_mandarin.jpg -------------------------------------------------------------------------------- /Chess_UI/sounds/explosion.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mm12432/MyChess/HEAD/Chess_UI/sounds/explosion.mp3 -------------------------------------------------------------------------------- /Chess_UI/sounds/confirmation.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mm12432/MyChess/HEAD/Chess_UI/sounds/confirmation.mp3 -------------------------------------------------------------------------------- /Chess_Core/Point.py: -------------------------------------------------------------------------------- 1 | # coding:utf8 2 | 3 | class Point(object): 4 | def __init__(self,x,y): 5 | self.x=x 6 | self.y=y -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # Usage 2 | Download the MyChess and libraries. 3 | ```bash 4 | git clone https://github.com/mm12432/MyChess.git 5 | pip install -r requirements.txt 6 | ``` 7 | 8 | Run MyChess with UI(recommended) 9 | ```bash 10 | python -m MyChess.Chess_UI.win_game 11 | ``` 12 | 13 | 14 | Run MyChess with terminal 15 | ```bash 16 | python -m MyChess.Chess_UI.cli_gmae 17 | ``` 18 | -------------------------------------------------------------------------------- /Chess_Core/Test.py: -------------------------------------------------------------------------------- 1 | # coding:utf-8 2 | from MyChess.Chess_Core import Chessboard 3 | 4 | 5 | def print_chessman_name(chessman): 6 | if chessman: 7 | print(chessman.name) 8 | else: 9 | print("None") 10 | 11 | 12 | def main(): 13 | cbd = Chessboard.Chessboard('123') 14 | cbd.init_board() 15 | cbd.print_to_cl() 16 | while not cbd.is_end(): 17 | cbd.calc_chessmans_moving_list() 18 | if cbd.is_red_turn: 19 | print("is_red_turn") 20 | else: 21 | print("is_black_turn") 22 | is_correct_chessman = False 23 | is_correct_position = False 24 | chessman = None 25 | while not is_correct_chessman: 26 | title = "请输入棋子名字: " 27 | input_chessman_name = input(title) 28 | chessman = cbd.get_chessman_by_name(input_chessman_name) 29 | if chessman != None and chessman.is_red == cbd.is_red_turn: 30 | is_correct_chessman = True 31 | print("当前可以落子的位置有:") 32 | for point in chessman.moving_list: 33 | print(point.x, point.y) 34 | else: 35 | print("没有找到此名字的棋子或未轮到此方走子") 36 | while not is_correct_position: 37 | title = "请输入落子的位置: " 38 | input_chessman_position = input(title) 39 | is_correct_position = chessman.move( 40 | input_chessman_position[0], input_chessman_position[1]) 41 | if is_correct_position: 42 | cbd.print_to_cl() 43 | cbd.clear_chessmans_moving_list() 44 | 45 | 46 | if __name__ == '__main__': 47 | main() 48 | -------------------------------------------------------------------------------- /Chess_UI/cli_game.py: -------------------------------------------------------------------------------- 1 | # coding:utf-8 2 | from MyChess.Chess_Core import Chessboard 3 | from MyChess.Chess_Core import Chessman 4 | 5 | 6 | def print_chessman_name(chessman): 7 | if chessman: 8 | print(chessman.name) 9 | else: 10 | print("None") 11 | 12 | 13 | def main(): 14 | cbd = Chessboard.Chessboard('000') 15 | cbd.init_board() 16 | cbd.print_to_cl() 17 | while not cbd.is_end(): 18 | cbd.calc_chessmans_moving_list() 19 | if cbd.is_red_turn: 20 | print("is_red_turn") 21 | else: 22 | print("is_black_turn") 23 | is_correct_chessman = False 24 | is_correct_position = False 25 | chessman = None 26 | while not is_correct_chessman: 27 | title = "请输入棋子名字: " 28 | input_chessman_name = input(title) 29 | chessman = cbd.get_chessman_by_name(input_chessman_name) 30 | if chessman != None and chessman.is_red == cbd.is_red_turn: 31 | is_correct_chessman = True 32 | print("当前可以落子的位置有:") 33 | for point in chessman.moving_list: 34 | print(point.x, point.y) 35 | else: 36 | print("没有找到此名字的棋子或未轮到此方走子") 37 | while not is_correct_position: 38 | title = "请输入落子的位置: " 39 | input_chessman_position = input(title) 40 | is_correct_position = chessman.move( 41 | input_chessman_position[0], input_chessman_position[1]) 42 | if is_correct_position: 43 | cbd.print_to_cl() 44 | cbd.clear_chessmans_moving_list() 45 | 46 | 47 | if __name__ == '__main__': 48 | main() 49 | -------------------------------------------------------------------------------- /Chess_UI/win_game.py: -------------------------------------------------------------------------------- 1 | # coding:utf-8 2 | import sys 3 | import pygame 4 | import random 5 | import os.path 6 | from MyChess.Chess_Core import Chessboard 7 | from MyChess.Chess_Core import Point 8 | from MyChess.Chess_Core import Chessman 9 | from pygame.locals import * 10 | 11 | main_dir = os.path.split(os.path.abspath(__file__))[0] 12 | SCREENRECT = Rect(0, 0, 720, 800) 13 | 14 | 15 | def load_image(file): 16 | "loads an image, prepares it for play" 17 | file = os.path.join(main_dir, 'Img', file) 18 | try: 19 | surface = pygame.image.load(file) 20 | except pygame.error: 21 | raise SystemExit('Could not load image "%s" %s' % 22 | (file, pygame.get_error())) 23 | return surface.convert() 24 | def load_sound(file): 25 | file = os.path.join(main_dir, 'sounds', file) 26 | try: 27 | sound = pygame.mixer.Sound(file) 28 | except pygame.error: 29 | raise SystemExit('Could not load sound "%s" %s' % 30 | (file, pygame.get_error())) 31 | return sound 32 | 33 | def load_images(*files): 34 | imgs = [] 35 | for file in files: 36 | imgs.append(load_image(file)) 37 | return imgs 38 | 39 | 40 | class Chessman_Sprite(pygame.sprite.Sprite): 41 | is_selected = False 42 | images = [] 43 | is_transparent = False 44 | def __init__(self, images, Kill_sound, chessman): 45 | pygame.sprite.Sprite.__init__(self) 46 | self.chessman = chessman 47 | self.images = images 48 | self.image = self.images[0] 49 | self.rect = Rect(chessman.col_num * 80, 50 | (9 - chessman.row_num) * 80, 80, 80) 51 | self.move_sound = load_sound("move.mp3") 52 | self.Kill_sound = Kill_sound 53 | 54 | def move(self, col_num, row_num): 55 | old_col_num = self.chessman.col_num 56 | old_row_num = self.chessman.row_num 57 | is_correct_position = self.chessman.move(col_num, row_num) 58 | if is_correct_position: 59 | self.rect.move_ip((col_num - old_col_num) 60 | * 80, (old_row_num - row_num) * 80) 61 | self.rect = self.rect.clamp(SCREENRECT) 62 | self.chessman.chessboard.clear_chessmans_moving_list() 63 | self.chessman.chessboard.calc_chessmans_moving_list() 64 | return True 65 | load_sound("lowtime.mp3").play() 66 | return False 67 | 68 | def update(self): 69 | if self.is_selected: 70 | if self.is_transparent: 71 | self.image = self.images[1] 72 | else: 73 | self.image = self.images[0] 74 | self.is_transparent = not self.is_transparent 75 | else: 76 | self.image = self.images[0] 77 | 78 | 79 | def creat_sprite_group(sprite_group, chessmans_hash): 80 | for chessman in chessmans_hash.values(): 81 | if chessman.is_red: 82 | if isinstance(chessman, Chessman.Rook): 83 | images = load_images("red_rook.gif", "transparent.gif") 84 | elif isinstance(chessman, Chessman.Cannon): 85 | images = load_images("red_cannon.gif", "transparent.gif") 86 | elif isinstance(chessman, Chessman.Knight): 87 | images = load_images("red_knight.gif", "transparent.gif") 88 | elif isinstance(chessman, Chessman.King): 89 | images = load_images("red_king.gif", "transparent.gif") 90 | elif isinstance(chessman, Chessman.Elephant): 91 | images = load_images("red_elephant.gif", "transparent.gif") 92 | elif isinstance(chessman, Chessman.Mandarin): 93 | images = load_images("red_mandarin.gif", "transparent.gif") 94 | else: 95 | images = load_images("red_pawn.gif", "transparent.gif") 96 | else: 97 | if isinstance(chessman, Chessman.Rook): 98 | images = load_images("black_rook.gif", "transparent.gif") 99 | elif isinstance(chessman, Chessman.Cannon): 100 | images = load_images("black_cannon.gif", "transparent.gif") 101 | elif isinstance(chessman, Chessman.Knight): 102 | images = load_images("black_knight.gif", "transparent.gif") 103 | elif isinstance(chessman, Chessman.King): 104 | images = load_images("black_king.gif", "transparent.gif") 105 | elif isinstance(chessman, Chessman.Elephant): 106 | images = load_images("black_elephant.gif", "transparent.gif") 107 | elif isinstance(chessman, Chessman.Mandarin): 108 | images = load_images("black_mandarin.gif", "transparent.gif") 109 | else: 110 | images = load_images("black_pawn.gif", "transparent.gif") 111 | if isinstance(chessman,Chessman.Cannon): 112 | Kill_sound = load_sound("explosion.mp3") 113 | else: 114 | Kill_sound = load_sound("berserk.mp3") 115 | chessman_sprite = Chessman_Sprite(images, Kill_sound, chessman) 116 | sprite_group.add(chessman_sprite) 117 | 118 | 119 | def select_sprite_from_group(sprite_group, col_num, row_num): 120 | for sprite in sprite_group: 121 | if sprite.chessman.col_num == col_num and sprite.chessman.row_num == row_num: 122 | return sprite 123 | 124 | 125 | def translate_hit_area(screen_x, screen_y): 126 | return screen_x // 80, 9 - screen_y // 80 127 | 128 | 129 | def main(winstyle=0): 130 | # set up mixer function for sounds 131 | pygame.mixer.init() 132 | pygame.init() 133 | bestdepth = pygame.display.mode_ok(SCREENRECT.size, winstyle, 32) 134 | screen = pygame.display.set_mode(SCREENRECT.size, winstyle, bestdepth) 135 | pygame.display.set_caption("中国象棋最强AI") 136 | 137 | # create the background, tile the bgd image 138 | bgdtile = load_image('boardchess.gif') 139 | background = pygame.Surface(SCREENRECT.size) 140 | for x in range(0, SCREENRECT.width, bgdtile.get_width()): 141 | background.blit(bgdtile, (x, 0)) 142 | screen.blit(background, (0, 0)) 143 | pygame.display.flip() 144 | 145 | cbd = Chessboard.Chessboard('000') 146 | cbd.init_board() 147 | 148 | chessmans = pygame.sprite.Group() 149 | framerate = pygame.time.Clock() 150 | 151 | load_sound("dong.mp3").play() 152 | 153 | creat_sprite_group(chessmans, cbd.chessmans_hash) 154 | current_chessman = None 155 | cbd.calc_chessmans_moving_list() 156 | while not cbd.is_end(): 157 | for event in pygame.event.get(): 158 | if event.type == pygame.QUIT: 159 | sys.exit() 160 | elif event.type == MOUSEBUTTONDOWN: 161 | pressed_array = pygame.mouse.get_pressed() 162 | for index in range(len(pressed_array)): 163 | if index == 0 and pressed_array[index]: 164 | mouse_x, mouse_y = pygame.mouse.get_pos() 165 | col_num, row_num = translate_hit_area(mouse_x, mouse_y) 166 | chessman_sprite = select_sprite_from_group( 167 | chessmans, col_num, row_num) 168 | if current_chessman is None and chessman_sprite != None: 169 | if chessman_sprite.chessman.is_red == cbd.is_red_turn: 170 | current_chessman = chessman_sprite 171 | chessman_sprite.is_selected = True 172 | elif current_chessman != None and chessman_sprite != None: 173 | if chessman_sprite.chessman.is_red == cbd.is_red_turn: 174 | current_chessman.is_selected = False 175 | current_chessman = chessman_sprite 176 | chessman_sprite.is_selected = True 177 | else: 178 | success = current_chessman.move( 179 | col_num, row_num) 180 | if success: 181 | current_chessman.Kill_sound.play() 182 | chessmans.remove(chessman_sprite) 183 | chessman_sprite.kill() 184 | current_chessman.is_selected = False 185 | current_chessman = None 186 | elif current_chessman != None and chessman_sprite is None: 187 | success = current_chessman.move(col_num, row_num) 188 | if success: 189 | current_chessman.is_selected = False 190 | current_chessman.move_sound.play() 191 | current_chessman = None 192 | 193 | framerate.tick(20) 194 | # clear/erase the last drawn sprites 195 | chessmans.clear(screen, background) 196 | 197 | # update all the sprites 198 | chessmans.update() 199 | chessmans.draw(screen) 200 | pygame.display.update() 201 | 202 | 203 | if __name__ == '__main__': 204 | main() 205 | -------------------------------------------------------------------------------- /Chess_Core/Chessboard.py: -------------------------------------------------------------------------------- 1 | # coding:utf-8 2 | from MyChess.Chess_Core import Chessman 3 | 4 | 5 | class Chessboard(object): 6 | 7 | def __init__(self, name): 8 | self.__name = name 9 | self.__is_red_turn = True 10 | self.__chessmans = [([None] * 10) for i in range(9)] 11 | self.__chessmans_hash = {} 12 | self.__history = {"red": {"chessman": None, "last_pos": None, "repeat": 0}, 13 | "black": {"chessman": None, "last_pos": None, "repeat": 0}} 14 | 15 | @property 16 | def is_red_turn(self): 17 | return self.__is_red_turn 18 | 19 | @property 20 | def name(self): 21 | return self.__name 22 | 23 | @name.setter 24 | def name(self, name): 25 | self.__name = name 26 | 27 | @property 28 | def chessmans(self): 29 | return self.__chessmans 30 | 31 | @property 32 | def chessmans_hash(self): 33 | return self.__chessmans_hash 34 | 35 | def init_board(self): 36 | red_rook_left = Chessman.Rook(" 车l红 ", "red_rook_left", True, self) 37 | red_rook_left.add_to_board(0, 0) 38 | red_rook_right = Chessman.Rook(" 车r红 ", "red_rook_right", True, self) 39 | red_rook_right.add_to_board(8, 0) 40 | black_rook_left = Chessman.Rook( 41 | " 车l黑 ", "black_rook_left", False, self) 42 | black_rook_left.add_to_board(0, 9) 43 | black_rook_right = Chessman.Rook( 44 | " 车r黑 ", "black_rook_right", False, self) 45 | black_rook_right.add_to_board(8, 9) 46 | red_knight_left = Chessman.Knight( 47 | " 马l红 ", "red_knight_left", True, self) 48 | red_knight_left.add_to_board(1, 0) 49 | red_knight_right = Chessman.Knight( 50 | " 马r红 ", "red_knight_right", True, self) 51 | red_knight_right.add_to_board(7, 0) 52 | black_knight_left = Chessman.Knight( 53 | " 马l黑 ", "black_knight_left", False, self) 54 | black_knight_left.add_to_board(1, 9) 55 | black_knight_right = Chessman.Knight( 56 | " 马r黑 ", "black_knight_right", False, self) 57 | black_knight_right.add_to_board(7, 9) 58 | red_cannon_left = Chessman.Cannon( 59 | " 炮l红 ", "red_cannon_left", True, self) 60 | red_cannon_left.add_to_board(1, 2) 61 | red_cannon_right = Chessman.Cannon( 62 | " 炮r红 ", "red_cannon_right", True, self) 63 | red_cannon_right.add_to_board(7, 2) 64 | black_cannon_left = Chessman.Cannon( 65 | " 炮l黑 ", "black_cannon_left", False, self) 66 | black_cannon_left.add_to_board(1, 7) 67 | black_cannon_right = Chessman.Cannon( 68 | " 炮r黑 ", "black_cannon_right", False, self) 69 | black_cannon_right.add_to_board(7, 7) 70 | red_elephant_left = Chessman.Elephant( 71 | " 相l红 ", "red_elephant_left", True, self) 72 | red_elephant_left.add_to_board(2, 0) 73 | red_elephant_right = Chessman.Elephant( 74 | " 相r红 ", "red_elephant_right", True, self) 75 | red_elephant_right.add_to_board(6, 0) 76 | black_elephant_left = Chessman.Elephant( 77 | " 象l黑 ", "black_elephant_left", False, self) 78 | black_elephant_left.add_to_board(2, 9) 79 | black_elephant_right = Chessman.Elephant( 80 | " 象r黑 ", "black_elephant_right", False, self) 81 | black_elephant_right.add_to_board(6, 9) 82 | red_mandarin_left = Chessman.Mandarin( 83 | " 仕l红 ", "red_mandarin_left", True, self) 84 | red_mandarin_left.add_to_board(3, 0) 85 | red_mandarin_right = Chessman.Mandarin( 86 | " 仕r红 ", "red_mandarin_right", True, self) 87 | red_mandarin_right.add_to_board(5, 0) 88 | black_mandarin_left = Chessman.Mandarin( 89 | " 仕l黑 ", "black_mandarin_left", False, self) 90 | black_mandarin_left.add_to_board(3, 9) 91 | black_mandarin_right = Chessman.Mandarin( 92 | " 仕r黑 ", "black_mandarin_right", False, self) 93 | black_mandarin_right.add_to_board(5, 9) 94 | red_king = Chessman.King(" 帅 红 ", "red_king", True, self) 95 | red_king.add_to_board(4, 0) 96 | black_king = Chessman.King(" 将 黑 ", "black_king", False, self) 97 | black_king.add_to_board(4, 9) 98 | red_pawn_1 = Chessman.Pawn(" 兵1红 ", "red_pawn_1", True, self) 99 | red_pawn_1.add_to_board(0, 3) 100 | red_pawn_2 = Chessman.Pawn(" 兵2红 ", "red_pawn_2", True, self) 101 | red_pawn_2.add_to_board(2, 3) 102 | red_pawn_3 = Chessman.Pawn(" 兵3红 ", "red_pawn_3", True, self) 103 | red_pawn_3.add_to_board(4, 3) 104 | red_pawn_4 = Chessman.Pawn(" 兵4红 ", "red_pawn_4", True, self) 105 | red_pawn_4.add_to_board(6, 3) 106 | red_pawn_5 = Chessman.Pawn(" 兵5红 ", "red_pawn_5", True, self) 107 | red_pawn_5.add_to_board(8, 3) 108 | black_pawn_1 = Chessman.Pawn(" 卒1黑 ", "black_pawn_1", False, self) 109 | black_pawn_1.add_to_board(0, 6) 110 | black_pawn_2 = Chessman.Pawn(" 卒2黑 ", "black_pawn_2", False, self) 111 | black_pawn_2.add_to_board(2, 6) 112 | black_pawn_3 = Chessman.Pawn(" 卒3黑 ", "black_pawn_3", False, self) 113 | black_pawn_3.add_to_board(4, 6) 114 | black_pawn_4 = Chessman.Pawn(" 卒4黑 ", "black_pawn_4", False, self) 115 | black_pawn_4.add_to_board(6, 6) 116 | black_pawn_5 = Chessman.Pawn(" 卒5黑 ", "black_pawn_5", False, self) 117 | black_pawn_5.add_to_board(8, 6) 118 | 119 | def add_chessman(self, chessman, col_num, row_num): 120 | self.chessmans[col_num][row_num] = chessman 121 | if chessman.name not in self.__chessmans_hash: 122 | self.__chessmans_hash[chessman.name] = chessman 123 | 124 | def remove_chessman_target(self, col_num, row_num): 125 | chessman_old = self.get_chessman(col_num, row_num) 126 | if chessman_old != None: 127 | self.__chessmans_hash.pop(chessman_old.name) 128 | 129 | def remove_chessman_source(self, col_num, row_num): 130 | self.chessmans[col_num][row_num] = None 131 | 132 | def calc_chessmans_moving_list(self): 133 | for chessman in self.__chessmans_hash.values(): 134 | if chessman.is_red == self.__is_red_turn: 135 | chessman.calc_moving_list() 136 | 137 | def clear_chessmans_moving_list(self): 138 | for chessman in self.__chessmans_hash.values(): 139 | chessman.clear_moving_list() 140 | 141 | def move_chessman(self, chessman, col_num, row_num): 142 | if chessman.is_red == self.__is_red_turn: 143 | # self.remove_chessman_source(chessman.col_num, chessman.row_num) 144 | self.remove_chessman_target(col_num, row_num) 145 | self.add_chessman(chessman, col_num, row_num) 146 | self.__is_red_turn = not self.__is_red_turn 147 | return True 148 | else: 149 | print("the wrong turn") 150 | return False 151 | 152 | def update_history(self, chessman, col_num, row_num): 153 | red_or_black = self.red_or_black(chessman) 154 | history_chessman = self.__history[red_or_black]["chessman"] 155 | history_pos = self.__history[red_or_black]["last_pos"] 156 | if history_chessman == chessman and history_pos != None and history_pos[0] == col_num and history_pos[1] == row_num: 157 | self.__history[red_or_black]["repeat"] += 1 158 | else: 159 | self.__history[red_or_black]["repeat"] = 0 160 | self.__history[red_or_black]["chessman"] = chessman 161 | self.__history[red_or_black]["last_pos"] = ( 162 | chessman.col_num, chessman.row_num) 163 | 164 | def red_or_black(self, chessman): 165 | if chessman.is_red: 166 | return "red" 167 | else: 168 | return "black" 169 | 170 | def is_end(self): 171 | return self.who_is_victor(6) 172 | 173 | def who_is_victor(self, repeat_num): 174 | whos_turn = "red" if self.__is_red_turn else "black" 175 | other_turn = "red" if not self.__is_red_turn else "black" 176 | chessman = self.get_chessman_by_name("{0}_king".format(whos_turn)) 177 | if chessman != None: 178 | if self.__history[other_turn]["repeat"] == repeat_num: 179 | print("{0} is victor".format(whos_turn)) 180 | return True 181 | else: 182 | return False 183 | else: 184 | print("{0} is victor".format(other_turn)) 185 | return True 186 | 187 | def get_chessman(self, col_num, row_num): 188 | return self.__chessmans[col_num][row_num] 189 | 190 | def get_chessman_by_name(self, name): 191 | if name in self.__chessmans_hash: 192 | return self.__chessmans_hash[name] 193 | 194 | def get_top_first_chessman(self, col_num, row_num): 195 | for i in range(row_num + 1, 10, 1): 196 | current = self.chessmans[col_num][i] 197 | if current != None: 198 | return current 199 | 200 | def get_bottom_first_chessman(self, col_num, row_num): 201 | for i in range(row_num - 1, -1, -1): 202 | current = self.chessmans[col_num][i] 203 | if current != None: 204 | return current 205 | 206 | def get_left_first_chessman(self, col_num, row_num): 207 | for i in range(col_num - 1, -1, -1): 208 | current = self.chessmans[i][row_num] 209 | if current != None: 210 | return current 211 | 212 | def get_right_first_chessman(self, col_num, row_num): 213 | for i in range(col_num + 1, 9, 1): 214 | current = self.chessmans[i][row_num] 215 | if current != None: 216 | return current 217 | 218 | def get_top_second_chessman(self, col_num, row_num): 219 | count = 0 220 | for i in range(row_num + 1, 10, 1): 221 | current = self.chessmans[col_num][i] 222 | if current != None: 223 | if count == 1: 224 | return current 225 | else: 226 | count += 1 227 | 228 | def get_bottom_second_chessman(self, col_num, row_num): 229 | count = 0 230 | for i in range(row_num - 1, -1, -1): 231 | current = self.chessmans[col_num][i] 232 | if current != None: 233 | if count == 1: 234 | return current 235 | else: 236 | count += 1 237 | 238 | def get_left_second_chessman(self, col_num, row_num): 239 | count = 0 240 | for i in range(col_num - 1, -1, -1): 241 | current = self.chessmans[i][row_num] 242 | if current != None: 243 | if count == 1: 244 | return current 245 | else: 246 | count += 1 247 | 248 | def get_right_second_chessman(self, col_num, row_num): 249 | count = 0 250 | for i in range(col_num + 1, 9, 1): 251 | current = self.chessmans[i][row_num] 252 | if current != None: 253 | if count == 1: 254 | return current 255 | else: 256 | count += 1 257 | 258 | def print_to_cl(self): 259 | screen = "\r\n" 260 | for i in range(9, -1, -1): 261 | for j in range(9): 262 | if self.__chessmans[j][i] != None: 263 | screen += self.__chessmans[j][i].name_cn 264 | else: 265 | screen += " . " 266 | screen += "\r\n" * 3 267 | print(screen) 268 | -------------------------------------------------------------------------------- /Chess_Core/Chessman.py: -------------------------------------------------------------------------------- 1 | # coding:utf-8 2 | 3 | from MyChess.Chess_Core import Point 4 | 5 | 6 | def num_between(max_num, min_num, current): 7 | return current >= min_num and current <= max_num 8 | 9 | 10 | def creat_points(list_points, list_vs, list_hs): 11 | for v in list_vs: 12 | for h in list_hs: 13 | list_points.append(Point.Point(v, h)) 14 | 15 | 16 | class Chessman(object): 17 | 18 | def __init__(self, name_cn, name, is_red, chessboard): 19 | self.__name = name 20 | self.__is_red = is_red 21 | self.__chessboard = chessboard 22 | self.__position = Point.Point(None, None) 23 | self.__moving_list = [] 24 | self.__top = 9 25 | self.__bottom = 0 26 | self.__left = 0 27 | self.__right = 8 28 | self.__is_alive = True 29 | self.__name_cn = name_cn 30 | 31 | @property 32 | def row_num(self): 33 | return self.__position.y 34 | 35 | @property 36 | def col_num(self): 37 | return self.__position.x 38 | 39 | @property 40 | def is_alive(self): 41 | return self.__is_alive 42 | 43 | @is_alive.setter 44 | def is_alive(self, is_alive): 45 | self.__is_alive = is_alive 46 | 47 | @property 48 | def chessboard(self): 49 | return self.__chessboard 50 | 51 | @property 52 | def is_red(self): 53 | return self.__is_red 54 | 55 | @property 56 | def name(self): 57 | return self.__name 58 | 59 | @property 60 | def name_cn(self): 61 | return self.__name_cn 62 | 63 | @property 64 | def position(self): 65 | return self.__position 66 | 67 | @property 68 | def moving_list(self): 69 | return self.__moving_list 70 | 71 | def clear_moving_list(self): 72 | self.__moving_list = [] 73 | 74 | def add_to_board(self, col_num, row_num): 75 | if self.border_check(col_num, row_num): 76 | self.__position.x = col_num 77 | self.__position.y = row_num 78 | self.__chessboard.add_chessman(self, col_num, row_num) 79 | else: 80 | print("the worng postion") 81 | 82 | def move(self, col_num, row_num): 83 | if self.in_moving_list(col_num, row_num): 84 | self.__chessboard.remove_chessman_source( 85 | self.__position.x, self.__position.y) 86 | self.__chessboard.update_history(self, col_num, row_num) 87 | self.__position.x = col_num 88 | self.__position.y = row_num 89 | return self.__chessboard.move_chessman(self, col_num, row_num) 90 | else: 91 | print("the worng target_position") 92 | return False 93 | 94 | def in_moving_list(self, col_num, row_num): 95 | for point in self.__moving_list: 96 | if point.x == col_num and point.y == row_num: 97 | return True 98 | return False 99 | 100 | def calc_moving_list(self): 101 | pass 102 | 103 | def border_check(self, col_num, row_num): 104 | return num_between(self.__top, self.__bottom, row_num) and num_between(self.__right, self.__left, col_num) 105 | 106 | def calc_moving_path(self, direction_chessman, direction_vertical_coordinate, current_vertical_coordinate, direction_parallel_coordinate, direction, border_vertical_coordinate, h_or_v, ignore_color=False): 107 | if direction_chessman != None: 108 | if direction_chessman.is_red == self.is_red or ignore_color: 109 | for i in range(direction_vertical_coordinate + direction, current_vertical_coordinate, direction): 110 | self.__moving_list.append( 111 | Point.Point(i, direction_parallel_coordinate) if h_or_v else Point.Point(direction_parallel_coordinate, i)) 112 | 113 | else: 114 | for i in range(direction_vertical_coordinate, current_vertical_coordinate, direction): 115 | self.__moving_list.append( 116 | Point.Point(i, direction_parallel_coordinate) if h_or_v else Point.Point(direction_parallel_coordinate, i)) 117 | else: 118 | for i in range(border_vertical_coordinate, current_vertical_coordinate, direction): 119 | self.__moving_list.append( 120 | Point.Point(i, direction_parallel_coordinate) if h_or_v else Point.Point(direction_parallel_coordinate, i)) 121 | 122 | def add_from_probable_points(self, probable_moving_points, current_color): 123 | for point in probable_moving_points: 124 | if self.border_check(point.x, point.y): 125 | chessman = self.chessboard.get_chessman( 126 | point.x, point.y) 127 | if chessman is None or chessman.is_red != current_color: 128 | self.moving_list.append(point) 129 | 130 | 131 | class Rook(Chessman): 132 | 133 | def __init__(self, name_cn, name, is_red, chessboard): 134 | super(Rook, self).__init__(name_cn, name, is_red, chessboard) 135 | self._Chessman__top = 9 136 | self._Chessman__bottom = 0 137 | self._Chessman__left = 0 138 | self._Chessman__right = 8 139 | 140 | def calc_moving_list(self): 141 | current_v_c = super(Rook, self).position.x 142 | current_h_c = super(Rook, self).position.y 143 | left = super(Rook, self).chessboard.get_left_first_chessman( 144 | current_v_c, current_h_c) 145 | right = super(Rook, self).chessboard.get_right_first_chessman( 146 | current_v_c, current_h_c) 147 | top = super(Rook, self).chessboard.get_top_first_chessman( 148 | current_v_c, current_h_c) 149 | bottom = super(Rook, self).chessboard.get_bottom_first_chessman( 150 | current_v_c, current_h_c) 151 | 152 | super(Rook, self).calc_moving_path(left, (left.position.x if left != None else None), 153 | current_v_c, current_h_c, 1, 0, True) 154 | super(Rook, self).calc_moving_path(right, (right.position.x if right != None else None), 155 | current_v_c, current_h_c, -1, 8, True) 156 | super(Rook, self).calc_moving_path(top, (top.position.y if top != None else None), 157 | current_h_c, current_v_c, -1, 9, False) 158 | super(Rook, self).calc_moving_path(bottom, (bottom.position.y if bottom != None else None), 159 | current_h_c, current_v_c, 1, 0, False) 160 | 161 | 162 | class Knight(Chessman): 163 | 164 | def __init__(self, name_cn, name, is_red, chessboard): 165 | super(Knight, self).__init__(name_cn, name, is_red, chessboard) 166 | self._Chessman__top = 9 167 | self._Chessman__bottom = 0 168 | self._Chessman__left = 0 169 | self._Chessman__right = 8 170 | 171 | def calc_moving_list(self): 172 | current_v_c = super(Knight, self).position.x 173 | current_h_c = super(Knight, self).position.y 174 | probable_obstacle_points = [] 175 | probable_moving_points = [] 176 | vs1 = (current_v_c + 1, current_v_c - 1) 177 | hs1 = (current_h_c,) 178 | vs2 = (current_v_c,) 179 | hs2 = (current_h_c + 1, current_h_c - 1) 180 | creat_points(probable_obstacle_points, vs1, hs1) 181 | creat_points(probable_obstacle_points, vs2, hs2) 182 | current_color = super(Knight, self).is_red 183 | for point in probable_obstacle_points: 184 | if super(Knight, self).border_check(point.x, point.y): 185 | chessman = super(Knight, self).chessboard.get_chessman( 186 | point.x, point.y) 187 | if chessman is None: 188 | if point.x == current_v_c: 189 | probable_moving_points.append( 190 | Point.Point(point.x + 1, 2 * point.y - current_h_c)) 191 | probable_moving_points.append( 192 | Point.Point(point.x - 1, 2 * point.y - current_h_c)) 193 | else: 194 | probable_moving_points.append( 195 | Point.Point(2 * point.x - current_v_c, point.y + 1)) 196 | probable_moving_points.append( 197 | Point.Point(2 * point.x - current_v_c, point.y - 1)) 198 | super(Knight, self).add_from_probable_points( 199 | probable_moving_points, current_color) 200 | 201 | 202 | class Cannon(Chessman): 203 | 204 | def __init__(self, name_cn, name, is_red, chessboard): 205 | super(Cannon, self).__init__(name_cn, name, is_red, chessboard) 206 | self._Chessman__top = 9 207 | self._Chessman__bottom = 0 208 | self._Chessman__left = 0 209 | self._Chessman__right = 8 210 | 211 | def calc_moving_list(self): 212 | current_v_c = super(Cannon, self).position.x 213 | current_h_c = super(Cannon, self).position.y 214 | left = super(Cannon, self).chessboard.get_left_first_chessman( 215 | current_v_c, current_h_c) 216 | right = super(Cannon, self).chessboard.get_right_first_chessman( 217 | current_v_c, current_h_c) 218 | top = super(Cannon, self).chessboard.get_top_first_chessman( 219 | current_v_c, current_h_c) 220 | bottom = super(Cannon, self).chessboard.get_bottom_first_chessman( 221 | current_v_c, current_h_c) 222 | tar_left = super(Cannon, self).chessboard.get_left_second_chessman( 223 | current_v_c, current_h_c) 224 | tar_right = super(Cannon, self).chessboard.get_right_second_chessman( 225 | current_v_c, current_h_c) 226 | tar_top = super(Cannon, self).chessboard.get_top_second_chessman( 227 | current_v_c, current_h_c) 228 | tar_bottom = super(Cannon, self).chessboard.get_bottom_second_chessman( 229 | current_v_c, current_h_c) 230 | super(Cannon, self).calc_moving_path(left, (left.position.x if left != None else None), 231 | current_v_c, current_h_c, 1, 0, True, True) 232 | super(Cannon, self).calc_moving_path(right, (right.position.x if right != None else None), 233 | current_v_c, current_h_c, -1, 8, True, True) 234 | super(Cannon, self).calc_moving_path(top, (top.position.y if top != None else None), 235 | current_h_c, current_v_c, -1, 9, False, True) 236 | super(Cannon, self).calc_moving_path(bottom, (bottom.position.y if bottom != None else None), 237 | current_h_c, current_v_c, 1, 0, False, True) 238 | current_color = super(Cannon, self).is_red 239 | if tar_left != None and tar_left.is_red != current_color: 240 | super(Cannon, self).moving_list.append( 241 | Point.Point(tar_left.position.x, tar_left.position.y)) 242 | if tar_right != None and tar_right.is_red != current_color: 243 | super(Cannon, self).moving_list.append( 244 | Point.Point(tar_right.position.x, tar_right.position.y)) 245 | if tar_top != None and tar_top.is_red != current_color: 246 | super(Cannon, self).moving_list.append( 247 | Point.Point(tar_top.position.x, tar_top.position.y)) 248 | if tar_bottom != None and tar_bottom.is_red != current_color: 249 | super(Cannon, self).moving_list.append( 250 | Point.Point(tar_bottom.position.x, tar_bottom.position.y)) 251 | 252 | 253 | class Mandarin(Chessman): 254 | 255 | def __init__(self, name_cn, name, is_red, chessboard): 256 | super(Mandarin, self).__init__(name_cn, name, is_red, chessboard) 257 | if self.is_red: 258 | self._Chessman__top = 2 259 | self._Chessman__bottom = 0 260 | self._Chessman__left = 3 261 | self._Chessman__right = 5 262 | else: 263 | self._Chessman__top = 9 264 | self._Chessman__bottom = 7 265 | self._Chessman__left = 3 266 | self._Chessman__right = 5 267 | 268 | def calc_moving_list(self): 269 | current_v_c = super(Mandarin, self).position.x 270 | current_h_c = super(Mandarin, self).position.y 271 | probable_moving_points = [] 272 | vs1 = (current_v_c + 1, current_v_c - 1) 273 | hs1 = (current_h_c + 1, current_h_c - 1) 274 | creat_points(probable_moving_points, vs1, hs1) 275 | current_color = super(Mandarin, self).is_red 276 | 277 | super(Mandarin, self).add_from_probable_points( 278 | probable_moving_points, current_color) 279 | 280 | 281 | class Elephant(Chessman): 282 | 283 | def __init__(self, name_cn, name, is_red, chessboard): 284 | super(Elephant, self).__init__(name_cn, name, is_red, chessboard) 285 | if self.is_red: 286 | self._Chessman__top = 4 287 | self._Chessman__bottom = 0 288 | self._Chessman__left = 0 289 | self._Chessman__right = 8 290 | else: 291 | self._Chessman__top = 9 292 | self._Chessman__bottom = 5 293 | self._Chessman__left = 0 294 | self._Chessman__right = 8 295 | 296 | def calc_moving_list(self): 297 | current_v_c = super(Elephant, self).position.x 298 | current_h_c = super(Elephant, self).position.y 299 | probable_obstacle_points = [] 300 | probable_moving_points = [] 301 | vs1 = (current_v_c + 1, current_v_c - 1) 302 | hs1 = (current_h_c + 1, current_h_c - 1) 303 | creat_points(probable_obstacle_points, vs1, hs1) 304 | current_color = super(Elephant, self).is_red 305 | for point in probable_obstacle_points: 306 | if super(Elephant, self).border_check(point.x, point.y): 307 | chessman = super(Elephant, self).chessboard.get_chessman( 308 | point.x, point.y) 309 | if chessman is None: 310 | probable_moving_points.append( 311 | Point.Point(2 * point.x - current_v_c, 2 * point.y - current_h_c)) 312 | super(Elephant, self).add_from_probable_points( 313 | probable_moving_points, current_color) 314 | 315 | 316 | class Pawn(Chessman): 317 | 318 | def __init__(self, name_cn, name, is_red, chessboard): 319 | super(Pawn, self).__init__(name_cn, name, is_red, chessboard) 320 | if self.is_red: 321 | self._Chessman__top = 9 322 | self._Chessman__bottom = 3 323 | self._Chessman__left = 0 324 | self._Chessman__right = 8 325 | self.__direction = 1 326 | self.__river = 5 327 | else: 328 | self._Chessman__top = 6 329 | self._Chessman__bottom = 0 330 | self._Chessman__left = 0 331 | self._Chessman__right = 8 332 | self.__direction = -1 333 | self.__river = 4 334 | 335 | def calc_moving_list(self): 336 | current_v_c = super(Pawn, self).position.x 337 | current_h_c = super(Pawn, self).position.y 338 | probable_moving_points = [] 339 | current_color = super(Pawn, self).is_red 340 | probable_moving_points.append( 341 | Point.Point(current_v_c, current_h_c + self.__direction)) 342 | if current_h_c * self.__direction >= self.__river * self.__direction: 343 | probable_moving_points.append( 344 | Point.Point(current_v_c + 1, current_h_c)) 345 | probable_moving_points.append( 346 | Point.Point(current_v_c - 1, current_h_c)) 347 | super(Pawn, self).add_from_probable_points( 348 | probable_moving_points, current_color) 349 | 350 | 351 | class King(Chessman): 352 | 353 | def __init__(self, name_cn, name, is_red, chessboard): 354 | super(King, self).__init__(name_cn, name, is_red, chessboard) 355 | if self.is_red: 356 | self._Chessman__top = 2 357 | self._Chessman__bottom = 0 358 | self._Chessman__left = 3 359 | self._Chessman__right = 5 360 | else: 361 | self._Chessman__top = 9 362 | self._Chessman__bottom = 7 363 | self._Chessman__left = 3 364 | self._Chessman__right = 5 365 | 366 | def calc_moving_list(self): 367 | current_v_c = super(King, self).position.x 368 | current_h_c = super(King, self).position.y 369 | probable_moving_points = [] 370 | vs1 = (current_v_c + 1, current_v_c - 1) 371 | hs1 = (current_h_c,) 372 | vs2 = (current_v_c,) 373 | hs2 = (current_h_c + 1, current_h_c - 1) 374 | creat_points(probable_moving_points, vs1, hs1) 375 | creat_points(probable_moving_points, vs2, hs2) 376 | current_color = super(King, self).is_red 377 | super(King, self).add_from_probable_points( 378 | probable_moving_points, current_color) 379 | --------------------------------------------------------------------------------