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
2 changes: 2 additions & 0 deletions src/cards/deck.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ def shuffle(self):
self.needs_shuffling = False

def getCard(self) -> Card:
print("[Deck::getCard()]")
self.top_card_index += 1
if self.top_card_index == self.red_card_index:
# The deck needs to be shuffled
self.needs_shuffling = True
print("[Deck::getCard()] -> deck need to be shuffled")
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Python style !?


return Card(*self.cards[self.top_card_index])

Expand Down
Empty file added src/controller/__init__.py
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ def add_card(self, card: Card, human_type: str, area_id: int = None):
:param Card card: the card we want to add to the organizer
:param str human_type: either "dealer" or "player"
:param int area_id: only for "player" human_type, specifies the id
of the area to add the card to
of the area to add the card to is linked to the hand of the player
:return: None
"""
card_tile = self._create_card_tile(card)
if human_type == "dealer":
if human_type.lower() == "dealer":
self._add_dealer_card(card_tile)
elif human_type == "player":
elif human_type.lower() == "player":
if area_id is None:
raise ValueError("Missing area_id for adding a player card")
elif area_id > len(self.player_areas):
Expand Down
51 changes: 44 additions & 7 deletions src/controller/game_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from src.humans.player import Player
from src.cards.deck import Deck
from src.common.constants import Decision
from src.controller.card_area_organizer import CardAreaOrganizer

import pygame
from pygame.locals import (
Expand Down Expand Up @@ -43,6 +44,19 @@ def __init__(self, window):
self.hand_idx = None
# self.view_game = View_game(window, view_config)

# Organizers
self.card_area_organizer = CardAreaOrganizer()

self.organizers = (
self.card_area_organizer,
)

self.subscribe_to_organizers()

def subscribe_to_organizers(self):
for organizer in self.organizers:
organizer.areas_updated.attach(self.refresh)

def game_launch(self):
self.view_game = ViewGame(self.window)
self.view_game.buttons["card"].signal.attach(self.btn_card)
Expand All @@ -67,7 +81,7 @@ def initiate_players(self):
self.add_human(Player(name_of_player, self.player_wallet))

def refresh(self):
self.view_game.refresh()
self.view_game.refresh(self.card_area_organizer)

def enable_buttons(self, *btn_names):
"""
Expand Down Expand Up @@ -131,6 +145,7 @@ def first_round(self):
for human in self.humans_list:
human.clear_hands()
self.dealer.clear_hand()
self.card_area_organizer.clear_areas()

# Set buttons state
self.enable_buttons("bet", "quit")
Expand Down Expand Up @@ -165,13 +180,22 @@ def first_round(self):
for self.human in self.humans_list:
print(self.human.name + "is receiving cards.")
# at initialization we only change the first hand of the player with 2 cards
self.human.add_card(self.deck.getCard(), 0)
self.human.add_card(self.deck.getCard(), 0)
human_card = self.deck.getCard()
human.add_card(human_card, 0)
self.card_area_organizer.add_card(human_card, "player", 0)

human_card = self.deck.getCard()
human.add_card(self.deck.getCard(), 0)
self.card_area_organizer.add_card(human_card, "player", 0)

# giving the dealer a hand (with 2 card)
self.dealer.add_card(self.deck.getCard())
self.dealer.add_card(self.deck.getCard())
print("End bet_round")
dealer_card = self.deck.getCard()
self.dealer.add_card(dealer_card)
self.card_area_organizer.add_card(dealer_card, "dealer", 0)

dealer_card = self.deck.getCard()
self.dealer.add_card(dealer_card)
self.card_area_organizer.add_card(dealer_card, "dealer", 0)
return True

def play_one_round(self):
Expand Down Expand Up @@ -222,7 +246,9 @@ def play_one_round(self):
while dealer_decision != Decision.stand:
print(dealer_decision)
if dealer_decision == Decision.hit:
self.dealer.add_card(self.deck.getCard())
dealer_card = self.deck.getCard()
self.dealer.add_card(dealer_card)
self.card_area_organizer.add_card(dealer_card, "dealer", 0)
print("Dealer hand : " + str(self.dealer.hand))
dealer_decision = self.dealer.choose_action()

