Thursday, January 29, 2015

Cards & Dice - Homework 2 Finished

So last time I posted about my Python homework, I had to created a very simple way to make a deck of cards and shuffle it. For the assignment to be completed, I had to add a dice game to it and have a way for the player to decide on what to do next. Here is my finished homework:

#Assignment002_ChristianMunoz.py
#01/29/2014

import random
import math
import sys
import time

#This function suspens the thread for around 1 second and gives the illusion of loading
def sleeper():
    time.sleep(1.0)
    
#Assigns a random number to the dice
def rollDice():

    tempInt = random.randint(1,6)

    return tempInt

#make the list of cards
def makeListOfCards(stringOfCards):

    listOfCards = stringOfCards.split()

    return listOfCards

#shuffle code
def shuffleDeck(inputDecks):

    shuffledDeck = []

    for i in range(len(inputDecks)):

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

    return(shuffledDeck)

#This function will run the card game
def cardGame():
    
    decksToShuffle = eval(input("Enter how many decks you would like to shuffle at once (1-5):  "))

    #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!")
        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()
        
        print("..")
        sleeper()

        print("...")
        sleeper()
        
        print(shuffleDeck(listOfCards))

        playAgain()

#This function will run the dice game
def diceGame():

    #List of 5 dice
    dice1 = rollDice()
    print(".")
    sleeper()
    
    dice2 = rollDice()
    print("..")
    sleeper()
    
    dice3 = rollDice()
    print("...")
    sleeper()
    
    dice4 = rollDice()
    print("....")
    sleeper()
    
    dice5 = rollDice()
    print(".....")
    sleeper()

    print("You got:", dice1,dice2,dice3,dice4,dice5)

    playAgain()

#This function will run the next game if the player wants to start again
def playAgain():

    decision = input("Would you like to play again? Y/N ")
    
    decision = decision.lower()

    if decision == "yes" or decision == "y":
        main()

    else:
        print("See you later!")
        sleeper()
        sys.exit()

#main function
def main():
    
    playerChoice = eval(input("Press 1 to play the card game or 2 for dice: "))

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

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

    #If the entry was valid, then execute code
    elif playerChoice == 1:
        print("You have chosen the card game!")
        cardGame()

    else:
        print("You have chosen the dice game!")
        diceGame()

#Starts the sequence
main()

I am really proud of this version of the "game". I spent some time making sure that it would work well and that it would look as interesting as possible. 

That's all for now, thanks for reading!

No comments:

Post a Comment