Sunday, May 18, 2014

Fun with Python: Rock-Paper-Scissors-Lizard-Spock


Rock-paper-scissors is a hand game that is played by two people. The players count to three in unison and simultaneously 'throw' one of three hand signals that correspond to rock, paper or scissors. The winner is determined by the rules:
·         Rock smashes scissors
·         Scissors cuts paper
·         Paper covers rock
Rock-paper-scissors is a surprisingly popular game that many people play seriously (see the Wikipedia article for details). Due to the fact that a tie happens around 1/3 of the time, several variants of Rock-Paper-Scissors exist that include more choices to make ties more unlikely.
Rock-paper-scissors-lizard-Spock (RPSLS) is a variant of Rock-paper-scissors that allows five choices. Each choice wins against two other choices, loses against two other choices and ties against itself. Much of RPSLS's popularity is that it has been featured in 3 episodes of the TV series "The Big Bang Theory". The Wikipedia entry for RPSLS gives the complete description of the details of the game.
In our first mini-project, we will build a Python function rpsls(name) that takes as input the string name, which is one of "rock","paper""scissors""lizard", or "Spock". The function then simulates playing a round of Rock-paper-scissors-lizard-Spock by generating its own random choice from these alternatives and then determining the winner using a simple rule that we will next describe.
While Rock-paper-scissor-lizard-Spock has a set of ten rules that logically determine who wins a round of RPSLS, coding up these rules would require a large number (5x5=25) of if/elif/else clauses in your mini-project code. A simpler method for determining the winner is to assign each of the five choices a number:
·         0 — rock
·         1 — Spock
·         2 — paper
·         3 — lizard
·         4 — scissors
In this expanded list, each choice wins against the preceding two choices and loses against the following two choices.
Mini-project development process
1.    Build a helper function name_to_number(name) that converts the string name into a number between 0 and 4 as described above. This function should use a sequence of if/elif/else clauses. You can use conditions of the form name == 'paper', etc. to distinguish the cases.To make debugging your code easier, we suggest including a final else clause that catches cases whenname does not match any of the five correct input strings and prints an appropriate error message.
2.    Next, you should build a second helper function number_to_name(num) that converts a number in the range 0 to 4 into its corresponding name as a string. Again, we suggest including a final else clause that catches cases when number is not in the correct range.
3. Build the first part of the main function rpsls(name) that converts name into the number player_number between 0 and 4 using the helper function name_to_number.
4.    Build the second part of rpsls(name) that generates a random number comp_number between 0 and 4 using the functionrandom.randrange(). I suggest experimenting with randrange in a separate CodeSkulptor window before deciding on how to call it to make sure that you do not accidently generate numbers in the wrong range.
5.    Build the last part of rpsls(name) that determines and prints out the winner. This test is actually very simple if you use the remainder operation (% in Python) to the difference between comp_number and player_number. If this is not immediately obvious to you, I would suggest reviewing the "More operations"  and "RPSLS" videos on remainders and modular arithmetic as well as experimenting with the remainder operator % in a separate CodeSkulptor window to understand its behavior.
6.    Using the helper function number_to_name, you should produce four print statements; print a blank line, print out the player's choice, print out the computer's choice and print out the winner.

This will be the only mini-project in the class that is not an interactive game. Since we have not yet learned enough to allow you to play the game interactively, you will simply call your rpsls function repeatedly in the program with different player choices. You will see that we have provided five such calls at the bottom of the template. Running your program repeatedly should generate different computer guesses and different winners each time. While you are testing, feel free to modify those calls, but make sure they are restored when you hand in your mini-project, as your peer assessors will expect them to be there.
The output of running your program should have the following form:
Player chooses rock
Computer chooses scissors
Player wins!

Player chooses Spock
Computer chooses lizard
Computer wins!

Player chooses paper
Computer chooses lizard
Computer wins!

Player chooses lizard
Computer chooses scissors
Computer wins!

Player chooses scissors
Computer chooses Spock
Computer wins!

