Friday, February 6, 2015

Fibonacci & Dice - Homework 3 Finished

I have finished this homework assignment for this week. This time around we had to make a program that would generated the Fibonacci sequence for however many number the user wanted and we had to make a really simple version of the die game Yahtzee.

I actually used a lot of the same code I made last week, so I didn't have to sit there for a long time making sure the little stuff worked and I could focus on the main logic of Yahtzee since it took longer than I thought it would.

Here is my code:

#Assignment003_ChristianMunoz.py
#02/06/2014

import random
import math
import sys
import time

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

#Assigns a random number to the dice
def rollDice():

    tempInt = random.randint(1,6)

    return tempInt

#This function runs the Fibonacci sequence
def fibonacci():

    finalSequence = []
        
    aInt = 0
    bInt = 1
    count = 0

    #Player decides how many digits of the fibonacci sequence they want
    amount = eval(input('How many digits of the sequence do you want? (At least 2) '))

    print(" ")

    #This checks to makes sure the number will always be an interger
    amount = math.floor(amount)

    #This checks to make sure its a valid entry
    if amount < 2:
        print("That is not a valid entry!")

        print(" ")
        
        fibonacci()

    #If the player just wants to see 2 digits then we print them right away
    elif amount == 2:
        finalSequence.append(aInt)
        finalSequence.append(bInt)

        print(finalSequence)

    #If the entry is valid, then execute code
    else:

        finalSequence.append(aInt)
        finalSequence.append(bInt)

        while (count < amount - 2):

            tempInt = aInt + bInt

            aInt = bInt
            bInt = tempInt

            finalSequence.append(tempInt)

            count = count + 1

        print(finalSequence)

    print(" ")

    playAgain()

#Makes the list of which to roll
def makeListOfDice(tempString):

    tempString = tempString.split()

    return tempString

#This function scores the player's current set of dice
def score(tempString, finalScore):

    #checks for Yahtzee
    if tempString.count(6) == 5 or tempString.count(5) == 5 or tempString.count(4) == 5 or tempString.count(3) == 5 or tempString.count(2) == 5 or tempString.count(1) == 5:

        finalScore += 50
        print("YAHTZEE!")
        print("Your Score is:",finalScore)
        print(" ")

    #checks for Full House or Three of a Kind
    elif tempString.count(6) == 3 or tempString.count(5) == 3 or tempString.count(4) == 3 or tempString.count(3) == 3 or tempString.count(2) == 3 or tempString.count(1) == 3:

        if tempString.count(1) == 2 or tempString.count(2) == 2 or tempString.count(3) == 2 or tempString.count(4) == 2 or tempString.count(5) == 2 or tempString.count(6) == 2:

            finalScore += 30
            print("FULL HOUSE!")
            print("Your score is:",finalScore)
            print(" ")

        else:

            finalScore += 20
            print("THREE OF A KIND!")
            print("Your score is:",finalScore)
            print(" ")

    #chekcs for Four of a Kind
    elif tempString.count(6) == 4 or tempString.count(5) == 4 or tempString.count(4) == 4 or tempString.count(3) == 4 or tempString.count(2) == 4 or tempString.count(1) == 4:

        finalScore += 25
        print("FOUR OF A KIND!")
        print("Your score is:",finalScore)
        print(" ")

    #checks for Small Straight
    elif tempString[0] == 1 and tempString[1] == 2 and tempString[2] == 3 and tempString[3] == 4 and tempString[4] == 5:

        finalScore += 35
        print("SMALL STRAIGHT!")
        print("Your score is:",finalScore)
        print(" ")

    #checks for Large Straight
    elif tempString[0] == 2 and tempString[1] == 3 and tempString[2] == 4 and tempString[3] == 5 and tempString[4] == 6:

        finalScore += 40
        print("LARGE STRAIGHT!")
        print("Your score is:",finalScore)
        print(" ")

    #this just adds the faces together
    else:

        finalScore += tempString[0]
        finalScore += tempString[1]
        finalScore += tempString[2]
        finalScore += tempString[3]
        finalScore += tempString[4]

        print("CHANCE!")
        print("Your score is:", finalScore)
        print(" ")
    
#This function will run the yahtzee game
def yahtzee():

    print(" ")

    diceList = []
    finalScore = 0

    for i in range(0,5):
        diceList.append(rollDice())

    diceList.sort()

    print("First roll you got:",diceList)

    print(" ")

    whichList = input("Which die/dice would you like to reroll? Or just press enter to keep what you have. ")

    print(" ")

    whichList = makeListOfDice(whichList)

    for i in whichList:

        diceList[int(i) - 1] = rollDice()

    diceList.sort()
        
    print("Second roll you got:",diceList)

    print(" ")

    whichList = input("Which die/dice would you like to reroll? Or just press enter to keep what you have. ")

    print(" ")

    whichList = makeListOfDice(whichList)

    for i in whichList:

        diceList[int(i) - 1] = rollDice()

    diceList.sort()
        
    print("Third roll you got:",diceList)
    
    print(" ")

    score(diceList, finalScore)

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

    print(" ")
    
    decision = decision.lower()

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

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

#main function
def main():
    
    playerChoice = eval(input("Press 1 to see the Fibonacci Sequence or 2 for Yahtzee: "))

    print(" ")

    #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 < 1 or playerChoice > 2:
        print("That is not a valid entry!")

        print(" ")
        
        main()

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

        print(" ")
        
        fibonacci()

    else:
        print("You have chosen Yahtzee!")
        
        yahtzee()

#Starts the sequence
main()

These files are getting bigger and bigger! But I'm sure that there are a few parts where I could have used a for loop instead of hard coding it.

That's all for now, thanks for reading!

No comments:

Post a Comment