Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 0 additions & 38 deletions src/card.py

This file was deleted.

69 changes: 45 additions & 24 deletions src/highlow.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,22 @@
RICH_CARD_FACE)
from console import console as c
from rich import print
from rich.prompt import Confirm


BET_HIGH = "high"
BET_LOW = "low"

def convert_card_to_tuple(card, hidden=False):
card_tuple = (card.value, card.suit, hidden)
return card_tuple

def show_hand(hand):
converted_cards = []
for i in range(hand.size):
converted_cards.append(convert_card_to_tuple(hand[i]))
print_cards(converted_cards)

"""This is the high low game"""
def run_game():
#initialize game variables
Expand All @@ -23,8 +34,23 @@ def run_game():
discard_pile = pd.Stack()
player = Player(player_hand, 10)
dealer = Player(dealer_hand, 1000)
def reset_game():
"""In a game over state, this function provides a way to reset the game state to keep playing."""
c.clear()
nonlocal round_count
nonlocal deck
nonlocal player_hand
nonlocal discard_pile
nonlocal player
nonlocal dealer
nonlocal dealer_hand
round_count = 0
player_hand = pd.Stack()
dealer_hand = pd.Stack()
discard_pile = pd.Stack()
player = Player(player_hand, 10)
dealer = Player(dealer_hand, 1000)

#for i in range(26):
while True:
#Shuffle phase
deck = pd.Deck(rebuild=False, re_shuffle=True)
Expand All @@ -46,16 +72,6 @@ def flip_high_low():
player_hand = deck.deal(1)
print(f"Players card is {player_hand[0]}")

def convert_card_to_tuple(card, hidden=False):
card_tuple = (card.value, card.suit, hidden)
return card_tuple

def show_hand(hand):
converted_cards = []
for i in range(hand.size):
converted_cards.append(convert_card_to_tuple(hand[i]))
print_cards(converted_cards)

#player_card_tuple = convert_card_to_tuple(player_hand.cards[0])
#print_cards([player_card_tuple])
players_card = convert_face_to_rich_text(RICH_CARD_FACE[player_hand.cards[0].value],player_hand.cards[0].suit)
Expand Down Expand Up @@ -115,6 +131,7 @@ def show_hand(hand):
continue
player_is_dur_dur_dur = False

c.clear()
print(f"Player's bet is {player_bet}")
print(f"The Ace Standard Coin value is revealed: {ace_standard_coin}")
print(f"Dealer shows their hand")
Expand Down Expand Up @@ -152,24 +169,28 @@ def show_hand(hand):
print(f"Player loses! {player_hand[0].value} is {str_val} than {dealer_hand[0].value}")
dealer.award_credits(pot)

#Are we at a game over state?
print(f"Player's remaining credits: {player.credits}")
if player.credits < 10 or dealer.credits < 10:
print(f"Not enough credits to play again.")
if player.credits >= 10:
print("Player Takes the House!")
c.print("[bold yellow on black]Player Takes the House![/bold yellow on black]")
else:
print("You're broke! Game Over")
c.print("[bold white on red]You're broke! Game Over[/bold white on red]")
restart_game = Confirm.ask("Do you want to play again?")
if restart_game:
reset_game()
c.clear()
continue
else:
c.clear()
break

play_again = Confirm.ask("Another round?",console=c)
if not play_again:
c.clear()
break
else:
c.clear()

print("Player discards hand")
discard_pile += player_hand.deal(1)
print("Dealer discards hand")
discard_pile += dealer_hand.deal(1)
print(f"Discard Pile Size {discard_pile.size}")
if discard_pile.size >= 52:
print("Last card has been played. Returning the discard pile to main deck.")
discard_pile.shuffle()
deck = discard_pile.deal(52)
print(f"{deck.size} cards returned from discard pile.")
print("-----------------------------")
return True
23 changes: 23 additions & 0 deletions src/odds_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from rankdicts import WAR_RANKS