Expand Down Expand Up @@ -259,6 +285,9 @@ def btn_card(self):
print("Is burnt or black jack")
self.playing = False

print("add card into card organizer for display purpose")
self.card_area_organizer.add_card(card, "player", self.hand_idx)

print("btn_card")

def btn_bet(self):
Expand Down Expand Up @@ -316,3 +345,11 @@ def btn_quit(self):
self.quit = True

print("btn_quit")

@classmethod
def get_card_organizer(cls):
"""
give the card organizer
:return: CardAreaOrganizer
"""
return cls.card_area_organizer
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No newline at end of file

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No newline at end of file

2 changes: 1 addition & 1 deletion src/views/tests/test_card_area_organizer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from src.cards.card import Card
from src.common.game_view_config import game_view_config
from src.views.card_area_organizer import CardAreaOrganizer
from src.controller.card_area_organizer import CardAreaOrganizer

# override method so we don't have to initialize a pygame window
CardAreaOrganizer._create_card_tile = lambda _, __: None
Expand Down
36 changes: 11 additions & 25 deletions src/views/view_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,16 @@
import pygame

from src.button import Button
from src.cards.card import Card
from src.common.constants import CONFIG_GAME_VIEW
from src.common.func_pictures import load_image
from src.common.game_view_config import game_view_config
from src.views.card_area_organizer import CardAreaOrganizer
from src.views.image_loaders.tokensloader import TokensLoader


class ViewGame:
def __init__(self, window):
self._area_counts = defaultdict(int)

# Organizers
self.card_area_organizer = CardAreaOrganizer()
self.organizers = (
self.card_area_organizer,
)

self.subscribe_to_organizers()

# Buttons
self.buttons = dict()

Expand All @@ -31,20 +21,16 @@ def __init__(self, window):
self.background = None # init in init_window
self.init_window()

def subscribe_to_organizers(self):
for organizer in self.organizers:
organizer.areas_updated.attach(self.refresh)

def init_window(self):
# Init the window with background
bgd_tile = load_image("green_carpet.jpeg")
bgd_tile = pygame.transform.scale(
bgd_tile, (game_view_config.window.width, game_view_config.window.height)
background_tile = load_image("green_carpet.jpeg")
background_tile = pygame.transform.scale(
background_tile, (game_view_config.window.width, game_view_config.window.height)
)
self.background = pygame.Surface(
(game_view_config.window.width, game_view_config.window.height)
)
self.background.blit(bgd_tile, (0, 0))
self.background.blit(background_tile, (0, 0))

# Display on windows
self.window.blit(self.background, (0, 0))
Expand All @@ -60,10 +46,6 @@ def init_window(self):
# # Update the scene
# dirty = all_sprites.draw(self.window)
# pygame.display.update(dirty)
self.card_area_organizer.add_card(Card("KING"), "dealer")
self.card_area_organizer.add_card(Card("QUEEN"), "dealer")

self.card_area_organizer.add_card(Card("ACE", "SPADES"), "player", 0)

def init_game_btns(self):
# Display game buttons area
Expand All @@ -83,17 +65,21 @@ def init_game_btns(self):
b.draw()
self.buttons[iid] = b

def refresh(self):
def refresh(self, card_area_organizer):
"""
:param card_area_organizer: organizer that keep place and value of all card
:return:
"""
print("Updating View...")
# Init sprites
sprite_group = pygame.sprite.Group()

self.window.blit(self.background, (0, 0))
self.init_game_btns()

for el in self.card_area_organizer.dealer_area:
for el in card_area_organizer.dealer_area:
self.window.blit(el.surface, el.position.as_tuple)
for area in self.card_area_organizer.player_areas:
for area in card_area_organizer.player_areas:
for el in area:
self.window.blit(el.surface, el.position.as_tuple)

Expand Down