diff --git a/SafariChess_backend.py b/SafariChess_backend.py index b2484e6..3418945 100644 --- a/SafariChess_backend.py +++ b/SafariChess_backend.py @@ -1,6 +1,7 @@ + class GameState: def __init__(self): ''' @@ -67,10 +68,25 @@ class GameState: return True return False - def Eliminate(self,row,col,nxt_row,nxt_col): + 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]) - # 定义移动方式 + # 定义移动方式 + #---------------------------------- + # 以下为移动方式的相关规定: + # 1.下一步的可行位置以二维数组的格式进行记录。 + # 2.getValidMoves返回值为下一步可行位置构成的集合。 + #---------------------------------- + def getAllMoves(self): + moves=[] + for row in range(len(self.board)): + for col in range(len(self.board[row])): + player = self.board[row][col][0] + if (player == 'r' and self.red_to_move or player == 'b' and not self.red_to_move): + self.moveFunctions[self.board[row][col][1]](row,col,moves) + return moves + + def getStdMoves(self,row,col,moves):#输入当前的位置,将可行路径输出给moves directions = [(1,0),(0,1),(-1,0),(0,-1)] enemy_color = 'b' if self.red_to_move else 'r' @@ -80,9 +96,9 @@ class GameState: 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)) + moves.append(Move((row,col),(new_row,new_col),self.board)) 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)) + moves.append(Move((row,col),(new_row,new_col),self.board)) return moves def getMseMoves(self,row,col,moves): @@ -94,9 +110,9 @@ class GameState: 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)) + moves.append(Move((row,col),(new_row,new_col),self.board)) elif nxt_piece[0]==enemy_color and self.Eliminate(row,col,new_row,new_col): - moves.append((new_row,new_col)) + moves.append(Move((row,col),(new_row,new_col),self.board)) return moves def getEagMoves(self,row,col,moves): @@ -112,9 +128,9 @@ class GameState: if 0<=new_row<=6: nxt_piece = self.board[new_row][col] if nxt_piece == '00': - moves.append((new_row,col)) + moves.append(Move((row,col),(new_row,new_col),self.board)) 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)) + moves.append(Move((row,col),(new_row,new_col),self.board)) else : break moveNum_row += 1 @@ -124,9 +140,9 @@ class GameState: 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)) + moves.append(Move((row,col),(new_row,new_col),self.board)) elif nxt_piece[0]==enemy_color and self.Eliminate(row,col,new_row,new_col): - moves.append((row,new_col)) + moves.append(Move((row,col),(new_row,new_col),self.board)) else : break moveNum_col += 1 @@ -143,9 +159,9 @@ class GameState: 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)) + moves.append(Move((row,col),(new_row,new_col),self.board)) 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)) + moves.append(Move((row,col),(new_row,new_col),self.board)) return moves def getLionMoves(self,row,col,moves): @@ -159,20 +175,20 @@ class GameState: 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)) + 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)) + 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)) + 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)) + moves.append(Move((row,col),(end_row, end_col),self.board)) break else: # friendly piece break @@ -184,4 +200,17 @@ class GameState: if self.board[3][0][0] == 'r' or self.board[3][8][0] == 'b': self.conquered = True return self.conquered - \ No newline at end of file + #移动操作 + def makeMove(self,move):#cur是当前位置,nxt是下一个位置,参数传入为元组 + self.board[move.end_row][move.end_col] = self.board[move.start_row][move.start_col] + self.board[move.start_row][move.start_col] = '00' + +class Move: + def __init__(self,start_loc,end_loc,board): + self.start_row = start_loc[0] + self.start_col = start_loc[1] + self.end_row = end_loc[0] + self.end_col = end_loc[1] + self.nxt_piece = board[self.end_row][self.end_col] + self.cur_piece = board[self.start_row][self.start_col] + self.attack = self.nxt_piece != '00' \ No newline at end of file diff --git a/SarafiChess_Gamev1.0.py b/SarafiChess_Gamev1.0.py index e5b22f7..60bcd95 100644 --- a/SarafiChess_Gamev1.0.py +++ b/SarafiChess_Gamev1.0.py @@ -191,7 +191,12 @@ def main(): screen.fill(pg.Color("white")) loadImages() drawBoard(screen) - + game_state=backend.GameState() + valid_moves=game_state.getAllMoves() + running = True + game_over = False + square_selected = ()#刚开始没有选择任何一个棋子 + click_queue = []#点击队列,记录第一次点击和第二次点击位置,方便移动棋子 pg.display.update() startGamePage() while True: @@ -203,7 +208,26 @@ def main(): elif e.type == pg.MOUSEBUTTONDOWN: #鼠标点击事件:用于选择棋子,移动棋子 - pass + if not game_over: + mouse_loc = pg.mouse.get_pos() + row = int((mouse_loc[1] - bias_top) / SIZE) + col = int((mouse_loc[0] - bias_left) / SIZE) + if square_selected == (row,col) or col >=DIMENSION_COLUMNS or row >= DIMENSION_ROWS: + square_selected = () + click_queue = [] + else: + square_selected = (row,col) + click_queue.append(square_selected) + if(len(click_queue) == 2): + cur_piece_loc = click_queue[0] + nxt_piece_loc = click_queue[1] + move = backend.Move(cur_piece_loc,nxt_piece_loc,game_state.board) + if move in valid_moves: + game_state.makeMove(move) + square_selected = () + click_queue = [] + else: + click_queue = [square_selected] elif e.type == pg.KEYDOWN: #设置某些按键用于悔棋,重新开始游戏,机器提示,退出游戏等功能 diff --git a/test.py b/test.py new file mode 100644 index 0000000..cc914df --- /dev/null +++ b/test.py @@ -0,0 +1,7 @@ +#导入所需的模块 +import sys +import pygame + + +if __name__ == '__main__': + \ No newline at end of file