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