Wednesday, February 18, 2015

A Blackjack of all Trades

So this week we have build upon the homework we have done on previous weeks and make a text based Blackjack game along with our Yahtzee game that was build before.

The assignment specifies that for the Yahtzee game we have to find a way to make the game memorize the player's total score for all the Yahtzee games he/she has played without using variable.

I know that I will have to go back and make a few changes, but for now this is what I have and thanks for reading:



#Assignment005_ChristianMunoz.py
#02/18/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 makeListOfCards(stringOfCards):
    """Makes the list of Cards"""
    listOfCards = stringOfCards.split()

    return listOfCards


def shuffleDeck(inputDecks):
    """Shuffle code"""
    shuffledDeck = []

    for i in range(len(inputDecks)):

        tempInt = random.randint(0,len(inputDecks) - 1)
        shuffledDeck.append(inputDecks.pop(tempInt))

    return(shuffledDeck)


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:
            temp_score += 50
            print("YAHTZEE!\nYour Score is:", temp_score, "\n")
            return temp_score

        #checks for Four of a Kind
        elif count_i == 4:
            temp_score += 25
            print("FOUR OF A KIND!\nYour score is:", temp_score, "\n")
            return temp_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:
                    temp_score += 30
                    print("FULL HOUSE!\nYour score is:", temp_score, "\n")
                    return temp_score
            else:
                temp_score += 20
                print("THREE OF A KIND!\nYour score is:", temp_score, "\n")
                return temp_score

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

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

    #this just adds the faces together
    temp_score += sum(dice_list)
    print("CHANCE!\nYour score is:", temp_score, "\n")
    return temp_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 blackjack():
    """This function will run the card game"""
    decksToShuffle = eval(input("Enter how many decks you would like to shuffle at once (1-5):  "))

    print(" ")

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

    #This checks to make sure the entry is a valid entry
    if decksToShuffle <= 0 or decksToShuffle > 5:
        print("That is not a valid entry!")

        print(" ")
       
        cardGame()

    #If the entry was valid, then execute code
    else:
        CardSymbols = '''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'''
       
        listOfCards = makeListOfCards(CardSymbols) * decksToShuffle

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

        print("...")
        sleeper(.5)

        print(" ")
       
        print(shuffleDeck(listOfCards))

        print(" ")

        play_again()


def yahtzee(y_score):
    """This function will run the yahtzee game."""
    temp_score = y_score
    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")

    temp_score = score_dice(dice_list, temp_score)

    play_again(temp_score)


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

    if decision == "yes" or decision == "y":

       player_input = eval(input("Press 1 to play the Blackjack or 2 for Yahtzee:\n"))

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


def main(first_time):
   
    y_score = 0

    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()

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


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

No comments:

Post a Comment