still errors
This commit is contained in:
parent
a6993ca49b
commit
7069c9df47
1 changed files with 70 additions and 41 deletions
111
player.py
111
player.py
|
@ -35,11 +35,11 @@ class Piece:
|
|||
validated_jumps = []
|
||||
|
||||
for jump in valid_jumps:
|
||||
if not self.does_jump_split_hive(jump, board):
|
||||
if not self.hive_integrity_check(jump, board):
|
||||
validated_jumps.append(jump)
|
||||
return validated_jumps
|
||||
|
||||
def does_jump_split_hive(self, jump, board):
|
||||
def hive_integrity_check(self, jump, board):
|
||||
board_copy = {p: {q: board[p][q] for q in board[p]} for p in board}
|
||||
|
||||
# Simulate the move
|
||||
|
@ -82,10 +82,10 @@ class Bee(Piece):
|
|||
self.q = q
|
||||
self.team = team
|
||||
|
||||
def get_valid_jumps(self) -> list:
|
||||
def get_valid_jumps(self, board) -> list:
|
||||
valid_moves = []
|
||||
for neighbor in get_neighbors(self.p, self.q):
|
||||
if neighbor in self.board and self.board[neighbor] == "":
|
||||
if neighbor in board and board[neighbor] == "":
|
||||
valid_moves.append(neighbor)
|
||||
return valid_moves
|
||||
|
||||
|
@ -155,7 +155,8 @@ class Grasshopper(Piece):
|
|||
):
|
||||
yield pos
|
||||
|
||||
return list(generate_moves())
|
||||
valid_moves = list(generate_moves())
|
||||
return valid_moves
|
||||
|
||||
|
||||
class Ant(Piece):
|
||||
|
@ -184,7 +185,8 @@ class Ant(Piece):
|
|||
yield next_pos
|
||||
yield from explore(next_pos[0], next_pos[1])
|
||||
|
||||
return list(explore(self.p, self.q))
|
||||
valid_moves = list(explore(self.p, self.q))
|
||||
return valid_moves
|
||||
|
||||
|
||||
class Player(Base.Board):
|
||||
|
@ -263,44 +265,77 @@ class Player(Base.Board):
|
|||
piece_class = self.get_piece_class(piece_letter)
|
||||
unplaced_pieces.append(piece_class(None, None, True))
|
||||
return unplaced_pieces
|
||||
|
||||
def get_valid_placements(self, translated_board, piece_to_place):
|
||||
valid_placements = []
|
||||
|
||||
if not translated_board:
|
||||
# If the board is empty, the piece can be placed anywhere.
|
||||
# For example, return the center of the board or any other arbitrary position.
|
||||
# If the board is empty, place the piece at the center.
|
||||
if not any(row.values() for row in translated_board.values()):
|
||||
return [(3, 6)]
|
||||
|
||||
# Iterate over each tile in the board
|
||||
for p, row in translated_board.items():
|
||||
for q, piece in row.items():
|
||||
if piece and piece.team == self.myIsUpper:
|
||||
# Check all neighbors of the piece
|
||||
for neighbor_p, neighbor_q in get_neighbors(p, q):
|
||||
if (
|
||||
neighbor_p,
|
||||
neighbor_q,
|
||||
) in translated_board and not translated_board[neighbor_p][
|
||||
neighbor_q
|
||||
]:
|
||||
# Check if the position is only adjacent to friendly pieces
|
||||
if all(
|
||||
not translated_board.get(adj_p, {}).get(adj_q)
|
||||
or translated_board[adj_p][adj_q].team == self.myIsUpper
|
||||
for adj_p, adj_q in get_neighbors(
|
||||
neighbor_p, neighbor_q
|
||||
)
|
||||
):
|
||||
valid_placements.append((neighbor_p, neighbor_q))
|
||||
for q, tile_content in row.items():
|
||||
# Check if the tile is empty
|
||||
if tile_content == "":
|
||||
# Check if any neighbors are your pieces and none are opponent's pieces
|
||||
neighbors = get_neighbors(p, q)
|
||||
if any(
|
||||
# Check if the neighboring tile is occupied by your team's piece
|
||||
isinstance(translated_board.get(np, {}).get(nq), Piece) and
|
||||
translated_board[np][nq].team == piece_to_place.team
|
||||
for np, nq in neighbors
|
||||
) and not any(
|
||||
# Check if the neighboring tile is occupied by the opponent's piece
|
||||
isinstance(translated_board.get(np, {}).get(nq), Piece) and
|
||||
translated_board[np][nq].team != piece_to_place.team
|
||||
for np, nq in neighbors
|
||||
):
|
||||
valid_placements.append((p, q))
|
||||
|
||||
return valid_placements
|
||||
valid_placements = []
|
||||
|
||||
# If the board is empty, place the piece at the center.
|
||||
if all(not row for row in translated_board.values()):
|
||||
return [(3, 6)]
|
||||
|
||||
# Iterate over each tile in the board
|
||||
for p, row in translated_board.items():
|
||||
for q, tile_content in row.items():
|
||||
# Check if the tile is empty
|
||||
if tile_content == "":
|
||||
# Check if any neighbors are your pieces and none are opponent's pieces
|
||||
neighbors = get_neighbors(p, q)
|
||||
if any(
|
||||
translated_board.get(np, {}).get(nq) and
|
||||
translated_board[np][nq].team == self.myIsUpper
|
||||
for np, nq in neighbors
|
||||
) and not any(
|
||||
translated_board.get(np, {}).get(nq) and
|
||||
translated_board[np][nq].team != self.myIsUpper
|
||||
for np, nq in neighbors
|
||||
):
|
||||
valid_placements.append((p, q))
|
||||
|
||||
return valid_placements
|
||||
|
||||
def move(self):
|
||||
def move(self, iter = 0):
|
||||
translated_board, total_pieces_count, _ = self.translate_board(self.board)
|
||||
|
||||
def choose_random_piece(pieces):
|
||||
return random.choice(pieces) if pieces else None
|
||||
|
||||
# Check if the queen bee is placed
|
||||
be_unplaced = "q" in [k.lower() for k in self.myPieces.keys()] and {k.lower(): v for k,v in self.myPieces.items()}["q"]!=0
|
||||
|
||||
# Logic for placing the queen bee based on a coin flip or on the 4th turn
|
||||
if be_unplaced and (total_pieces_count > 3 or random.choice([True, False])):
|
||||
queen_bee = self.get_piece_class('Q')(None, None, self.myIsUpper)
|
||||
valid_placements = self.get_valid_placements(translated_board, queen_bee)
|
||||
if valid_placements:
|
||||
new_p, new_q = random.choice(valid_placements)
|
||||
return queen_bee.get_piece_info(self.myIsUpper)[:1] + [None, None, new_p, new_q]
|
||||
|
||||
if total_pieces_count == 0:
|
||||
piece_to_place = choose_random_piece(self.get_unplaced_pieces())
|
||||
return (
|
||||
|
@ -325,27 +360,23 @@ class Player(Base.Board):
|
|||
|
||||
else:
|
||||
move_or_place = random.choice(["move", "place"])
|
||||
if move_or_place == "move" and self.queen_placed:
|
||||
print(translated_board)
|
||||
if move_or_place == "move":
|
||||
movable_pieces = [
|
||||
piece
|
||||
for row in translated_board.values()
|
||||
for piece in row.values()
|
||||
if piece and piece.validate_jumps(piece, translated_board)
|
||||
if piece and piece.validate_jumps(translated_board) and piece.team
|
||||
]
|
||||
chosen_piece = choose_random_piece(movable_pieces)
|
||||
if chosen_piece:
|
||||
new_p, new_q = random.choice(
|
||||
chosen_piece.validate_jumps(chosen_piece, translated_board)
|
||||
chosen_piece.validate_jumps(translated_board)
|
||||
)
|
||||
return chosen_piece.get_piece_info(self.myIsUpper) + [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
|
||||
)
|
||||
print(piece_to_place, valid_placements)
|
||||
valid_placements = self.get_valid_placements(translated_board, piece_to_place)
|
||||
new_p, new_q = (
|
||||
random.choice(valid_placements)
|
||||
if valid_placements
|
||||
|
@ -357,9 +388,7 @@ class Player(Base.Board):
|
|||
new_p,
|
||||
new_q,
|
||||
]
|
||||
return (
|
||||
[]
|
||||
) # uncase of the inability to do anything, return empty list tho this wont ever run
|
||||
return []
|
||||
|
||||
|
||||
def updatePlayers(move, activePlayer, passivePlayer):
|
||||
|
|
Loading…
Add table
Reference in a new issue