generated from mwc/project_game
added demo game in chris_demo
You had some really clear planning in your commit message-- In the "chris_demo" folder I implemented a bunch of what you planned. I suggest playing the game I built (go into chris_demo and run python game.py), and then carefully reading all the source code. Then, if you like the patterns I used, copy or move whatever you want to use into your main project folder. Also, if there are parts of my code you don't understand, please let me know (in a commit message or in person next time I'm in class) and I'll be happy to explain it.
This commit is contained in:
88
chris_demo/dealer.py
Normal file
88
chris_demo/dealer.py
Normal file
@@ -0,0 +1,88 @@
|
||||
from deck import Deck
|
||||
|
||||
class Dealer:
|
||||
"""The Dealer runs the game, following these rules:
|
||||
|
||||
I have a card class that returns a card value
|
||||
I have a dealer file that has a function that deals the first two cards
|
||||
I'm still a little lost on how this works into the Game class
|
||||
and how to organize the play.
|
||||
I want to:
|
||||
|
||||
1. deal two cards to each dealer and player
|
||||
2. hide one of the dealers cards
|
||||
3. present the user with their cards and let them
|
||||
choose * hit me OR * stay.
|
||||
4. If they hit, deal another card to them and calculate if they
|
||||
went over 21. If they went over 21 end the game with BUST Dealer WINS!.
|
||||
If they didn't go over 21, calculate their score and allow the dealer
|
||||
their turn.
|
||||
5. If they stay, calculate their score and let the dealer have their turn.
|
||||
6. Dealer - checks their cards and compares them to the player. Then
|
||||
chooses to hit or stay. Print choice to screen. If they hit, show the card
|
||||
to the player.
|
||||
"""
|
||||
|
||||
display = False
|
||||
playing = False
|
||||
dealer_origin = (1, 3)
|
||||
player_origin = (1, 5)
|
||||
|
||||
def __init__(self, position):
|
||||
self.position = position
|
||||
self.dealer_cards = []
|
||||
self.player_cards = []
|
||||
|
||||
def play_turn(self, game):
|
||||
if not self.playing:
|
||||
self.set_up_new_round(game)
|
||||
self.playing = True
|
||||
|
||||
def handle_keystroke(self, keystroke, game):
|
||||
game.log(keystroke)
|
||||
if keystroke == "h":
|
||||
self.deal_card_to_player()
|
||||
game.state["score"] = self.get_player_score()
|
||||
|
||||
def set_up_new_round(self, game):
|
||||
self.deck = Deck(self.position)
|
||||
for agent in self.deck.get_agents():
|
||||
game.add_agent(agent)
|
||||
self.deal_card_to_dealer(hidden=True)
|
||||
self.deal_card_to_dealer(hidden=False)
|
||||
self.deal_card_to_player()
|
||||
self.deal_card_to_player()
|
||||
game.state["score"] = self.get_player_score()
|
||||
|
||||
def deal_card_to_dealer(self, hidden=True):
|
||||
card = self.deck.draw()
|
||||
if hidden:
|
||||
card.hide()
|
||||
else:
|
||||
card.show()
|
||||
card.set_position(self.get_position_of_next_dealer_card())
|
||||
self.dealer_cards.append(card)
|
||||
|
||||
def deal_card_to_player(self, hidden=False):
|
||||
card = self.deck.draw()
|
||||
if hidden:
|
||||
card.hide()
|
||||
else:
|
||||
card.show()
|
||||
card.set_position(self.get_position_of_next_player_card())
|
||||
self.player_cards.append(card)
|
||||
|
||||
def get_position_of_next_player_card(self):
|
||||
x, y = self.player_origin
|
||||
return (x + 4 * len(self.player_cards), y)
|
||||
|
||||
def get_position_of_next_dealer_card(self):
|
||||
x, y = self.dealer_origin
|
||||
return (x + 4 * len(self.dealer_cards), y)
|
||||
|
||||
def get_player_score(self):
|
||||
return sum([card.value() for card in self.player_cards])
|
||||
|
||||
def end_round(self, game):
|
||||
for agent in self.deck.get_agents():
|
||||
game.remove_agent(agent)
|
||||
Reference in New Issue
Block a user