Difference between revisions of "CSC111 Homework 3 Solution Programs 2014"
(→Program 1) |
(→Program 1) |
||
Line 3: | Line 3: | ||
=Program 1= | =Program 1= | ||
+ | <br /> | ||
+ | ==Version 1== | ||
<br /> | <br /> | ||
An elegant solution that simply replicates the code for each round. We hadn't seen the while loop yet, so this was perfectly acceptable. The code is well organized and easy to scan/read. | An elegant solution that simply replicates the code for each round. We hadn't seen the while loop yet, so this was perfectly acceptable. The code is well organized and easy to scan/read. | ||
Line 128: | Line 130: | ||
</source> | </source> | ||
<br /> | <br /> | ||
+ | ==Version 2== | ||
<br /> | <br /> | ||
+ | Here we have a solution where a '''while''' loop is used. Notice how much tighter the code is when we use loops! The code is well documented and comments highlights the different sections. | ||
+ | <br /> | ||
+ | <source lang="python"> | ||
+ | # hw3a.py | ||
+ | # Erika Earley (ar), Ayliffe Brown (cr) | ||
+ | # (slightly edited by D. Thiebaut) | ||
+ | # 2/18/14 | ||
+ | # write a rock-paper-scissors-MUG game that includes new for rules for Mug | ||
+ | # choice, recognizes both lowercase and uppercase inputs from the user, lets | ||
+ | # the user play 3 rounds with the computer, keeps track of the score, and | ||
+ | # prints the result for each round and for each 3-round game. | ||
+ | |||
+ | |||
+ | from random import choice | ||
+ | |||
+ | OPTIONS = ['R', 'P', 'S', 'M' ] | ||
+ | NOROUNDS = 3 | ||
+ | |||
+ | # Create/initialize the variables which will keep track of the score | ||
+ | computerScore = 0 | ||
+ | humanScore = 0 | ||
+ | |||
+ | # ============================================= | ||
+ | # Create/initialize varible which will keep track of # rounds | ||
+ | # After 3 rounds, the result of the entire game will be printed | ||
+ | round = 0 | ||
+ | while round < NOROUNDS: | ||
+ | |||
+ | computer = choice( OPTIONS ) | ||
+ | human = input( "What is your choice?\n" ).upper() | ||
+ | print( "The computer has chosen", computer ) | ||
+ | if ( computer=='R' and human=='P' ) \ | ||
+ | or ( computer=='P' and human=='S' ) \ | ||
+ | or ( computer=='S' and human=='R' ) \ | ||
+ | or ( computer=='S' and human=='M' ) \ | ||
+ | or ( computer=='M' and human=='P' ) \ | ||
+ | or ( computer=='M' and human=='R' ) : | ||
+ | print( "YOU WIN!" ) | ||
+ | humanScore = humanScore + 1 | ||
+ | |||
+ | elif ( computer=='P' and human=='R' ) \ | ||
+ | or ( computer=='S' and human=='P' ) \ | ||
+ | or ( computer=='R' and human=='S' ) \ | ||
+ | or ( computer=='M' and human=='S' ) \ | ||
+ | or ( computer=='P' and human=='M' ) \ | ||
+ | or ( computer=='R' and human=='M' ) : | ||
+ | print( "YOU LOSE" ) | ||
+ | computerScore = computerScore + 1 | ||
+ | else: | ||
+ | print( "It's a tie!" ) | ||
+ | round = round + 1 | ||
+ | |||
+ | # ============================================= | ||
+ | # Compare the score-count for the computer vs. the score-count for | ||
+ | # the human to determine the winner | ||
+ | if computerScore > humanScore: | ||
+ | print( "You lose the whole game! Boooo." ) | ||
+ | elif computerScore < humanScore: | ||
+ | print( "You win the 3 rounds! WOOHOO!" ) | ||
+ | else: | ||
+ | print( "Your 3 rounds end in a tie! How boring." ) | ||
+ | |||
+ | </source> | ||
<br /> | <br /> | ||
<br /> | <br /> |
Revision as of 11:40, 27 February 2014
--D. Thiebaut (talk) 10:36, 27 February 2014 (EST)
Program 1
Version 1
An elegant solution that simply replicates the code for each round. We hadn't seen the while loop yet, so this was perfectly acceptable. The code is well organized and easy to scan/read.
# hw3a.py
# by Zoey Jingyi Sun(ch) and Phyllis Chen(cv)
# (slightly edited by D. Thiebaut)
#
# This program allow users to play a more advanced version of
# "ROCK/PAPER/SCISSORS/MUG" by adding a MUG option. The rules are "Scissors fall in mug, and mug wins
# Paper blocks mug, and paper wins,Rock breaks mug, and rock wins". This program
# will let the user play three rounds and keep track of the score. In the end, it
# will declare the winner.
#
#Initialized the computerscore and humanscore
computerScore = 0
humanScore = 0
# ========================================================
# Round1
from random import choice
OPTIONS = ['R', 'P', 'S' ,'M']
computer = choice( OPTIONS )
human = input( "What is your play? " ).replace(" ","").upper()
print( "The computer has chosen", computer )
if ( computer=='R' and human=='P' ) \
or ( computer=='P' and human=='S' ) \
or ( computer=='S' and human=='R')\
or ( computer=='M' and human=='P')\
or ( computer=='M' and human=='R')\
or ( computer=='S' and human=='M'):
print( "Congrats, you win!" )
humanScore = humanScore + 1
elif ( computer=='R' and human=='S' ) \
or ( computer=='P' and human=='R' ) \
or ( computer=='S' and human=='P' )\
or ( computer=='M' and human=='S')\
or ( computer=='R' and human=='M')\
or ( computer=='P' and human=='M'):
print( "Sorry, you lose this time!" )
computerScore = computerScore + 1
else:
print( "It's a tie!" )
# ========================================================
#Round2
computer = choice( OPTIONS )
human = input( "What is your play? " ).replace(" ","").upper()
print( "The computer has chosen", computer )
if ( computer=='R' and human=='P' ) \
or ( computer=='P' and human=='S' ) \
or ( computer=='S' and human=='R')\
or ( computer=='M' and human=='P')\
or ( computer=='M' and human=='R')\
or ( computer=='S' and human=='M'):
print( "Congrats, you win!" )
humanScore = humanScore + 1
elif ( computer=='R' and human=='S' ) \
or ( computer=='P' and human=='R' ) \
or ( computer=='S' and human=='P' )\
or ( computer=='M' and human=='S')\
or ( computer=='R' and human=='M')\
or ( computer=='P' and human=='M'):
print( "Sorry, you lose this time!" )
computerScore = computerScore + 1
else:
print( "It's a tie!" )
# ========================================================
#Round3
computer = choice( OPTIONS )
human = input( "What is your play? " ).replace(" ","").upper()
print( "The computer has chosen", computer )
if ( computer=='R' and human=='P' ) \
or ( computer=='P' and human=='S' ) \
or ( computer=='S' and human=='R')\
or ( computer=='M' and human=='P')\
or ( computer=='M' and human=='R')\
or ( computer=='S' and human=='M'):
print( "Congrats, you win!" )
humanScore = humanScore + 1
elif ( computer=='R' and human=='S' ) \
or ( computer=='P' and human=='R' ) \
or ( computer=='S' and human=='P' )\
or ( computer=='M' and human=='S')\
or ( computer=='R' and human=='M')\
or ( computer=='P' and human=='M'):
print( "Sorry, you lose this time!" )
computerScore = computerScore + 1
else:
print( "It's a tie!" )
# ========================================================
# Outcome
if computerScore > humanScore:
print( "You lose! I am sorry, but you are no match for me!" )
elif computerScore < humanScore:
print( "You win the 3 rounds! Congrats!" )
else:
print( "Your 3 rounds end in a tie!" )
Version 2
Here we have a solution where a while loop is used. Notice how much tighter the code is when we use loops! The code is well documented and comments highlights the different sections.
# hw3a.py
# Erika Earley (ar), Ayliffe Brown (cr)
# (slightly edited by D. Thiebaut)
# 2/18/14
# write a rock-paper-scissors-MUG game that includes new for rules for Mug
# choice, recognizes both lowercase and uppercase inputs from the user, lets
# the user play 3 rounds with the computer, keeps track of the score, and
# prints the result for each round and for each 3-round game.
from random import choice
OPTIONS = ['R', 'P', 'S', 'M' ]
NOROUNDS = 3
# Create/initialize the variables which will keep track of the score
computerScore = 0
humanScore = 0
# =============================================
# Create/initialize varible which will keep track of # rounds
# After 3 rounds, the result of the entire game will be printed
round = 0
while round < NOROUNDS:
computer = choice( OPTIONS )
human = input( "What is your choice?\n" ).upper()
print( "The computer has chosen", computer )
if ( computer=='R' and human=='P' ) \
or ( computer=='P' and human=='S' ) \
or ( computer=='S' and human=='R' ) \
or ( computer=='S' and human=='M' ) \
or ( computer=='M' and human=='P' ) \
or ( computer=='M' and human=='R' ) :
print( "YOU WIN!" )
humanScore = humanScore + 1
elif ( computer=='P' and human=='R' ) \
or ( computer=='S' and human=='P' ) \
or ( computer=='R' and human=='S' ) \
or ( computer=='M' and human=='S' ) \
or ( computer=='P' and human=='M' ) \
or ( computer=='R' and human=='M' ) :
print( "YOU LOSE" )
computerScore = computerScore + 1
else:
print( "It's a tie!" )
round = round + 1
# =============================================
# Compare the score-count for the computer vs. the score-count for
# the human to determine the winner
if computerScore > humanScore:
print( "You lose the whole game! Boooo." )
elif computerScore < humanScore:
print( "You win the 3 rounds! WOOHOO!" )
else:
print( "Your 3 rounds end in a tie! How boring." )