Compare commits

..

15 Commits

Author SHA1 Message Date
e2c99dfea4 made game playable 2025-11-08 21:31:36 +01:00
d57022f742 cleanup 2025-11-06 20:00:39 +01:00
a3aa761739 added test for Player() 2025-11-06 20:00:22 +01:00
4222e76262 added exception for keyboard interrupt 2025-11-06 19:44:05 +01:00
8c3dbc8fcd added comments 2025-11-06 19:40:47 +01:00
acd5fa7201 rearranged clearing of the hand and terminal 2025-10-29 21:23:08 +01:00
a0175343f9 clean loop with cleared hand 2025-10-29 21:13:30 +01:00
197572567a made it functional 2025-10-29 21:01:03 +01:00
a4cdd72ac9 removed comments 2025-10-29 18:50:06 +01:00
4ae994b554 added Hand() tests 2025-10-29 18:48:52 +01:00
7979767b3d fixed import 2025-10-29 18:48:20 +01:00
4043e47208 removed bs 2025-10-29 17:49:42 +01:00
900304c477 sd 2025-10-29 17:48:33 +01:00
6bc4c60ba6 sd 2025-10-29 17:48:16 +01:00
f7b80262f4 ugz 2025-10-29 17:43:01 +01:00
6 changed files with 161 additions and 39 deletions

View File

@@ -30,15 +30,3 @@ class Deck:
"""Removes the last card off the deck and returns it.""" """Removes the last card off the deck and returns it."""
card = self.cards.pop() card = self.cards.pop()
return card 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())

View File

@@ -1,4 +1,4 @@
import re import os
from pyjack.dealer import Dealer from pyjack.dealer import Dealer
from pyjack.deck import Deck from pyjack.deck import Deck
from pyjack.player import Player from pyjack.player import Player
@@ -8,14 +8,11 @@ class Game:
"""The Game class is the central coordinator, managing the flow of """The Game class is the central coordinator, managing the flow of
the entire round, from setup to payout.""" the entire round, from setup to payout."""
# TODO: - play_round() function
# - show_cards function
def __init__(self) -> None: def __init__(self) -> None:
"""Creates instances of 'Deck', 'Player', and 'Dealer'.""" """Creates instances of 'Deck', 'Player', and 'Dealer'."""
self.deck = Deck() self.deck = Deck()
self.player = Player(0) self.player = Player(100)
self.dealer = Dealer(1000) self.dealer = Dealer(10000)
def compare_hands(self, bet: float) -> str: def compare_hands(self, bet: float) -> str:
"""*Resolution/Payout*: Compares final hand values, checks for """*Resolution/Payout*: Compares final hand values, checks for
@@ -65,18 +62,95 @@ class Game:
second card is shown or concealed.""" second card is shown or concealed."""
player_cards = self.player.hand.cards player_cards = self.player.hand.cards
player_hand = self.player.hand
dealer_cards = self.dealer.hand.cards dealer_cards = self.dealer.hand.cards
dealer_hand = self.dealer.hand
print("--Player Cards:--") print("--Player Cards:--")
for card in player_cards: for card in player_cards:
print(str(card)) print(str(card))
print(f"Hand value: {player_hand.value}")
if hide_dealer: if hide_dealer:
card = dealer_cards[0] card = dealer_cards[0]
print("--Dealer Cards:--") print("--Dealer Cards:--")
print(str(card)) print(str(card))
print("XXXX") print("XXXX\n")
else: else:
print("--Dealer Cards:--") print("--Dealer Cards:--")
for card in dealer_cards: for card in dealer_cards:
print(str(card)) 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__()

View File

@@ -1,4 +1,4 @@
from card import Card from pyjack.card import Card
class Hand: class Hand:
@@ -32,13 +32,3 @@ class Hand:
if self.value > 21 and self.aces > 0: if self.value > 21 and self.aces > 0:
self.value -= 10 self.value -= 10
self.aces -= 1 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)

View File

@@ -1,9 +1,19 @@
from pyjack.player import Player import os
from pyjack.dealer import Dealer
from pyjack.card import Card
from pyjack.hand import Hand
from pyjack.deck import Deck
from pyjack.game import Game from pyjack.game import Game
if __name__ == "__main__": 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")

View File

@@ -1,13 +1,45 @@
import pytest import pytest
from pyjack.card import Card
from pyjack.hand import Hand from pyjack.hand import Hand
@pytest.fixture @pytest.fixture
def default_hand(): def default_hand():
my_hand = Hand() my_hand = Hand()
return my_hand return my_hand
# Test 2
def test_adding_card_to_hand(default_hand): def test_adding_card_to_hand(default_hand):
default_hand.add_card("diamonds","2") default_hand.add_card(Card("diamonds", "2"))
default_hand.add_card("diamonds","K") 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
View 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