fixed some issues, offline test not working

This commit is contained in:
Heli-o 2024-01-03 00:13:13 +01:00
parent 6a96aa8e36
commit dde404034a
3 changed files with 22 additions and 46 deletions

Binary file not shown.

BIN
begin.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

View file

@ -18,7 +18,6 @@ def get_neighbors(p, q):
class Piece: class Piece:
def get_piece_info(self): def get_piece_info(self):
# Convert the class name to the corresponding letter
class_to_letter = { class_to_letter = {
"Bee": "Q", "Bee": "Q",
"Beetle": "B", "Beetle": "B",
@ -27,7 +26,7 @@ class Piece:
"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:
letter = letter.lower() letter = letter.lower()
return [letter, self.p, self.q] return [letter, self.p, self.q]
@ -36,17 +35,14 @@ class Piece:
class Bee(Piece): class Bee(Piece):
def __init__(self, board, p, q, team): def __init__(self, p, q, team):
self.board = board
self.p = p self.p = p
self.q = q self.q = q
self.team = team # 'team' should be 'upper' or 'lower' based on the piece case. self.team = team
def get_valid_jumps(self) -> list: def get_valid_jumps(self) -> list:
# The bee can move to an adjacent empty space.
valid_moves = [] valid_moves = []
for neighbor in get_neighbors(self.p, self.q): for neighbor in get_neighbors(self.p, self.q):
# Check if the neighbor is within the bounds of the board and is empty
if neighbor in self.board and self.board[neighbor] == "": if neighbor in self.board and self.board[neighbor] == "":
valid_moves.append(neighbor) valid_moves.append(neighbor)
return valid_moves return valid_moves
@ -59,7 +55,6 @@ class Beetle(Piece):
self.team = team self.team = team
def get_valid_jumps(self, board): def get_valid_jumps(self, board):
# The beetle can move to an adjacent space whether it is empty or not.
valid_moves = get_neighbors(self.p, self.q) valid_moves = get_neighbors(self.p, self.q)
return valid_moves return valid_moves
@ -71,25 +66,22 @@ class Spider(Piece):
self.team = team self.team = team
def get_valid_jumps(self, board): def get_valid_jumps(self, board):
# Use BFS to find all tiles exactly three moves away
start = (self.p, self.q) start = (self.p, self.q)
visited = set() # Keep track of visited tiles visited = set()
queue = deque([(start, 0)]) # Queue of (position, distance) queue = deque([(start, 0)]) # Queue of (position, distance)
valid_moves = [] valid_moves = []
while queue: while queue:
current_position, current_distance = queue.popleft() current_position, current_distance = queue.popleft()
if current_distance == 3: if current_distance == 3:
# We have found a tile exactly three moves away
valid_moves.append(current_position) valid_moves.append(current_position)
continue # We don't want to move further from this tile continue
if current_position in visited: if current_position in visited:
continue # Already visited this tile continue
visited.add(current_position) visited.add(current_position)
# Get all neighboring positions
for neighbor in get_neighbors(*current_position): for neighbor in get_neighbors(*current_position):
if ( if (
neighbor in board neighbor in board
@ -110,13 +102,10 @@ class Grasshopper(Piece):
def get_valid_jumps(self, board): def get_valid_jumps(self, board):
# Generator function to yield valid moves # Generator function to yield valid moves
def generate_moves(): def generate_moves():
# Check each direction for possible jumps for dp, dq in get_neighbors(0, 0):
for dp, dq in get_neighbors(0, 0): # Use (0, 0) to get direction vectors
pos = (self.p + dp, self.q + dq) pos = (self.p + dp, self.q + dq)
# Continue in the direction until we find a piece to jump over
while pos in board and board[pos] != "": while pos in board and board[pos] != "":
pos = (pos[0] + dp, pos[1] + dq) pos = (pos[0] + dp, pos[1] + dq)
# If we jumped over at least one piece and landed on an empty space, yield the move
if ( if (
pos in board pos in board
and board[pos] == "" and board[pos] == ""
@ -136,12 +125,11 @@ class Ant(Piece):
def get_valid_jumps(self, board): def get_valid_jumps(self, board):
visited = set() # Keep track of visited nodes to prevent cycles visited = set() # Keep track of visited nodes to prevent cycles
# Recursive function to explore all valid moves # Just a recursive, eh?
def explore(p, q): def explore(p, q):
for dp, dq in get_neighbors(0, 0): # Get direction vectors for dp, dq in get_neighbors(0, 0):
new_p, new_q = p + dp, q + dq new_p, new_q = p + dp, q + dq
next_pos = (new_p, new_q) next_pos = (new_p, new_q)
# Continue in this direction while there are pieces to slide around
while ( while (
next_pos in board next_pos in board
and board[next_pos] != "" and board[next_pos] != ""
@ -149,14 +137,11 @@ class Ant(Piece):
): ):
visited.add(next_pos) visited.add(next_pos)
next_pos = (next_pos[0] + dp, next_pos[1] + dq) next_pos = (next_pos[0] + dp, next_pos[1] + dq)
# If we found an empty space, add it as a valid move and explore from there
if next_pos in board and board[next_pos] == "": if next_pos in board and board[next_pos] == "":
visited.add(next_pos) visited.add(next_pos)
yield next_pos yield next_pos
# Explore further moves from this new position
yield from explore(next_pos[0], next_pos[1]) yield from explore(next_pos[0], next_pos[1])
# Start exploring from the Ant's current position
return list(explore(self.p, self.q)) return list(explore(self.p, self.q))
@ -187,7 +172,7 @@ class Player(Base.Board):
return result return result
def translate_board(self, board): def translate_board(self, board):
# Create a dictionary to map letters to piece classes # mapper dict
piece_class_mapping = { piece_class_mapping = {
"Q": Bee, "Q": Bee,
"B": Beetle, "B": Beetle,
@ -197,16 +182,15 @@ class Player(Base.Board):
} }
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
my_pieces_count = 0 # To count the number of your pieces on the board my_pieces_count = 0
for p, row in board.items(): for p, row in board.items():
for q, piece_letter in row.items(): for q, tile_content in row.items():
if ( if tile_content.isalpha():
piece_letter.isalpha() topmost_piece_letter = tile_content[-1]
): # Check if it's a letter representing a piece is_upper = topmost_piece_letter.isupper()
is_upper = piece_letter.isupper() piece_class = piece_class_mapping.get(topmost_piece_letter.upper())
piece_class = piece_class_mapping.get(piece_letter.upper())
if piece_class: if piece_class:
piece_instance = piece_class(p, q, is_upper == self.myIsUpper) piece_instance = piece_class(p, q, is_upper == self.myIsUpper)
translated_board[p][q] = piece_instance translated_board[p][q] = piece_instance
@ -214,25 +198,22 @@ class Player(Base.Board):
if is_upper == self.myIsUpper: if is_upper == self.myIsUpper:
my_pieces_count += 1 my_pieces_count += 1
else: else:
translated_board[p][q] = piece_letter translated_board[p][q] = topmost_piece_letter
else: else:
translated_board[p][q] = piece_letter translated_board[p][q] = tile_content
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
return any(isinstance(p, Bee) for p in self.myPieces) return any(isinstance(p, Bee) for p in self.myPieces)
def get_piece_class(self, letter): def get_piece_class(self, letter):
# Maps letter to piece class
return {"Q": Bee, "B": Beetle, "S": Spider, "G": Grasshopper, "A": Ant}.get( return {"Q": Bee, "B": Beetle, "S": Spider, "G": Grasshopper, "A": Ant}.get(
letter letter.upper()
) )
def get_unplaced_pieces(self): def get_unplaced_pieces(self):
# Return a list of unplaced pieces
unplaced_pieces = [] unplaced_pieces = []
for piece_letter, count in self.myPieces.items(): for piece_letter, count in self.myPieces.items():
for _ in range(count): for _ in range(count):
@ -243,11 +224,9 @@ class Player(Base.Board):
def move(self): def move(self):
translated_board, total_pieces_count, _ = self.translate_board(self.board) translated_board, total_pieces_count, _ = self.translate_board(self.board)
# Randomly choose a piece to place or move
def choose_random_piece(pieces): def choose_random_piece(pieces):
return random.choice(pieces) if pieces else None return random.choice(pieces) if pieces else None
# 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 ( return (
@ -256,11 +235,10 @@ class Player(Base.Board):
else [] else []
) )
# If we're the second player, place next to the first player's piece
elif total_pieces_count == 1: elif total_pieces_count == 1:
for _, row in translated_board.items(): for _, row in translated_board.items():
for _, piece in row.items(): for _, piece in row.items():
if piece: # Found the first player's piece if piece:
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())
@ -271,7 +249,6 @@ class Player(Base.Board):
else [] 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: 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":
@ -304,8 +281,7 @@ class Player(Base.Board):
new_q, new_q,
] ]
# If it's not possible to move or place a piece, pass the turn return [] # uncase of the inability to do anything, return empty list
return []
def updatePlayers(move, activePlayer, passivePlayer): def updatePlayers(move, activePlayer, passivePlayer):