Detect sequential cards
This commit is contained in:
parent
575b3ef340
commit
a97d2b9342
90
blackjack.py
90
blackjack.py
|
@ -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
|
||||||
|
for card in cards:
|
||||||
self.play_card(player, card)
|
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,27 +461,23 @@ 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 = []
|
|
||||||
for card in parsed_cards:
|
|
||||||
if len(card) == 0:
|
|
||||||
print("Invalid card")
|
|
||||||
continue
|
|
||||||
print("Parsed card iter", card)
|
|
||||||
try:
|
try:
|
||||||
card_to_play = Card(card)
|
cards_to_play = list(map(Card, parsed_cards))
|
||||||
print("Card to play", card_to_play)
|
print("Cards to play", cards_to_play)
|
||||||
except InvalidCard:
|
played = game.play_combination(current_player, *cards_to_play)
|
||||||
print("Invalid card:", card)
|
# if len(cards_to_play) == 1:
|
||||||
continue
|
# if len(card) == 0:
|
||||||
if card_to_play not in current_player.hand.cards:
|
# print("Invalid card")
|
||||||
print(f"Card is not in your hand: {card_to_play}")
|
# continue
|
||||||
continue
|
# card_to_play = Card(card)
|
||||||
print(f"Attempting to play {card_to_play}")
|
# print("Card to play", card_to_play)
|
||||||
played = game.play_card(current_player, card_to_play)
|
#
|
||||||
if not played:
|
# print(f"Attempting to play {card_to_play}")
|
||||||
print("Illegal move!")
|
# played = game.play_card(current_player, card_to_play)
|
||||||
continue
|
# if not played:
|
||||||
if card_to_play == game.rules.change_suit:
|
# print("Illegal move!")
|
||||||
|
# continue
|
||||||
|
except ChangeSuit:
|
||||||
suit = None
|
suit = None
|
||||||
while not suit:
|
while not suit:
|
||||||
new_suit = input("Enter a new suit: #> ")
|
new_suit = input("Enter a new suit: #> ")
|
||||||
|
@ -470,6 +488,18 @@ while 1:
|
||||||
continue
|
continue
|
||||||
print(f"Changing last card of {game.playdeck.cards[-1]} to {suit}")
|
print(f"Changing last card of {game.playdeck.cards[-1]} to {suit}")
|
||||||
game.playdeck.cards[-1].suit = 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:
|
if len(current_player.hand.cards) == 0:
|
||||||
print(f"Player {current_player} wins!")
|
print(f"Player {current_player} wins!")
|
||||||
|
|
Loading…
Reference in New Issue