Compare commits
15 Commits
64ac166c55
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| e2c99dfea4 | |||
| d57022f742 | |||
| a3aa761739 | |||
| 4222e76262 | |||
| 8c3dbc8fcd | |||
| acd5fa7201 | |||
| a0175343f9 | |||
| 197572567a | |||
| a4cdd72ac9 | |||
| 4ae994b554 | |||
| 7979767b3d | |||
| 4043e47208 | |||
| 900304c477 | |||
| 6bc4c60ba6 | |||
| f7b80262f4 |
@@ -30,15 +30,3 @@ class Deck:
|
||||
"""Removes the last card off the deck and returns it."""
|
||||
card = self.cards.pop()
|
||||
return card
|
||||
|
||||
|
||||
# my_deck = Deck()
|
||||
# shuffled_deck = my_deck.shuffle()
|
||||
#
|
||||
# print(my_deck)
|
||||
#
|
||||
# new_card = my_deck.deal_card()
|
||||
# print("New card: \n")
|
||||
# print(str(new_card))
|
||||
#
|
||||
# print(new_card.get_value())
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import re
|
||||
import os
|
||||
from pyjack.dealer import Dealer
|
||||
from pyjack.deck import Deck
|
||||
from pyjack.player import Player
|
||||
@@ -8,14 +8,11 @@ class Game:
|
||||
"""The Game class is the central coordinator, managing the flow of
|
||||
the entire round, from setup to payout."""
|
||||
|
||||
# TODO: - play_round() function
|
||||
# - show_cards function
|
||||
|
||||
def __init__(self) -> None:
|
||||
"""Creates instances of 'Deck', 'Player', and 'Dealer'."""
|
||||
self.deck = Deck()
|
||||
self.player = Player(0)
|
||||
self.dealer = Dealer(1000)
|
||||
self.player = Player(100)
|
||||
self.dealer = Dealer(10000)
|
||||
|
||||
def compare_hands(self, bet: float) -> str:
|
||||
"""*Resolution/Payout*: Compares final hand values, checks for
|
||||
@@ -65,18 +62,95 @@ class Game:
|
||||
second card is shown or concealed."""
|
||||
|
||||
player_cards = self.player.hand.cards
|
||||
player_hand = self.player.hand
|
||||
dealer_cards = self.dealer.hand.cards
|
||||
dealer_hand = self.dealer.hand
|
||||
|
||||
print("--Player Cards:--")
|
||||
for card in player_cards:
|
||||
print(str(card))
|
||||
print(f"Hand value: {player_hand.value}")
|
||||
|
||||
if hide_dealer:
|
||||
card = dealer_cards[0]
|
||||
print("--Dealer Cards:--")
|
||||
print(str(card))
|
||||
print("XXXX")
|
||||
print("XXXX\n")
|
||||
else:
|
||||
print("--Dealer Cards:--")
|
||||
for card in dealer_cards:
|
||||
print(str(card))
|
||||
print(f"Hand value: {dealer_hand.value}\n")
|
||||
|
||||
def show_player_cards(self) -> None:
|
||||
"""Displays the players hand"""
|
||||
player_cards = self.player.hand.cards
|
||||
player_hand = self.player.hand
|
||||
|
||||
print("--Player Cards:--")
|
||||
for card in player_cards:
|
||||
print(str(card))
|
||||
|
||||
print(f"Hand value: {player_hand.value}")
|
||||
|
||||
def play_round(self) -> None:
|
||||
"""Logic of a game round"""
|
||||
|
||||
# Simplify value calls:
|
||||
|
||||
deck = self.deck
|
||||
|
||||
player = self.player
|
||||
dealer = self.dealer
|
||||
|
||||
# TODO: implement logic for setting the bet and passing the
|
||||
# value to the hand comparison. Also keep the chip ammount of
|
||||
# the player over multiple rounds.
|
||||
|
||||
# player starts with x amount and bets per round from his
|
||||
# current chips. Internally the chips are handled as a float
|
||||
# the player gets to see the float number as 'IS' and the
|
||||
# rounded down number as 'Payout'
|
||||
|
||||
# HACK: this is just to fuck around and find out...
|
||||
|
||||
player_bet = input("How much do you want to bet?")
|
||||
# player.chips += float(player_bet)
|
||||
|
||||
# Beginning of the round, the deck gets shuffled and all the
|
||||
# player cards are shown, while the dealer only shows one card.
|
||||
deck.shuffle()
|
||||
|
||||
player.hand.add_card(deck.deal_card())
|
||||
player.hand.add_card(deck.deal_card())
|
||||
dealer.hand.add_card(deck.deal_card())
|
||||
dealer.hand.add_card(deck.deal_card())
|
||||
|
||||
self.show_cards(hide_dealer=True)
|
||||
|
||||
# Player picks a card and adds it to his hand.
|
||||
player_turn = True
|
||||
while player_turn:
|
||||
if input("Do you want another card? [y] ") == "y":
|
||||
player.hit(deck)
|
||||
self.show_player_cards()
|
||||
else:
|
||||
print("\n")
|
||||
player.stand()
|
||||
player_turn = False
|
||||
|
||||
# Dealers turn and showing the cards of both player and dealer.
|
||||
dealer.play(deck)
|
||||
self.show_cards()
|
||||
|
||||
# Comparing hands and returning the winner of the game.
|
||||
os.system("clear")
|
||||
print(self.compare_hands(float(player_bet)))
|
||||
print(f"Player Chips: {player.chips}")
|
||||
print(f"Dealer Chips: {dealer.chips}")
|
||||
|
||||
# After each full round, the players and dealers hand get
|
||||
# cleared and a new deck is created.
|
||||
player.hand.__init__()
|
||||
dealer.hand.__init__()
|
||||
deck.__init__()
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from card import Card
|
||||
from pyjack.card import Card
|
||||
|
||||
|
||||
class Hand:
|
||||
@@ -32,13 +32,3 @@ class Hand:
|
||||
if self.value > 21 and self.aces > 0:
|
||||
self.value -= 10
|
||||
self.aces -= 1
|
||||
|
||||
|
||||
# my_hand = Hand()
|
||||
#
|
||||
# my_hand.add_card(Card("diamond", "K"))
|
||||
# my_hand.add_card(Card("diamond", "8"))
|
||||
# my_hand.add_card(Card("diamond", "A"))
|
||||
#
|
||||
# print(my_hand.value)
|
||||
# print(my_hand.cards)
|
||||
|
||||
@@ -1,9 +1,19 @@
|
||||
from pyjack.player import Player
|
||||
from pyjack.dealer import Dealer
|
||||
from pyjack.card import Card
|
||||
from pyjack.hand import Hand
|
||||
from pyjack.deck import Deck
|
||||
import os
|
||||
from pyjack.game import Game
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("Hello World!")
|
||||
os.system("clear")
|
||||
print("pyjack by cerberus")
|
||||
print("do you wanna play a round??")
|
||||
|
||||
# Create game instance with the player and dealer
|
||||
game = Game()
|
||||
|
||||
# Play the game
|
||||
try:
|
||||
while True:
|
||||
game.play_round()
|
||||
|
||||
except KeyboardInterrupt:
|
||||
print("End Program")
|
||||
|
||||
@@ -1,13 +1,45 @@
|
||||
import pytest
|
||||
|
||||
from pyjack.card import Card
|
||||
from pyjack.hand import Hand
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def default_hand():
|
||||
my_hand = Hand()
|
||||
return my_hand
|
||||
|
||||
# Test 2
|
||||
|
||||
def test_adding_card_to_hand(default_hand):
|
||||
default_hand.add_card("diamonds","2")
|
||||
default_hand.add_card("diamonds","K")
|
||||
default_hand.add_card(Card("diamonds", "2"))
|
||||
default_hand.add_card(Card("spades", "A"))
|
||||
|
||||
assert default_hand.value == 13
|
||||
assert len(default_hand.cards) == 2
|
||||
|
||||
|
||||
def test_adjusting_for_ace(default_hand):
|
||||
"""Tests if the value of an ace is adjusted if the hand holds 1 ace
|
||||
and two cards, making the value of the hand > 21"""
|
||||
|
||||
# Creating a hand with a value pre adjustment of 31
|
||||
cards = [Card("spades", "10"), Card("diamonds", "K"), Card("heart", "A")]
|
||||
for card in cards:
|
||||
default_hand.add_card(card)
|
||||
|
||||
default_hand.adjust_for_aces()
|
||||
|
||||
assert default_hand.value == 21
|
||||
|
||||
|
||||
def testing_adjusting_for_multiple_aces(default_hand):
|
||||
""" "Testing if multiple aces a treated correctly, resulting in the
|
||||
hands value to be 12."""
|
||||
# Creating a hand with a value pre adjustment of 32
|
||||
cards = [Card("spades", "10"), Card("diamonds", "A"), Card("heart", "A")]
|
||||
for card in cards:
|
||||
default_hand.add_card(card)
|
||||
|
||||
default_hand.adjust_for_aces()
|
||||
|
||||
assert default_hand.value == 12
|
||||
|
||||
28
tests/test_player.py
Normal file
28
tests/test_player.py
Normal file
@@ -0,0 +1,28 @@
|
||||
import pytest
|
||||
|
||||
from pyjack.player import Player
|
||||
from pyjack.deck import Deck
|
||||
|
||||
|
||||
# Start Fixtures
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def player() -> Player:
|
||||
"""Fixture for the player test."""
|
||||
player = Player(0)
|
||||
return player
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def deck() -> Deck:
|
||||
deck = Deck()
|
||||
return deck
|
||||
|
||||
|
||||
# Start Tests:
|
||||
|
||||
|
||||
def test_player_hit_one_card(player, deck):
|
||||
player.hit(deck)
|
||||
assert len(player.hand.cards) == 1
|
||||
Reference in New Issue
Block a user