This commit is contained in:
Heli-o 2024-01-01 19:03:55 +01:00
parent 3412605b1e
commit 7ec9c0a084

View file

@ -20,11 +20,11 @@ class Piece:
def get_piece_info(self):
# Convert the class name to the corresponding letter
class_to_letter = {
'Bee': 'Q',
'Beetle': 'B',
'Spider': 'S',
'Grasshopper': 'G',
'Ant': 'A'
"Bee": "Q",
"Beetle": "B",
"Spider": "S",
"Grasshopper": "G",
"Ant": "A",
}
letter = class_to_letter[self.__class__.__name__]
if not self.team: # If the piece is not on the player's team, make it lowercase
@ -189,11 +189,11 @@ class Player(Base.Board):
def translate_board(self, board):
# Create a dictionary to map letters to piece classes
piece_class_mapping = {
'Q': Bee,
'B': Beetle,
'S': Spider,
'G': Grasshopper,
'A': Ant
"Q": Bee,
"B": Beetle,
"S": Spider,
"G": Grasshopper,
"A": Ant,
}
translated_board = {p: {} for p in board}
@ -202,7 +202,9 @@ class Player(Base.Board):
for p, row in board.items():
for q, piece_letter in row.items():
if piece_letter.isalpha(): # Check if it's a letter representing a piece
if (
piece_letter.isalpha()
): # Check if it's a letter representing a piece
is_upper = piece_letter.isupper()
piece_class = piece_class_mapping.get(piece_letter.upper())
if piece_class:
@ -225,13 +227,9 @@ class Player(Base.Board):
def get_piece_class(self, letter):
# Maps letter to piece class
return {
'Q': Bee,
'B': Beetle,
'S': Spider,
'G': Grasshopper,
'A': Ant
}.get(letter)
return {"Q": Bee, "B": Beetle, "S": Spider, "G": Grasshopper, "A": Ant}.get(
letter
)
def get_unplaced_pieces(self):
# Return a list of unplaced pieces
@ -252,7 +250,11 @@ class Player(Base.Board):
# If we're the first player and no pieces are on the board, place a random piece at the center
if total_pieces_count == 0:
piece_to_place = choose_random_piece(self.get_unplaced_pieces())
return piece_to_place.get_piece_info()[:1] + [None, None, 3, 6] if piece_to_place else []
return (
piece_to_place.get_piece_info()[:1] + [None, None, 3, 6]
if piece_to_place
else []
)
# If we're the second player, place next to the first player's piece
elif total_pieces_count == 1:
@ -262,29 +264,50 @@ class Player(Base.Board):
adjacent_positions = get_neighbors(piece.p, piece.q)
random_position = choose_random_piece(adjacent_positions)
piece_to_place = choose_random_piece(self.get_unplaced_pieces())
return piece_to_place.get_piece_info()[:1] + [None, None, *random_position] if piece_to_place else []
return (
piece_to_place.get_piece_info()[:1]
+ [None, None, *random_position]
if piece_to_place
else []
)
# After the queen is placed or after the 4th turn, randomly choose between moving and placing a piece
elif self.queen_placed or total_pieces_count >= 8:
move_or_place = random.choice(["move", "place"])
if move_or_place == "move":
movable_pieces = [p for p in translated_board.values() if p.get_valid_jumps(translated_board)]
movable_pieces = [
p
for p in translated_board.values()
if p.get_valid_jumps(translated_board)
]
chosen_piece = choose_random_piece(movable_pieces)
if chosen_piece:
new_p, new_q = random.choice(chosen_piece.get_valid_jumps(translated_board))
new_p, new_q = random.choice(
chosen_piece.get_valid_jumps(translated_board)
)
return chosen_piece.get_piece_info() + [new_p, new_q]
else:
piece_to_place = choose_random_piece(self.get_unplaced_pieces())
if piece_to_place:
valid_placements = self.get_valid_placements(translated_board, piece_to_place)
new_p, new_q = random.choice(valid_placements) if valid_placements else (None, None)
return piece_to_place.get_piece_info()[:1] + [None, None, new_p, new_q]
valid_placements = self.get_valid_placements(
translated_board, piece_to_place
)
new_p, new_q = (
random.choice(valid_placements)
if valid_placements
else (None, None)
)
return piece_to_place.get_piece_info()[:1] + [
None,
None,
new_p,
new_q,
]
# If it's not possible to move or place a piece, pass the turn
return []
def updatePlayers(move, activePlayer, passivePlayer):
"""write move made by activePlayer player
this method assumes that all moves are correct, no checking is made