diff --git a/SafariChess_backend.py b/SafariChess_backend.py index 0cdb262..b2484e6 100644 --- a/SafariChess_backend.py +++ b/SafariChess_backend.py @@ -1,7 +1,6 @@ - class GameState: def __init__(self): ''' @@ -20,6 +19,7 @@ class GameState: ['00','b3','00','00','00','00','00','r2','00'], ['00','00','b1','00','00','00','r7','00','00'], ] + #用数字1-7代替棋子的强弱关系 self.moveFunctions = { "1": self.getMseMoves, "2": self.getEagMoves, @@ -33,26 +33,43 @@ class GameState: self.red_trap_loc=[(3,7),(2,8),(4,8)] self.blue_home=(3,0) self.red_home=(3,8) - self.red_to_move=True + + #蓝方(左)先行 + self.red_to_move=False self.conquered=False self.MASSACRE=False + self.color = 'r' if self.red_to_move else 'b' + + # 判断特殊位置 + def inHome(self,row,col,color): + if color=="b": + if (row,col)==self.blue_home: + return True + else: + if (row,col)==self.red_home: + return True + return False + def inWater(self,row,col): if row in [1,2,4,5]: if col in [3,4,5]: return True return False + def inTrap(self,row,col,color): - if color=="blue": + if color=="b": if (row,col) in self.blue_trap_loc: return True else: if (row,col) in self.red_trap_loc: return True return False + def Eliminate(self,row,col,nxt_row,nxt_col): return self.board[nxt_row][nxt_col] == '00' or self.board[nxt_row][nxt_col][0] != self.board[row][col][0] and eval(self.board[row][col][1]) >= eval(self.board[nxt_row][nxt_col][1]) + # 定义移动方式 def getStdMoves(self,row,col,moves):#输入当前的位置,将可行路径输出给moves directions = [(1,0),(0,1),(-1,0),(0,-1)] @@ -62,19 +79,106 @@ class GameState: new_col = col + direction[1]*1 if 0<=new_row<=8 and 0<=new_col<=6 : nxt_piece = self.board[new_row][new_col] - if nxt_piece == '00' and not self.inWater(new_row,new_col):#如果下一个位置是空的,则可以移动 + if nxt_piece == '00' and not self.inWater(new_row,new_col) and not self.inHome(new_row,new_col,self.color):#如果下一个位置是空的,则可以移动 moves.append((new_row,new_col)) - elif nxt_piece[0]==enemy_color and not self.inWater(new_row,new_col) and self.Eliminate(row,col,new_row,new_col):#如果是敌方棋子,且不在水里,且可以消除,则添加到可行路径中 + elif nxt_piece[0]==enemy_color and not self.inWater(new_row,new_col) and not self.inHome(new_row,new_col,self.color) and self.Eliminate(row,col,new_row,new_col):#如果是敌方棋子,且不在水里,且可以消除,则添加到可行路径中 moves.append((new_row,new_col)) return moves + def getMseMoves(self,row,col,moves): - pass + directions = [(1,0),(0,1),(-1,0),(0,-1)] + enemy_color = 'b' if self.red_to_move else 'r' + for direction in directions: + new_row = row + direction[0]*1 + new_col = col + direction[0]*1 + if 0<=new_row<=8 and 0<=new_col<=6 : + nxt_piece = self.board[new_row][new_col] + if nxt_piece == '00' and not self.inHome(new_row,new_col,self.color): + moves.append((new_row,new_col)) + elif nxt_piece[0]==enemy_color and self.Eliminate(row,col,new_row,new_col): + moves.append((new_row,new_col)) + return moves + def getEagMoves(self,row,col,moves): - pass + #可能存在一些问题,主要是移动方式的判断以及在陷阱的判断 + enemy_color = 'b' if self.red_to_move else 'r' + moveNum_row = -6 + moveNum_col = -8 + new_row = row + new_col = col + + while(moveNum_row <= 6 ): + new_row = row + moveNum_row + if 0<=new_row<=6: + nxt_piece = self.board[new_row][col] + if nxt_piece == '00': + moves.append((new_row,col)) + elif nxt_piece[0]==enemy_color and not self.inWater(new_row,col) and not self.inHome(row,new_col,self.color) and self.Eliminate(row,col,new_row,new_col): + moves.append((new_row,col)) + else : + break + moveNum_row += 1 + + while(moveNum_row <= 8): + new_col = col + moveNum_col + if 0 <= new_col <= 8: + nxt_piece = self.board[row][new_col] + if nxt_piece == '00' and not self.inWater(row,new_col) and not self.inHome(row,new_col,self.color): + moves.append((row,new_col)) + elif nxt_piece[0]==enemy_color and self.Eliminate(row,col,new_row,new_col): + moves.append((row,new_col)) + else : + break + moveNum_col += 1 + return moves + + + def getFoxMoves(self,row,col,moves): - pass + directions = [(1,0),(0,1),(-1,0),(0,-1),(1,1),(1,-1),(-1,1),(-1,-1)] + enemy_color = 'b' if self.red_to_move else 'r' + for direction in directions: + new_row = row + direction[0]*1 + new_col = col + direction[1]*1 + if 0<=new_row<=8 and 0<=new_col<=6 : + nxt_piece = self.board[new_row][new_col] + if nxt_piece == '00' and not self.inWater(new_row,new_col) and not self.inHome(new_row,new_col,self.color):#如果下一个位置是空的,则可以移动 + moves.append((new_row,new_col)) + elif nxt_piece[0]==enemy_color and not self.inWater(new_row,new_col) and not self.inHome(new_row,new_col,self.color) and self.Eliminate(row,col,new_row,new_col):#如果是敌方棋子,且不在水里,且可以消除,则添加到可行路径中 + moves.append((new_row,new_col)) + return moves + def getLionMoves(self,row,col,moves): - pass + directions = ((-1, 0), (0, -1), (1, 0), (0, 1)) + enemy_color = "b" if self.red_to_move else "r" + for direction in directions: + for i in range(1, 2): + end_row = row + direction[0] * 1 + end_col = col + direction[1] * 1 + if 0 <= end_row <= 8 and 0 <= end_col <= 6: # check for possible moves only in boundaries of the board + end_piece = self.board[end_row][end_col] + + if end_piece == "--" and not self.inWater(end_row,end_col) and self.moveNotOwnDen(end_row,end_col,enemy_color): # empty space is valid and Not in Water + moves.append(Move((row, col), (end_row, end_col), self.board)) + + elif end_piece == "--" and self.inWater(end_row,end_col): + jump_row = end_row - row #Vertical jump + jump_col = end_col - col #Horizontal jump + + if jump_row != 0 and self.jumpConditions(row,col,end_row,end_col,jump_row,jump_col,enemy_color): + moves.append(Move((row, col), (end_row+(3*jump_row), end_col), self.board)) + + elif jump_col != 0 and self.jumpConditions(row,col,end_row,end_col,jump_row,jump_col,enemy_color): + moves.append(Move((row, col), (end_row, end_col+(2*jump_col)), self.board)) + + elif end_piece[0] == enemy_color and not self.inWater(end_row,end_col) and self.canAttack(row, col, end_row, end_col): # capture enemy piece + moves.append(Move((row, col), (end_row, end_col), self.board)) + break + else: # friendly piece + break + else: # off board + break + # 判断是否胜利 def conquer(self): if self.board[3][0][0] == 'r' or self.board[3][8][0] == 'b': diff --git a/SarafiChess_Gamev1.0.py b/SarafiChess_Gamev1.0.py index 9476ed2..a252234 100644 --- a/SarafiChess_Gamev1.0.py +++ b/SarafiChess_Gamev1.0.py @@ -41,7 +41,7 @@ def loadImages():#加载图片,a,b双方分别有象狮豹狼狐鹰鼠七个角 #screen = pg.display.set_mode((BOARD_WIDTH + MOVE_LOG_PANEL_WIDTH, BOARD_HEIGHT))#设置窗口大小 #screen.fill(pg.Color("white")) -def drawBoardPieces(screen):#绘制棋盘:格子,河流,陷阱,巢穴 棋子:14个棋子,初始位置固定 +def drawBoard(screen):#绘制棋盘:格子,河流,陷阱,巢穴 #global colors bias_top = 200 bias_left = 10 @@ -66,10 +66,13 @@ def drawBoardPieces(screen):#绘制棋盘:格子,河流,陷阱,巢穴 棋 screen.blit(IMAGES["home1"], pg.Rect((bias_left+6, (3 * SIZE) +bias_top +6, SIZE ,SIZE ))) screen.blit(IMAGES["home2"], pg.Rect( 8*SIZE+bias_left+6, (3 * SIZE) +bias_top +6, SIZE ,SIZE )) - #画棋子的位置 +def drawPieces(screen,board): + #画棋子的位置 for row in range(DIMENSION_ROWS): for column in range(DIMENSION_COLUMNS): - piece = + piece = board[row][column] + if piece != "--": + screen.blit(IMAGES[piece], pg.Rect((column * SIZE) + 10, (row * SIZE) + 200, SIZE, SIZE)) @@ -124,7 +127,8 @@ def main(): clock = pg.time.Clock() screen.fill(pg.Color("white")) loadImages() - drawBoardPieces(screen) + drawBoard(screen) + pg.display.update() startGamePage() while True: