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