Still untested

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

View file

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