This next assignment is to make a game similar to rock, paper, scissors but with memory and it is called Cowboy, Ninja, Bear.
The first thing I will do is import the random number generator.
import random random.seed()
#Second I will print what the program does and the options available to the user.
print(" ___ _ _ ___ ") print("/ _) | \| | | - ) ") print(" (_ _ | \ | _ | _ \ _") print("\___)(_)|_\__|(_)|___/(_) ") print() print("Welcome to Cowboy, Ninja, Bear!") print() print("The Ninja beats the Cowboy using lighting speed ninja kicks.") print() print("The Cowboy beats the Bear with his quick draw and perfect accuracy.") print() print("The Bear beats the Ninja with a strong swipe of his clawed paw.") print("") print("Type N to choose the Ninja.") print("") print("Type C to choose the Cowboy.") print("") print("Type B to choose the Bear.") print("") print("Type Q to quit the program.") print() #Third I will create the counters I will use for the program including an AI.
roundCounter = 0 tieCounter = 0
#Win counters
computerWin = 0 userWin = 0
#User choice counters, beginning at one to prevent multiplication by zero.
userCowboy = 1 userNinja = 1 userBear = 1
#Stomper counters, used to prevent the user from figuring out what the program, #will pick an keep picking the opposite.
ninjaStomper = 0 cowboyStomper = 0 bearStomper = 0
#Fourth I will assign the computer a random number between from one to three.
while True: computer = random.randint(1,3) #Fifth I will ask for user input within a loop. user = input("Please enter your choice, N for Ninja, C for Cowboy, B for Bear. ")
#Sixth I will convert any capitalized letters to lowercase.
user = user.lower() #Seventh I will convert user input into an integer. if user == "n": user = 1 elif user == "c": user = 2 elif user == "b": user = 3
elif user=="q": break #Eighth I will make sure input is valid. else: print("Invalid input, Enter N for Ninja, C for Cowboy, or B for Bear. ")
#Ninth I will add in an AI and use counters to determine computers choice. if userBear > userCowboy: computer = 2
if userCowboy > userNinja: computer = 1
if userNinja > userBear: computer = 3
#Tenth I will use the stompers if they keep picking the same choice.
if ninjaStomper > 2: computer = 3 if ninjaStomper > 7: ninjaStomper = 1 bearStomper = 0 cowboyStomper = 0 if ninjaStomper < 0: ninjaStomper = 1 if cowboyStomper > 2: computer = 1 if cowboyStomper > 7: cowboyStomper = 1 ninjaStomper = 0 bearStomper = 0 if cowboyStomper < 0: cowboyStomper = 1
if bearStomper > 2: computer = 2 if bearStomper > 7: bearStomper = 1 cowboyStomper = 0 ninjaStomper = 0 if bearStomper < 0: bearStomper = 1
#Eleventh I will print the results accordingly and keep track of counters. if user == computer: tieCounter = tieCounter +1 roundCounter = roundCounter +1 print ("The game ended in a tie. You chose the same as the computer.") print ("No one won round:",roundCounter,"with",userWin,"WINS and",computerWin,"LOSSES and",tieCounter,"ties.") print("-------------------------------------------------------------------") print()
elif user==1 and computer==2: roundCounter = roundCounter +1 userWin = userWin +1 userNinja = userNinja +1 ninjaStomper = ninjaStomper +1 cowboyStomper = cowboyStomper -1 print ("The Ninja beat the Cowboy using lighting speed ninja kicks.") print ("You won round:",roundCounter,"with",userWin,"WINS and",computerWin,"LOSSES and",tieCounter,"ties.") print("-------------------------------------------------------------------") print() elif user==1 and computer==3: roundCounter = roundCounter +1 computerWin = computerWin +1 userNinja = userNinja +1 ninjaStomper = ninjaStomper +1 bearStomper = bearStomper -1 print ("The Bear beat the Ninja with a strong swipe of his clawed paw.") print ("You lost round:",roundCounter,"with",userWin,"WINS and",computerWin,"LOSSES and",tieCounter,"ties.") print("-------------------------------------------------------------------") print()
elif user==2 and computer==1: roundCounter = roundCounter +1 computerWin = computerWin +1 userCowboy = userCowboy +1 ninjaStomper = ninjaStomper -1 cowboyStomper = cowboyStomper +1 bearStomper = bearStomper -1 print ("The Ninja beat the Cowboy using lighting speed ninja kicks.") print ("You lost round:",roundCounter,"with",userWin,"WINS and",computerWin,"LOSSES and",tieCounter,"ties.") print("-------------------------------------------------------------------") print() elif user==2 and computer==3: roundCounter = roundCounter +1 userWin = userWin +1 userCowboy = userCowboy +1 cowboyStomper = cowboyStomper +1 bearStomper = bearStomper -1 print ("The Cowboy beat the Bear with his quick draw and perfect accuracy.") print ("You won round:",roundCounter,"with",userWin,"WINS and",computerWin,"LOSSES and",tieCounter,"ties.") print("-------------------------------------------------------------------") print() elif user==3 and computer==2: roundCounter = roundCounter +1 computerWin = computerWin +1 userBear = userBear +1 cowboyStomper = cowboyStomper -1 bearStomper = bearStomper +1 print ("The Cowboy beat the Bear with his quick draw and perfect accuracy.") print ("You lost round:",roundCounter,"with",userWin,"WINS and",computerWin,"LOSSES and",tieCounter,"ties.") print("-------------------------------------------------------------------") print()
elif user==3 and computer==1: roundCounter = roundCounter +1 userWin = userWin +1 userBear = userBear +1 ninjaStomper = ninjaStomper -1 bearStomper = bearStomper +1 print ("The Bear beat the Ninja with a strong swipe of his clawed paw.") print ("You won round:",roundCounter,"with",userWin,"WINS and",computerWin,"LOSSES and",tieCounter,"ties.") print("-------------------------------------------------------------------") print()
#Finally I will print that the user chose to exit.
print("------------------------------------------------------------------") print ("Thank you for playing Cowboy, Ninja, Bear!") print ("The final score was",userWin,"wins and",computerWin,"losses over",roundCounter,"rounds.") print ("The Cowboy was selected by you",userCowboy -1,"times.") print ("The Ninja was selected by you",userNinja -1,"times.") print ("The Bear was selected by you",userBear -1,"times.") print ("Please play again.") print (" _______ _____ ____ ____ __ ___ ____") print ("| || | || | | || \ || \ ") print ("|__ __|| | || __| | __|| \ || \ ") print (" | | | || __) | __)| || D )") print (" | | | | || | | || \ || / ") print (" |__| |__|__||____| |____||___\__||____/ ") print()
quit=input("Press Enter to exit")
Edited by teknix (10/11/13 11:58 AM)
|