Tuesday, May 6, 2014

Fun with Python : Magic Eight Ball

import random
import sys

answers = (["GET THE FUCK OFF, I'M NOT ANSWERING THAT!",
            "ARE YOU HIGH? NAH!! NOT GONNA HAPPEN, BRO!"
            "OF COURSE. YOU ARE A CUTIE PIE!"
            "FUCK YOU! YOUR STUPIDITY LEVEL IS OVER 9000."
            "HELL, YEAH!"
            "FUCKING NO."
            "DEFINITELY."
            "THERE'S A 50% CHANCE"
            "FLIP A COIN. NOW GO AWAY!"
            "DON'T COUNT ON IT."
            "TAKE A DUMP AND THEN ASK AGAIN, MAYBE?"
            "IF IT'S MEANT TO BE, IT'S MEANT TO BE."
])

print("WELCOME TO MAGIC FUCKING EIGHT BALL!!")
print("WHAT THE HELL IS YOUR QUESTION?")
question = input()

fuck = 1
while fuck == 1:
    x = random.choice(answers)
    print("MY ANSWER IS: " , x)
    print("IF YOU WANT TO EXIT, TYPE QUIT OR ASK AGAIN")
    question = input()
    if question == 'quit':
        quit = sys.exit("DIE ALONE, YOU MORON!")
    if question == 'QUIT':
        quit = sys.exit(["DIE ALONE, YOU MORON!"])

Fun with Python: Hangman Game

import random
#We should use the random library to choose a random word.
#Add "import random" to the top of your program
#Change the line that sets up the secret so that it looks like below
#The square brackets [ ] and commas make a list
#And the random.choice picks one thing randomly from the list.

#If you need to load a List from the Internet, follow the three steps below
#import urllib.request
#animals = urllib.request.urlopen('http://davidbau.com/data/animals') .read() .decode("utf8") .split()
#secret = random.choice(animals)


print("LET'S PLAY HANGMAN, BITCHES. OR DIE!!")
secret = random.choice(['elephant', 'lion', 'horse', 'mouse', 'tiger', 'panda', 'turtle','beaver'])
guesses = 'aeiou'
turns = 7

while turns > 0:
    missed = 0
    for letter in secret:
        if letter in guesses:
            print(letter, end=" ")
        else:
            print('_', end=" ")
            missed +=1

           
#You need to indent everything under the "while" command to make this work.
#So you will need to add some spaces in front of most of your program.

#Let's also move the guessing after the hint instead of before.

#The command "turns -= 1" means subtract one from "turns," so if it used to be 5, it will be 4.
#Then the next time around it will be 3 and so on. When turns is finally zero, the "while" command will stop repeating.

    print

    if missed == 0:
#The "missed" number counts how many blanks still missing. If it is zero, it means we won.
        print("YOU WIN, DUMBASS!!")
        break #And the "break" command breaks out of the "while" section early

    guess = input("GUESS A LETTER, YOU MORON: ")
#To let the player guess we will use a function called "input()"
    guesses += guess

    #The 'guesses += guess' line adds the guess to the string of guesses.
   #If the string of guesses was "aeiou" and the new guess is "c", then the string of guesses will become "aeiouc"

    if guess not in secret:
        turns -=1
        print("NOOOOOOOOO!!")
        print(turns, "MORE TURNS")
        if turns < 7: print("   0   ")
        if turns < 6: print("   |   ")
        if turns < 5: print(" \_|_/ ")
        if turns < 4: print("   |   ")
        if turns < 3: print("   |   ")
        if turns < 2: print("  / \  ")
        if turns < 1: print(" d   b ")
        if turns == 0:
            print("YOU ARE AN IDIOT! THE ANSWER IS", secret)
#The "if guess not in secret" line checks if the guess was wrong.
#We only count down the "turns" if our guess was wrong
#When we guess wrong, we also print a bunch of stuff like "Nooooooo!!"
#Along with how many more turns we have.
#When we are wrong for the last time, we print the secret.