Difference between revisions of "CSC111 Homework 3 Solution Programs 2014"

From dftwiki3
Jump to: navigation, search
(Program 1)
(Version 2)
Line 196: Line 196:
 
</source>
 
</source>
 
<br />
 
<br />
 +
=Program 2=
 +
<br />
 +
<source lang="python">
 +
# hw3b.py
 +
# Yijin Wei (bd) and Julieanna Niu (bi)
 +
# (slightly edited by D. Thiebaut)
 +
# This program will play three rounds of rock-paper-scissors with the user and
 +
# display the result of the game. The input can be r, s, or p in lower or upper case.
 +
# If the user enters an invalid input, the program will have one chance to input
 +
# a new choice, otherwise the program assigns one randomly.
 +
 +
from random import choice
 +
 +
OPTIONS            = [ 'R', 'P', 'S' ]
 +
USERWINS        = "You win!"
 +
COMPUTERWINS = "The computer wins!"
 +
NOROUNDS        = 3
 +
 +
totalTime = 0
 +
userScore = 0
 +
computerScore = 0
 +
 +
# ============================================
 +
# repeats a round while the total amount of rounds is less than three
 +
while totalTime < NOROUNDS:
 +
 +
    computer = choice( OPTIONS )
 +
    print("The computer has chosen",computer )
 +
    human = input( "Your play? " ).upper()
 +
 +
  # asks the user for new character if the user input is invalid the first time
 +
    if human != 'R' and human != 'P' and human != 'S':
 +
      human = input ("Please enter a valid character: ").upper()
 +
 +
      # assigns a character if the user input is invalid the second time
 +
      if human != 'R' and human != 'P' and human != 'S':
 +
        human=choice( OPTIONS )
 +
        print("The computer has assigned you",human)
 +
    print( "Your play: %s  Computer Play: %s" % ( human, computer ) )
 +
   
 +
  # the program runs the series of test cases
 +
    if human==computer:
 +
        print( "It's a tie!" )
 +
    elif human == 'P' and computer =='R' \
 +
        or human == 'R' and computer =='S' \
 +
        or human == 'S' and computer =='P':
 +
        print( USERWINS )
 +
        userScore = userScore+1
 +
    else:
 +
        print( COMPUTERWINS )
 +
        computerScore = computerScore +1
 +
    totalTime=totalTime+1
 +
    print()
 +
 +
# ============================================
 +
# determines and outputs the final result
 +
 +
if userScore>computerScore:
 +
    print ("You are the winner of the three rounds!")
 +
elif computerScore>userScore:
 +
    print("The computer is the winner of the three rounds!")
 +
else:
 +
    print("The overall result is a tie!")
 +
 +
</source>
 
<br />
 
<br />
 
<br />
 
<br />

Revision as of 10:45, 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." )


Program 2


# hw3b.py
# Yijin Wei (bd) and Julieanna Niu (bi)
# (slightly edited by D. Thiebaut)
# This program will play three rounds of rock-paper-scissors with the user and
# display the result of the game. The input can be r, s, or p in lower or upper case.
# If the user enters an invalid input, the program will have one chance to input
# a new choice, otherwise the program assigns one randomly.

from random import choice

OPTIONS            = [ 'R', 'P', 'S' ]
USERWINS         = "You win!"
COMPUTERWINS = "The computer wins!"
NOROUNDS        = 3

totalTime = 0
userScore = 0
computerScore = 0

# ============================================
# repeats a round while the total amount of rounds is less than three
while totalTime < NOROUNDS:

    computer = choice( OPTIONS )
    print("The computer has chosen",computer )
    human = input( "Your play? " ).upper()

   # asks the user for new character if the user input is invalid the first time
    if human != 'R' and human != 'P' and human != 'S':
       human = input ("Please enter a valid character: ").upper()

      # assigns a character if the user input is invalid the second time
       if human != 'R' and human != 'P' and human != 'S':
         human=choice( OPTIONS )
         print("The computer has assigned you",human)
    print( "Your play: %s  Computer Play: %s" % ( human, computer ) )
    
   # the program runs the series of test cases 
    if human==computer:
        print( "It's a tie!" )
    elif human == 'P' and computer =='R' \
         or human == 'R' and computer =='S' \
         or human == 'S' and computer =='P':
        print( USERWINS )
        userScore = userScore+1
    else:
        print( COMPUTERWINS )
        computerScore = computerScore +1
    totalTime=totalTime+1
    print()

# ============================================
# determines and outputs the final result

if userScore>computerScore:
    print ("You are the winner of the three rounds!")
elif computerScore>userScore:
    print("The computer is the winner of the three rounds!")
else:
    print("The overall result is a tie!")