basic function realized

tzy_branch
tzzzzzzzx 3 years ago
parent 9937a9ba1b
commit 34ea6c821b

@ -202,7 +202,7 @@ def main():
click_queue = []#点击队列,记录第一次点击和第二次点击位置,方便移动棋子 click_queue = []#点击队列,记录第一次点击和第二次点击位置,方便移动棋子
pg.display.update() pg.display.update()
startGamePage() startGamePage()
while True: while running:
for e in pg.event.get(): for e in pg.event.get():
#接下来处理游戏中的事件 #接下来处理游戏中的事件
if e.type == pg.QUIT: if e.type == pg.QUIT:
@ -240,7 +240,9 @@ def main():
if mademove: if mademove:
valid_moves = game_state.getAllMoves() valid_moves = game_state.getAllMoves()
ShowGameState(screen,game_state,valid_moves,square_selected) ShowGameState(screen,game_state,valid_moves,square_selected)
if game_state.conquer():
showGameOverText(screen,"player "+game_state.win_person+" wins")
game_over = True
clock.tick(60) clock.tick(60)
pg.display.flip() pg.display.flip()

@ -1,6 +1,3 @@
class GameState: class GameState:
def __init__(self): def __init__(self):
''' '''
@ -33,13 +30,16 @@ class GameState:
self.red_trap_loc=[(3,7),(2,8),(4,8)] self.red_trap_loc=[(3,7),(2,8),(4,8)]
self.blue_home=(3,0) self.blue_home=(3,0)
self.red_home=(3,8) self.red_home=(3,8)
self.blue_pieces=[7,6,5,4,3,2,1]
self.red_pieces=[7,6,5,4,3,2,1]
#蓝方(左)先行 #蓝方(左)先行
self.red_to_move=False self.red_to_move=False
self.conquered=False self.conquered=False
self.win_person=''
self.MASSACRE=False self.MASSACRE=False
self.color = 'r' if self.red_to_move else 'b' def color(self):
return 'r' if self.red_to_move else 'b'
# 判断特殊位置 # 判断特殊位置
@ -58,7 +58,7 @@ class GameState:
return True return True
return False return False
def inTrap(self,row,col,color): def inTrap(self,row,col,color): #是否在color方的陷阱中
if color=="b": if color=="b":
if (row,col) in self.blue_trap_loc: if (row,col) in self.blue_trap_loc:
return True return True
@ -68,7 +68,10 @@ class GameState:
return False 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]) and not (eval(self.board[row][col][1]) == 7 and eval(self.board[nxt_row][nxt_col][1]) == 1 ) or eval(self.board[row][col][1])==1 and eval(self.board[nxt_row][nxt_col][1])==7) next_blank = self.board[nxt_row][nxt_col] == '00'
attack = 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]) and not (eval(self.board[row][col][1]) == 7 and eval(self.board[nxt_row][nxt_col][1]) == 1 ) or eval(self.board[row][col][1])==1 and eval(self.board[nxt_row][nxt_col][1])==7)
other_in_trap = self.board[row][col][0] != self.board[nxt_row][nxt_col][0] and (self.board[nxt_row][nxt_col][0] == 'r' and self.inTrap(nxt_row,nxt_col,'b') or self.board[nxt_row][nxt_col][0] == 'b' and self.inTrap(nxt_row,nxt_col,'r'))
return next_blank or attack or other_in_trap
# 定义移动方式 # 定义移动方式
#---------------------------------- #----------------------------------
@ -94,9 +97,9 @@ class GameState:
new_col = col + direction[1]*1 new_col = col + direction[1]*1
if 0<=new_row<=6 and 0<=new_col<=8 : if 0<=new_row<=6 and 0<=new_col<=8 :
nxt_piece = self.board[new_row][new_col] 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):#如果下一个位置是空的,则可以移动 if nxt_piece == '00' and not self.inWater(new_row,new_col) and not self.inHome(new_row,new_col,self.color()):#如果下一个位置是空的,则可以移动
moves.append(Move((row,col),(new_row,new_col),self.board)) 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):#如果是敌方棋子,且不在水里,且可以消除,则添加到可行路径中 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(Move((row,col),(new_row,new_col),self.board)) moves.append(Move((row,col),(new_row,new_col),self.board))
return moves return moves
@ -108,6 +111,7 @@ class GameState:
new_col = col + direction[1]*1 new_col = col + direction[1]*1
if 0<=new_row<=6 and 0<=new_col<=8 : if 0<=new_row<=6 and 0<=new_col<=8 :
nxt_piece = self.board[new_row][new_col] nxt_piece = self.board[new_row][new_col]
if self.inHome(new_row,new_col,self.color()): continue
if nxt_piece == '00': if nxt_piece == '00':
moves.append(Move((row,col),(new_row,new_col),self.board)) 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) and not self.inWater(row,col): elif nxt_piece[0]==enemy_color and self.Eliminate(row,col,new_row,new_col) and not self.inWater(row,col):
@ -117,34 +121,45 @@ class GameState:
def getEagMoves(self,row,col,moves): def getEagMoves(self,row,col,moves):
#可能存在一些问题,主要是移动方式的判断以及在陷阱的判断 #可能存在一些问题,主要是移动方式的判断以及在陷阱的判断
enemy_color = 'b' if self.red_to_move else 'r' enemy_color = 'b' if self.red_to_move else 'r'
moveNum_row = -6
moveNum_col = -8
new_row = row new_row = row
new_col = col new_col = col
while(moveNum_row <= 6 ): #U&D direction
new_row = row + moveNum_row while new_row >= 0:
if 0<=new_row<=6:
nxt_piece = self.board[new_row][col] nxt_piece = self.board[new_row][col]
if nxt_piece == '00': if nxt_piece == '00' and not self.inTrap(new_row,col,enemy_color):
moves.append(Move((row,col),(new_row,new_col),self.board)) if not self.inHome(new_row,col,self.color()) and not self.inWater(new_row,col): moves.append(Move((row,col),(new_row,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): else:
moves.append(Move((row,col),(new_row,new_col),self.board)) if self.Eliminate(row,col,new_row,col): moves.append(Move((row,col),(new_row,col),self.board))
else : if new_row != row: break
break new_row -= 1
moveNum_row += 1 new_row = row
while new_row <= 6:
while(moveNum_row <= 8): nxt_piece = self.board[new_row][col]
new_col = col + moveNum_col if nxt_piece == '00' and not self.inTrap(new_row,new_col,enemy_color):
if 0 <= new_col <= 8: if not self.inHome(new_row,col,self.color()) and not self.inWater(new_row,col): moves.append(Move((row,col),(new_row,col),self.board))
else:
if self.Eliminate(row,col,new_row,col): moves.append(Move((row,col),(new_row,col),self.board))
if new_row != row: break
new_row += 1
#L&R Direction
while new_col >= 0:
nxt_piece = self.board[row][new_col] 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): if nxt_piece == '00' and not self.inTrap(row,new_col,enemy_color):
moves.append(Move((row,col),(new_row,new_col),self.board)) if not self.inHome(row,new_col,self.color()) and not self.inWater(row,new_col): moves.append(Move((row,col),(row,new_col),self.board))
elif nxt_piece[0]==enemy_color and self.Eliminate(row,col,new_row,new_col): else:
moves.append(Move((row,col),(new_row,new_col),self.board)) if self.Eliminate(row,col,row,new_col): moves.append(Move((row,col),(row,new_col),self.board))
else : if new_col != col: break
break new_col -= 1
moveNum_col += 1 new_col = col
while new_col <= 8:
nxt_piece = self.board[row][new_col]
if nxt_piece == '00' and not self.inTrap(row,new_col,enemy_color):
if not self.inHome(row,new_col,self.color()) and not self.inWater(row,new_col): moves.append(Move((row,col),(row,new_col),self.board))
else:
if self.Eliminate(row,col,row,new_col): moves.append(Move((row,col),(row,new_col),self.board))
if new_col != col: break
new_col += 1
return moves return moves
@ -157,51 +172,79 @@ class GameState:
new_col = col + direction[1]*1 new_col = col + direction[1]*1
if 0<=new_row<=6 and 0<=new_col<=8 : if 0<=new_row<=6 and 0<=new_col<=8 :
nxt_piece = self.board[new_row][new_col] 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):#如果下一个位置是空的,则可以移动 if nxt_piece == '00' and not self.inWater(new_row,new_col) and not self.inHome(new_row,new_col,self.color()):#如果下一个位置是空的,则可以移动
moves.append(Move((row,col),(new_row,new_col),self.board)) 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):#如果是敌方棋子,且不在水里,且可以消除,则添加到可行路径中 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(Move((row,col),(new_row,new_col),self.board)) moves.append(Move((row,col),(new_row,new_col),self.board))
return moves return moves
def getLionMoves(self,row,col,moves): def getLionMoves(self,row,col,moves):
directions = ((-1, 0), (0, -1), (1, 0), (0, 1)) directions = [(1,0),(0,1),(-1,0),(0,-1)]
enemy_color = "b" if self.red_to_move else "r" enemy_color = 'b' if self.red_to_move else 'r'
for direction in directions: for direction in directions:
for i in range(1, 2): new_row = row + direction[0]*1
end_row = row + direction[0] * 1 new_col = col + direction[1]*1
end_col = col + direction[1] * 1 if 0<=new_row<=6 and 0<=new_col<=8 :
if 0 <= end_row <= 6 and 0 <= end_col <= 8: # check for possible moves only in boundaries of the board nxt_piece = self.board[new_row][new_col]
end_piece = self.board[end_row][end_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(Move((row,col),(new_row,new_col),self.board))
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 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(Move((row, col), (end_row, end_col), self.board)) moves.append(Move((row,col),(new_row,new_col),self.board))
jump_vert_loc=[(0,3),(0,4),(0,5),(3,3),(3,4),(3,5),(6,3),(6,4),(6,5)]
elif end_piece == "--" and self.inWater(end_row,end_col): jump_hori_loc=[(1,2),(1,6),(2,2),(2,6),(4,2),(4,6),(5,2),(5,6)]
jump_row = end_row - row #Vertical jump enemy_mouse = enemy_color + '1'
jump_col = end_col - col #Horizontal jump if (row,col) in jump_vert_loc:
if row == 0:
if jump_row != 0 and self.jumpConditions(row,col,end_row,end_col,jump_row,jump_col,enemy_color): if self.board[1][col] != enemy_mouse and self.board[2][col] != enemy_mouse:
moves.append(Move((row, col), (end_row+(3*jump_row), end_col), self.board)) if self.Eliminate(row,col,3,col): moves.append(Move((row,col),(3,col),self.board))
elif row == 3:
elif jump_col != 0 and self.jumpConditions(row,col,end_row,end_col,jump_row,jump_col,enemy_color): if self.board[1][col] != enemy_mouse and self.board[2][col] != enemy_mouse:
moves.append((end_row, end_col+(2*jump_col))) if self.Eliminate(row,col,0,col): moves.append(Move((row,col),(0,col),self.board))
if self.board[4][col] != enemy_mouse and self.board[5][col] != enemy_mouse:
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 if self.Eliminate(row,col,6,col): moves.append(Move((row,col),(6,col),self.board))
moves.append(Move((row, col), (end_row, end_col), self.board)) elif row == 6:
break if self.board[4][col] != enemy_mouse and self.board[5][col] != enemy_mouse:
else: # friendly piece if self.Eliminate(row,col,3,col): moves.append(Move((row,col),(3,col),self.board))
break elif (row,col) in jump_hori_loc:
else: # off board if col == 2:
break lea = True
for i in range(3,6):
if self.board[row][i] == enemy_mouse: lea = False
if lea and self.Eliminate(row,col,row,6): moves.append(Move((row,col),(row,6),self.board))
elif col == 6:
lea = True
for i in range(3,6):
if self.board[row][i] == enemy_mouse: lea = False
if lea and self.Eliminate(row,col,row,2): moves.append(Move((row,col),(row,2),self.board))
return moves
# 判断是否胜利 # 判断是否胜利
def conquer(self): def conquer(self):
if self.board[3][0][0] == 'r' or self.board[3][8][0] == 'b': if self.board[3][0][0] == 'r' or self.board[3][8][0] == 'b':
self.conquered = True self.conquered = True
if self.board[3][0][0] == 'r':
self.win_person = 'r'
else: self.win_person = 'b'
elif len(self.blue_pieces) == 0:
self.conquered = True
self.win_person = 'r'
elif len(self.red_pieces) == 0:
self.conquered = True
self.win_person = 'b'
elif len(self.getAllMoves()) == 0:
self.conquered = True
self.win_person = 'r' if self.color() == 'b' else 'b'
return self.conquered return self.conquered
#移动操作 #移动操作
def makeMove(self,move):#cur是当前位置nxt是下一个位置,参数传入为元组 def makeMove(self,move):#cur是当前位置nxt是下一个位置,参数传入为元组
# makeMove假设这个move一定是合法的
if self.board[move.end_row][move.end_col] != '00':
nxt_piece = self.board[move.end_row][move.end_col]
if nxt_piece[0] == 'r':
self.red_pieces.remove(int(nxt_piece[1]))
else:
self.blue_pieces.remove(int(nxt_piece[1]))
self.board[move.end_row][move.end_col] = self.board[move.start_row][move.start_col] 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' self.board[move.start_row][move.start_col] = '00'
self.red_to_move = not self.red_to_move self.red_to_move = not self.red_to_move

Loading…
Cancel
Save