Started integrating c++ into python
This commit is contained in:
@@ -59,20 +59,24 @@ class Board:
|
||||
MAX_COLUMN_SIZE = 8
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.field: List[List[Card]] = [[]] * Board.MAX_COLUMN_SIZE
|
||||
self.bunker: List[Union[Tuple[SpecialCard, int], Optional[Card]]] = [None] * 3
|
||||
self.goal: List[Optional[NumberCard]] = [None] * 3
|
||||
self.flower_gone: bool = False
|
||||
self._field: List[List[Card]] = [[]] * Board.MAX_COLUMN_SIZE
|
||||
self._bunker: List[Union[Tuple[SpecialCard, int], Optional[Card]]] = [None] * 3
|
||||
self._goal: List[Optional[NumberCard]] = [None] * 3
|
||||
self._flower_gone: bool = False
|
||||
|
||||
def getGoal(self, suit: NumberCard.Suit) -> int:
|
||||
for card in self.goal:
|
||||
for card in self._goal:
|
||||
if card is not None and card.suit == suit:
|
||||
return card.number
|
||||
else:
|
||||
return 0
|
||||
|
||||
def setField(self, field: List[List[Card]]) -> None:
|
||||
assert len(field) == 8
|
||||
self._field = field
|
||||
|
||||
def getGoalId(self, suit: NumberCard.Suit) -> int:
|
||||
for index, card in enumerate(self.goal):
|
||||
for index, card in enumerate(self._goal):
|
||||
if card is not None and card.suit == suit:
|
||||
return index
|
||||
else:
|
||||
@@ -83,9 +87,9 @@ class Board:
|
||||
assert 0 <= value
|
||||
assert value <= 9
|
||||
if value == 0:
|
||||
self.goal[self.getGoalId(suit)] = None
|
||||
self._goal[self.getGoalId(suit)] = None
|
||||
else:
|
||||
self.goal[self.getGoalId(suit)] = NumberCard(suit, number=value)
|
||||
self._goal[self.getGoalId(suit)] = NumberCard(suit, number=value)
|
||||
|
||||
def incGoal(self, suit: NumberCard.Suit) -> None:
|
||||
self.setGoal(suit, self.getGoal(suit) + 1)
|
||||
@@ -96,16 +100,16 @@ class Board:
|
||||
return False
|
||||
if any(not isinstance(x, tuple) for x in self.bunker):
|
||||
return False
|
||||
if not self.flower_gone:
|
||||
if not self._flower_gone:
|
||||
return False
|
||||
assert all(not x for x in self.field)
|
||||
assert all(not x for x in self._field)
|
||||
return True
|
||||
|
||||
@property
|
||||
def state_identifier(self) -> int:
|
||||
"""Returns a unique identifier to represent the board state"""
|
||||
result: int = 0
|
||||
for card in self.bunker:
|
||||
for card in self._bunker:
|
||||
result <<= 2
|
||||
if isinstance(card, tuple):
|
||||
result |= 0
|
||||
@@ -119,10 +123,10 @@ class Board:
|
||||
result |= card.identifier()
|
||||
|
||||
result <<= 1
|
||||
if self.flower_gone:
|
||||
if self._flower_gone:
|
||||
result |= 1
|
||||
|
||||
assert len(self.goal) == 3
|
||||
assert len(self._goal) == 3
|
||||
suit_sequence = list(NumberCard.Suit)
|
||||
for card in self.goal:
|
||||
result <<= 5
|
||||
@@ -134,12 +138,12 @@ class Board:
|
||||
# Max stack size is 13
|
||||
# (4 random cards from the start, plus a stack from 9 to 1)
|
||||
# So 4 bits are sufficient
|
||||
for stack in self.field:
|
||||
for stack in self._field:
|
||||
assert len(stack) == len(stack) & 0b1111
|
||||
result <<= 4
|
||||
result |= len(stack)
|
||||
|
||||
for field_card in itertools.chain.from_iterable(self.field):
|
||||
for field_card in itertools.chain.from_iterable(self._field):
|
||||
result <<= 5
|
||||
result |= field_card.identifier()
|
||||
|
||||
@@ -159,12 +163,12 @@ class Board:
|
||||
SpecialCard.Hua: 0,
|
||||
}
|
||||
|
||||
if self.flower_gone:
|
||||
if self._flower_gone == True:
|
||||
special_cards[SpecialCard.Hua] += 1
|
||||
|
||||
for card in itertools.chain(
|
||||
self.bunker,
|
||||
itertools.chain.from_iterable(stack for stack in self.field if stack),
|
||||
self._bunker,
|
||||
itertools.chain.from_iterable(stack for stack in self._field if stack),
|
||||
):
|
||||
if isinstance(card, tuple):
|
||||
special_cards[card[0]] += 4
|
||||
|
||||
Reference in New Issue
Block a user