#Alright, now we need to know what the favorable outcomes are.
#First What's our cards value
first_card_value = 3
print(f"hand1 value {first_card_value}")
#How many cards are less than this card
cards_that_beat_player = 0
cards_that_player_beats = 0
for rank in WAR_RANKS:
print(f"does {first_card_value} beat {WAR_RANKS[rank]}")
if first_card_value > WAR_RANKS[rank]:
cards_that_player_beats += 4
elif first_card_value == WAR_RANKS[rank]:
cards_that_player_beats += 3
else:
cards_that_beat_player += 4

print(f"cards that beat player {cards_that_beat_player}")
print(f"cards that player beats {cards_that_player_beats}")

9 changes: 8 additions & 1 deletion src/player.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from pydealer import Stack

class Player():

def __init__(self, hand=[], credits=0):
Expand All @@ -17,7 +19,12 @@ def take_credits(self, amount):
def award_credits(self, amount):
self.credits += amount

"""Adds a card to the top of the given players hand."""
def add_card_to_hand(self, card):
self.hand.append(card)
if type(self.hand) is Stack:
self.hand.add(card)
else:
self.hand.append(card)



30 changes: 28 additions & 2 deletions src/rankdicts.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,32 @@
"5": 5,
"4": 4,
"3": 3,
"2": 2,
"Joker": 0
"2": 2
}

WAR_RANKS = {
"Ace": 14,
"King": 13,
"Queen": 12,
"Jack": 11,
"10": 10,
"9": 9,
"8": 8,
"7": 7,
"6": 6,
"5": 5,
"4": 4,
"3": 3,
"2": 2
}

def card_to_int(card,rank_dict) -> int:
return rank_dict[card.value]

def stack_to_int(stack,rank_dict) -> int:
accumulator = 0
for card in stack:
accumulator += card_to_int(card, rank_dict)
return accumulator


7 changes: 0 additions & 7 deletions src/test_card.py

This file was deleted.

21 changes: 14 additions & 7 deletions src/test_player.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,39 @@
import unittest
from card import PlayingCard
from player import Player
import pydealer as pd

class TestPlayer(unittest.TestCase):

def setup_player(self):
player_hand = pd.Stack()
deck = pd.Deck()
player_hand += deck.deal(3)
player = Player(player_hand, 10)
return player

def test_player_repr(self):
player = Player([PlayingCard("2", "hearts"),PlayingCard("3", "spades"), PlayingCard("4", "diamonds")], 10)
player = self.setup_player()
self.assertEqual("Player has 3 cards and 10 credits", str(player))

def test_take_credits(self):
player = Player([PlayingCard("2", "hearts"),PlayingCard("3", "spades"), PlayingCard("4", "diamonds")], 10)
player = self.setup_player()
player.take_credits(5)
self.assertEqual("Player has 3 cards and 5 credits", str(player))

def test_take_credits_too_many(self):
player = Player([PlayingCard("2", "hearts"),PlayingCard("3", "spades"), PlayingCard("4", "diamonds")], 10)
player = self.setup_player()
try:
player.take_credits(59)
self.fail("Taking more credits than player has should throw a ValueError")
except ValueError:
pass

def test_add_card_to_hand(self):
player = Player([PlayingCard("2", "hearts"),PlayingCard("3", "spades"), PlayingCard("4", "diamonds")], 10)
player.add_card_to_hand(PlayingCard("5", "clubs"))
player = self.setup_player()
player.add_card_to_hand(pd.Deck().deal(1))
self.assertEqual("Player has 4 cards and 10 credits", str(player))

def test_award_credits(self):
player = Player([PlayingCard("2", "hearts"),PlayingCard("3", "spades"), PlayingCard("4", "diamonds")], 10)
player = self.setup_player()
player.award_credits(10)
self.assertEqual("Player has 3 cards and 20 credits", str(player))
1 change: 1 addition & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#!/bin/bash
python3 -m unittest discover -s src