Worked a little more on decoupling

This commit is contained in:
Lukas Wölfer
2020-03-24 14:45:48 +01:00
parent cd1bc39bad
commit 28546cf5e9
5 changed files with 39 additions and 30 deletions

View File

@@ -9,7 +9,7 @@ def possible_huakill_action(
search_board: board.Board,
) -> Iterator[board_actions.HuaKillAction]:
"""Check if the flowercard can be eliminated"""
for index, stack in enumerate(search_board.field):
for index, stack in enumerate(search_board.getField()):
if stack and stack[-1] == board.SpecialCard.Hua:
yield board_actions.HuaKillAction(
source_field_id=index, source_field_row_index=len(stack) - 1
@@ -25,17 +25,17 @@ def possible_dragonkill_actions(
board.SpecialCard.Fa,
board.SpecialCard.Bai,
]
if not any(x is None for x in search_board.bunker):
if not any(x is None for x in search_board.getBunker()):
new_possible_dragons = []
for dragon in possible_dragons:
if any(x == dragon for x in search_board.bunker):
if any(x == dragon for x in search_board.getBunker()):
new_possible_dragons.append(dragon)
possible_dragons = new_possible_dragons
for dragon in possible_dragons:
bunker_dragons = [i for i, d in enumerate(search_board.bunker) if d == dragon]
bunker_dragons = [i for i, d in enumerate(search_board.getBunker()) if d == dragon]
field_dragons = [
i for i, f in enumerate(search_board.field) if f if f[-1] == dragon
i for i, f in enumerate(search_board.getField()) if f if f[-1] == dragon
]
if len(bunker_dragons) + len(field_dragons) != 4:
continue
@@ -44,7 +44,7 @@ def possible_dragonkill_actions(
destination_bunker_id = bunker_dragons[0]
else:
destination_bunker_id = [
i for i, x in enumerate(search_board.bunker) if x is None
i for i, x in enumerate(search_board.getBunker()) if x is None
][0]
source_stacks = [(board.Position.Bunker, i) for i in bunker_dragons]
@@ -61,13 +61,13 @@ def possible_bunkerize_actions(
search_board: board.Board,
) -> Iterator[board_actions.BunkerizeAction]:
"""Enumerates all possible card moves from the field to the bunker"""
open_bunker_list = [i for i, x in enumerate(search_board.bunker) if x is None]
open_bunker_list = [i for i, x in enumerate(search_board.getBunker()) if x is None]
if not open_bunker_list:
return
open_bunker = open_bunker_list[0]
for index, stack in enumerate(search_board.field):
for index, stack in enumerate(search_board.getField()):
if not stack:
continue
yield board_actions.BunkerizeAction(
@@ -85,11 +85,11 @@ def possible_debunkerize_actions(
"""Enumerates all possible card moves from the bunker to the field"""
bunker_number_cards = [
(i, x)
for i, x in enumerate(search_board.bunker)
for i, x in enumerate(search_board.getBunker())
if isinstance(x, board.NumberCard)
]
for index, card in bunker_number_cards:
for other_index, other_stack in enumerate(search_board.field):
for other_index, other_stack in enumerate(search_board.getField()):
if not other_stack:
continue
if not isinstance(other_stack[-1], board.NumberCard):
@@ -113,12 +113,12 @@ def possible_goal_move_actions(
"""Enumerates all possible moves from anywhere to the goal"""
field_cards = [
(board.Position.Field, index, stack[-1])
for index, stack in enumerate(search_board.field)
for index, stack in enumerate(search_board.getField())
if stack
]
bunker_cards = [
(board.Position.Bunker, index, card)
for index, card in enumerate(search_board.bunker)
for index, card in enumerate(search_board.getBunker())
]
top_cards = [
x for x in field_cards + bunker_cards if isinstance(x[2], board.NumberCard)
@@ -129,6 +129,7 @@ def possible_goal_move_actions(
result = []
for source, index, card in top_cards:
assert isinstance(card, board.NumberCard)
obvious = all(
search_board.getGoal(other_suit) >= card.number - 2
for other_suit in set(board.NumberCard.Suit) - {card.suit}
@@ -137,7 +138,7 @@ def possible_goal_move_actions(
board_actions.GoalAction(
card=card,
source_id=index,
source_row_index=len(search_board.field[index]) - 1
source_row_index=len(search_board.getField()[index]) - 1
if source == board.Position.Field
else None,
source_position=source,
@@ -164,7 +165,7 @@ def _can_stack(bottom: board.Card, top: board.Card) -> bool:
def _get_cardstacks(search_board: board.Board) -> List[List[board.Card]]:
"""Returns all cards on one stack that can be moved at once"""
result: List[List[board.Card]] = []
for stack in search_board.field:
for stack in search_board.getField():
result.append([])
if not stack:
continue
@@ -194,13 +195,13 @@ def possible_field_move_actions(
)
for source_index, source_substack in substacks:
for destination_index, destination_stack in enumerate(search_board.field):
for destination_index, destination_stack in enumerate(search_board.getField()):
if source_index == destination_index:
continue
if destination_stack:
if not _can_stack(destination_stack[-1], source_substack[0]):
continue
elif len(source_substack) == len(search_board.field[source_index]):
elif len(source_substack) == len(search_board.getField()[source_index]):
continue
elif first_empty_field_id == -1:
first_empty_field_id = destination_index
@@ -209,7 +210,7 @@ def possible_field_move_actions(
yield board_actions.MoveAction(
cards=source_substack,
source_id=source_index,
source_row_index=len(search_board.field[source_index])
source_row_index=len(search_board.getField()[source_index])
- len(source_substack),
destination_id=destination_index,
destination_row_index=len(destination_stack),