Detect sequential cards

This commit is contained in:
Mark Veidemanis 2022-03-12 00:03:50 +00:00
parent 575b3ef340
commit a97d2b9342
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
1 changed files with 72 additions and 42 deletions

View File

@ -10,6 +10,18 @@ class InvalidCard(Exception):
class InvalidSuit(Exception): class InvalidSuit(Exception):
pass pass
class NoMatchingCard(Exception):
pass
class ChangeSuit(Exception):
pass
class IllegalMove(Exception):
pass
class NotSequential(Exception):
pass
class Game(object): class Game(object):
def __init__(self, deck, rules): def __init__(self, deck, rules):
self.players = [] self.players = []
@ -81,6 +93,7 @@ class Game(object):
last_card = self.playdeck.last_card last_card = self.playdeck.last_card
print(f"Last card {last_card}") print(f"Last card {last_card}")
if player == self.last_player: if player == self.last_player:
print("Player is last player, setting special to False")
special = False special = False
is_playable = self.rules.is_playable_on(last_card, card, special=special) is_playable = self.rules.is_playable_on(last_card, card, special=special)
print("Card is playable", is_playable) print("Card is playable", is_playable)
@ -95,17 +108,18 @@ class Game(object):
last_card = self.playdeck.last_card last_card = self.playdeck.last_card
for index, card in enumerate(cards): for index, card in enumerate(cards):
print("Combination iter", index, card) print("Combination iter", index, card)
# Special cards only active on last card is_playable = self.rules.is_playable_on(last_card, card, special=False)
if index == len(cards)-1:
print("We are checking the last card")
special = True
else:
special = False
is_playable = self.rules.is_playable_on(last_card, card, special)
if not is_playable: if not is_playable:
return False raise IllegalMove
if index != 0:
is_sequential = self.rules.is_sequential(last_card, card)
if not is_sequential:
raise NotSequential
last_card = card last_card = card
self.play_card(player, card) for card in cards:
self.play_card(player, card)
if card == self.rules.change_suit:
raise ChangeSuit
class Player(object): class Player(object):
@ -146,6 +160,7 @@ class SetOfCards(object):
self.cards.remove(mycard) self.cards.remove(mycard)
print(f"Returned {mycard} and amended cards: {self.cards}") print(f"Returned {mycard} and amended cards: {self.cards}")
return mycard return mycard
raise NoMatchingCard
def __str__(self): def __str__(self):
return str(self.cards) return str(self.cards)
@ -267,9 +282,16 @@ class Rules(object):
return True return True
return False return False
def is_playable_on(self, bottom_card, card, special, combination=False): def is_sequential(self, last_card, card):
print(f"Is playable on {bottom_card} {card}") print("Sequential", last_card, card)
is_one_above = card.val.numeric_value == last_card.val.numeric_value + 1
print("is one above", is_one_above)
is_one_below = card.val.numeric_value == last_card.val.numeric_value - 1
print("is one below", is_one_below)
return is_one_below | is_one_above
def is_playable_on(self, bottom_card, card, special):
print(f"Is playable on {bottom_card} {card}")
if special: if special:
if bottom_card in self.specials_power: if bottom_card in self.specials_power:
print("Bottom card in specials all") print("Bottom card in specials all")
@ -439,37 +461,45 @@ while 1:
text = input("~> ") text = input("~> ")
parsed_cards = text.split(",") parsed_cards = text.split(",")
print("Parsed", parsed_cards) print("Parsed", parsed_cards)
cards_to_play = [] try:
for card in parsed_cards: cards_to_play = list(map(Card, parsed_cards))
if len(card) == 0: print("Cards to play", cards_to_play)
print("Invalid card") played = game.play_combination(current_player, *cards_to_play)
continue # if len(cards_to_play) == 1:
print("Parsed card iter", card) # if len(card) == 0:
try: # print("Invalid card")
card_to_play = Card(card) # continue
print("Card to play", card_to_play) # card_to_play = Card(card)
except InvalidCard: # print("Card to play", card_to_play)
print("Invalid card:", card) #
continue # print(f"Attempting to play {card_to_play}")
if card_to_play not in current_player.hand.cards: # played = game.play_card(current_player, card_to_play)
print(f"Card is not in your hand: {card_to_play}") # if not played:
continue # print("Illegal move!")
print(f"Attempting to play {card_to_play}") # continue
played = game.play_card(current_player, card_to_play) except ChangeSuit:
if not played: suit = None
print("Illegal move!") while not suit:
continue new_suit = input("Enter a new suit: #> ")
if card_to_play == game.rules.change_suit: try:
suit = None suit = Suit(new_suit)
while not suit: except InvalidSuit:
new_suit = input("Enter a new suit: #> ") print(f"Suit {new_suit} is not valid, please select S, H, D or C")
try: continue
suit = Suit(new_suit) print(f"Changing last card of {game.playdeck.cards[-1]} to {suit}")
except InvalidSuit: game.playdeck.cards[-1].suit = suit
print(f"Suit {new_suit} is not valid, please select S, H, D or C") except InvalidCard:
continue print("Invalid card:", card)
print(f"Changing last card of {game.playdeck.cards[-1]} to {suit}") continue
game.playdeck.cards[-1].suit = suit except IllegalMove:
print("Illegal move!")
continue
except NoMatchingCard:
print("You don't have this card!")
continue
except NotSequential:
print("Combination is not sequential!")
continue
if len(current_player.hand.cards) == 0: if len(current_player.hand.cards) == 0:
print(f"Player {current_player} wins!") print(f"Player {current_player} wins!")