Saturday, February 21, 2015

Blackjack & Yahtzee - Homework 5 Finished

So this week's homework was to build upon a couple different homework assignments, mainly making a simple Blackjack game out of the "card game" we made a few weeks ago and to add a way to track your Yahtzee score without losing it between games and without using global variables.

The score for Yahtzee was really simple, but Blackjack was really challenging and I think that I couldn't have done the scoring of cards better since its hard coded at this time and the function has to switch through a bunch of different if statements.

This time around I used Pycharm, which is a really nice IDE for Python. Folding code and having the IDE correct you while you are coding makes it a lot easier than having to dig through a text editor to figure out what you did wrong!

We also had to draw up a design paper that would explain how you build the process of the game. It doesn't have to be perfect, but it was just to illustrate that you put some thought into how we build the game. Here is mine:


Anyways, here is what I made and thanks for reading:



#Assignment005_ChristianMunoz.py
#02/20/2014

import random
import math
import sys
import time


def sleeper(seconds):
    """This function suspends the thread for a given amount of seconds."""
    time.sleep(seconds)


def make_deck():
    """Makes the list of Cards"""
    card_symbols = '''AS AH AD AC
                      KS KH KD KC
                      QS QH QD QC
                      JS JH JD JC
                      10S 10H 10D 10C
                      9S 9H 9D 9C
                      8S 8H 8D 8C
                      7S 7H 7D 7C
                      6S 6H 6D 6C
                      5S 5H 5D 5C
                      4S 4H 4D 4C
                      3S 3H 3D 3C
                      2S 2H 2D 2C'''
    card_symbols = card_symbols.split()
    return card_symbols


def shuffle_deck(input_decks):
    """This function shuffles the deck of cards"""
    random.shuffle(input_decks)

    return input_decks


def roll_die():
    """Assigns a random number to the dice"""
    return random.randint(1, 6)


def make_reroll_list(player_input):
    """Makes the list of which dice to roll"""
    #tempString = tempString.split()

    #return tempString
    return [int(s) for s in player_input.split()]


def score_dice(dice_list, y_score):
    """Score the player's current set of dice."""
    #temp_score = y_score
   
    for i in range(1, 6):
        count_i = dice_list.count(i)
        #checks for Yahtzee
        if count_i == 5:
            y_score += 50
            print("YAHTZEE!\nYour Score is:", y_score, "\n")
            return y_score

        #checks for Four of a Kind
        elif count_i == 4:
            y_score += 25
            print("FOUR OF A KIND!\nYour score is:", y_score, "\n")
            return y_score

        #checks for Full House or Three of a Kind
        elif count_i == 3:
            for j in range(1, 6):
                if dice_list.count(j) == 2:
                    y_score += 30
                    print("FULL HOUSE!\nYour score is:", y_score, "\n")
                    return y_score
            else:
                y_score += 20
                print("THREE OF A KIND!\nYour score is:", y_score, "\n")
                return y_score

    #checks for Small Straight
    small = [1, 2, 3, 4, 5]
    for s in small:
        if s not in dice_list:
            break
    else:
        y_score += 35
        print("SMALL STRAIGHT!\nYour score is:", y_score, "\n")
        return y_score

    #checks for Large Straight
    large = [2, 3, 4, 5, 6]
    for l in large:
        if l not in dice_list:
            break
    else:
        y_score += 40
        print("LARGE STRAIGHT!\nYour score is:", y_score, "\n")
        return y_score

    #this just adds the faces together
    y_score += sum(dice_list)
    print("CHANCE!\nYour score is:", y_score, "\n")
    return y_score


def query_reroll():
    """Ask the use which dice they want to reroll."""
    reroll_list = input("Which die/dice would you like to reroll? Or just press enter to keep what you have.\n")
    reroll_list = make_reroll_list(reroll_list)
    return reroll_list


def reroll_dice(dice_list, reroll_list):
    """Reroll the given dice."""
    for r in reroll_list:
        dice_list[r - 1] = roll_die()
    dice_list.sort()


def deal_card(deck,hand):
    """This function will check if the deck has enough cards and then deal one card.
        In order to stop people from counting cards, this functions shuffles a new
        deck if it goes below 26 cards"""
    if int(len(deck)) > 26:
        hand.append(deck.pop())
        return deck, hand
    else:
        print("The deck is getting low, shuffling a new deck!\n.")
        deck = make_deck()
        deck = shuffle_deck(deck)
        sleeper(.5)
        print("..")
        sleeper(.5)
        print("...\n")
        sleeper(.5)

        print("The deck has been shuffled!\n")
        hand.append(deck.pop())
        return deck, hand


def get_values(hand):
    """Get the value of the cards"""
    aces = 0
    score = 0

    aces += int(hand.count("AS"))
    aces += int(hand.count("AH"))
    aces += int(hand.count("AD"))
    aces += int(hand.count("AC"))

    for card in hand:
        if card == "2S" or card == "2H" or card == "2D" or card == "2C":
            score += 2
        elif card == "3S" or card == "3H" or card == "3D" or card == "3C":
            score += 3
        elif card == "4S" or card == "4H" or card == "4D" or card == "4C":
            score += 4
        elif card == "5S" or card == "5H" or card == "5D" or card == "5C":
            score += 5
        elif card == "6S" or card == "6H" or card == "6D" or card == "6C":
            score += 6
        elif card == "7S" or card == "7H" or card == "7D" or card == "7C":
            score += 7
        elif card == "8S" or card == "8H" or card == "8D" or card == "8C":
            score += 8
        elif card == "9S" or card == "9H" or card == "9D" or card == "9C":
            score += 9
        elif card == "10S" or card == "10H" or card == "10D" or card == "10C":
            score += 10
        elif card == "JS" or card == "JH" or card == "JD" or card == "JC":
            score += 10
        elif card == "QS" or card == "QH" or card == "QD" or card == "QC":
            score += 10
        elif card == "KS" or card == "KH" or card == "KD" or card == "KC":
            score += 10

    if aces == 1 and score <= 10:
        score += 11
    elif aces > 0 and score > 10:
        score += aces

    return score


