dimanche 8 mars 2015

Rock, Paper, Scissors style game (Cowboy, Ninja, Bear)

In our assignment found here we are creating a game called Cowboy, Ninja, Bear which is essentially Rock, Paper, Scissors. So I have two questions.


1.) How do I assign c, n, or b to the random number generated by the program of 1, 2, or 3?


2.) Is there a quick and easy way to make sure that N is equal to n in order to shorten the code when comparing the program's choice and the user's choice? So it only has to compare between c, b, and n instead of throwing C, B, and N into the loop as well?



"""
Author:
Program: cnb.py
Description: A game of Cowboy, Ninja, Bear. Similar to Rock, Paper, Scissors.
The user picks either Ninja, Cowboy, or Bear. The program compares this against
its own randomly generated choice and replys with the rounds outcome. The program
also keeps track of the number of wins, losses, ties, and overall rounds.
"""

import random
random.seed()

counter = 1
winCounter = 0
loseCounter = 0
tieCounter = 0

#Input Rules

print ("Enter:")
print (" 'C' or 'c' for Cowboy")
print (" 'N' or 'n' for Ninja")
print (" 'B' or 'b' for Bear")
print()


#Prompt user for input

print ("Round", counter, "Fight!")
userStr = input("Please enter a weapon: ")



#If user input is not compliant with rules instruct
#user to retry.

if userStr == "q" or userStr == "Q":
print("Game Over!")
if userStr != "N" or userStr != "n" or userStr != "C" or userStr != "c" or userStr != "B" or userStr != "b" or userStr == "q" or userStr == "Q":
print ()
print ("That's not a valid choice!")
userStr = input("Please enter a weapon: ")

#Computer picks Weapon

computer = random.randint(1,3)



#Compare Results and print results

#Win
if userStr == c and computer == c
winCounter = winCounter + 1
print ("You win")
if userStr == n and computer == n
winCounter = winCounter + 1
print ("You win")
if userStr == b and computer == b
winCounter = winCounter + 1
print ("You win")

#Loss
if userStr == c and computer == c
lossCounter = lossCounter + 1
print ("You lose")
if userStr == n and computer == n
lossCounter = lossCounter + 1
print ("You lose")
if userStr == b and computer == b
lossCounter = lossCounter + 1
print ("You lose")

#Tie
if userStr == c and computer == c
tieCounter = tieCounter + 1
print ("You tied")
if userStr == n and computer == n
tieCounter = tieCounter + 1
print ("You tied")
if userStr == b and computer == b
tieCounter = tieCounter + 1
print ("You tied")



#Loop to new round

counter = counter + 1
print()
print ("Round", counter)
userStr = input("Please enter a weapon: ")




Aucun commentaire:

Enregistrer un commentaire