def blackjack(y_score):
    """This function will run the card game"""
    player_hand = []
    dealer_hand = []

    deck = make_deck()

    print("Shuffling the deck\n.")
    sleeper(.5)
    print("..")
    sleeper(.5)
    print("...\n")
    sleeper(.5)

    deck = shuffle_deck(deck)

    print("The deck has been shuffled, we are ready to start!\n")

    deck, player_hand = deal_card(deck,player_hand)
    deck, dealer_hand = deal_card(deck,dealer_hand)
    deck, player_hand = deal_card(deck,player_hand)
    deck, dealer_hand = deal_card(deck,dealer_hand)

    player_total = get_values(player_hand)
    dealer_total = get_values(dealer_hand)

    print("Your hand is: ", player_hand)
    print("Dealer's top card is: ", dealer_hand[0])

    if player_total == 21:
        print("You got BLACKJACK!\n")
        sleeper(.5)
        print("The dealer has revealed his full hand: ", dealer_hand)
        sleeper(.5)
        if player_total == dealer_total:
            print("\nThis is a DRAW!\n")
            play_again(y_score)
        else:
            print("\nYou WIN!\n")
            play_again(y_score)
    else:
        while player_total <= 21:
            decision = input("Would you like to HIT or STAY? H/S\n").lower()
            if decision == "h":
                print("You have decided to HIT!\n")
                deck, player_hand = deal_card(deck,player_hand)
                player_total = get_values(player_hand)
                print("Your hand is: ", player_hand)
                if player_total > 21:
                    print("BUST!\nYou LOST\n")
                    sleeper(.5)
                    play_again(y_score)
            elif decision == "s":
                print("You have decided to STAY!\n")
                sleeper(.5)
                break

    print("The dealer has revealed his full hand: ", dealer_hand)
    sleeper(1.0)

    if player_total == 21 and player_total == dealer_total:
        print("This is a DRAW!\n")
        play_again(y_score)
    elif dealer_total == 21:
        print("The dealer got BLACKJACK!\nYou LOST!\n")
        play_again(y_score)
    elif dealer_total > 16:
        print("You got: ", player_total)
        print("The dealer got: ", dealer_total)
        if player_total > dealer_total:
            print("\nYou WIN!\n")
            play_again(y_score)
        else:
            print("\nYou LOST\n")
            play_again(y_score)
    else:
        while dealer_total <= 16:
            print("The dealer has to HIT again!\n")
            sleeper(1.0)
            deck, dealer_hand = deal_card(deck,dealer_hand)
            dealer_total = get_values(dealer_hand)
            print("The dealer's hand is: ", dealer_hand)
            if dealer_total > 21:
                print("The dealer got a BUST!\nYou WIN!")
                play_again(y_score)
        print("You got: ", player_total)
        print("The dealer got: ", dealer_total)
        if player_total > dealer_total:
            print("\nYou WIN!\n")
            play_again(y_score)
        else:
            print("\nYou LOST\n")
            play_again(y_score)


def yahtzee(y_score):
    """This function will run the yahtzee game."""
    print(" ")

    dice_list = [roll_die() for i in range(0, 5)]
    dice_list.sort()

    print("First roll you got:", dice_list, "\n")

    reroll_list = query_reroll()
    reroll_dice(dice_list, reroll_list)
       
    print("Second roll you got:", dice_list, "\n")

    reroll_list = query_reroll()
    reroll_dice(dice_list, reroll_list)

    print("Third roll you got:", dice_list, "\n")

    y_score = score_dice(dice_list, y_score)

    play_again(y_score)


def play_again(y_score):
    """This function will run the next game if the player wants to start again"""
    decision = input("Would you like to play again? Y/N\n").lower()

    if decision == "yes" or decision == "y":
        main(False, y_score)
    elif decision == "no" or decision == "n":
        print("See you later!")
    else:
        print("That is not a valid entry!\n")
        play_again(y_score)


def main(first_time, y_score):
    if first_time == True:
        player_input = eval(input("Press 1 to play the Blackjack or 2 for Yahtzee:\n"))

        #This makes sure that  the number will always be an integer
        player_input = math.floor(player_input)

        #This checks to make sure the entry is a valid entry
        if player_input < 1 or player_input > 2:
            print("That is not a valid entry!\n")
            main(True,y_score)

        #If the entry was valid, then execute code
        elif player_input == 1:
            print("You have chosen Blackjack!\n")
            blackjack(y_score)
        else:
            print("You have chosen Yahtzee!")
            yahtzee(y_score)
    else:
        player_input = eval(input("Press 1 to play the Blackjack or 2 for Yahtzee:\n"))

        #This makes sure that the number will always be an integer
        player_input = math.floor(player_input)

        #This checks to make sure the entry is a valid entry
        if player_input < 1 or player_input > 2:
            print("That is not a valid entry!\n")
            main(False, y_score)

        #If the entry was valid, then execute code
        elif player_input == 1:
            print("You have chosen Blackjack!\n")
            blackjack(y_score)
        else:
            print("You have chosen Yahtzee!")
            yahtzee(y_score)


if __name__ == "__main__":
    main(True,0)
    sleeper(1.0)
    sys.exit()


No comments:

Post a Comment