Difference between revisions of "CSC111 Rock-Paper-Scissors Game"

From dftwiki3
Jump to: navigation, search
(Version 2)
(Version 3)
 
Line 144: Line 144:
 
<br />
 
<br />
 
<br />
 
<br />
 +
<br />
 +
 +
{| style="width:100%; background:silver"
 +
|-
 +
|
 +
==Mini Challenge: Random Cheers==
 +
|}
 +
[[Image:QuestionMark1.jpg|right|120px]]
 +
<br />
 +
How would you make the computer display random sentences to tell the user who wins?  For example, when the user wins, the computer could sometimes say "Good job!", or "You win this one fair and square!", or "I'm afraid I lost this one!".
 
<br />
 
<br />
 
<br />
 
<br />

Latest revision as of 10:44, 14 February 2014

--D. Thiebaut (talk) 09:35, 14 February 2014 (EST)


Version 1


# rock-paper-scissors
# rock1.py
# D. Thiebaut
# example of the use of if-else for playing the game
# (this program is not very robust and will not work
# if the user does not enter an uppercase letter that is
# R, P, or S.
#
from random import choice

#--- constants ----
OPTIONS = [ 'R', 'P', 'S' ]
USERWINS = "You win!"
COMPUTERWINS = "I win!"

#--- computer picks a letter ---
computer = choice( OPTIONS )
#print( computer )

#--- user picks a letter ---
human    = input( "Your play? " )

print( "Your play: %s  Computer Play: %s" % ( human, computer ) )

#--- decide who wins ---
if human==computer:
    print( "It's a tie!" )
else:
    if human == 'P':
        # user plays Paper
        if computer == 'S':
            # computer plays Scissors
            print( COMPUTERWINS )    
        else:
            # computer plays 'R'
            print( USERWINS )  
    else:
        if human == 'R':
            # user plays Rock
            if computer == 'S':
                #computer plays Scissors
                print( USERWINS )  
            else:
                # computer plays Paper
                print( COMPUTERWINS )    
        else:
            # user has played Scissors
            if computer == 'R':
                # computer plays Rock
                print( COMPUTERWINS )    
            else:
                # computer plays Paper
                print( USERWINS )


Version 2

This is a tighter version. Shorter and still efficent.

# rock-paper-scissors
# rock1.py
# D. Thiebaut
# example of the use of if-else for playing the game
# (this program is not very robust and will not work
# if the user does not enter an uppercase letter that is
# R, P, or S.
#
from random import choice

OPTIONS = [ 'R', 'P', 'S' ]
USERWINS = "You win!"
COMPUTERWINS = "I win!"

computer = choice( OPTIONS )
print( computer )

human    = input( "Your play? " )

print( "Your play: %s  Computer Play: %s" % ( human, computer ) )

if human==computer:
    print( "It's a tie!" )
elif human == 'P' and computer =='R':
    print( USERWINS )
elif human == 'P' and computer =='S':
    print( COMPUTERWINS )
elif human == 'R' and computer =='S':
    print( USERWINS )
elif human == 'R' and computer =='P':
    print( COMPUTERWINS )
elif human == 'S' and computer =='P':
    print( USERWINS )
#elif human == 'S' and computer =='R':
else:
    print( COMPUTERWINS )


Version 3


Even more tight, and still very efficient.

# rock-paper-scissors
# rock1.py
# D. Thiebaut
# example of the use of if-else for playing the game
# (this program is not very robust and will not work
# if the user does not enter an uppercase letter that is
# R, P, or S.
#
from random import choice

OPTIONS = [ 'R', 'P', 'S' ]
USERWINS = "You win!"
COMPUTERWINS = "I win!"

computer = choice( OPTIONS )
print( computer )

human    = input( "Your play? " )

print( "Your play: %s  Computer Play: %s" % ( human, computer ) )

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 )
else:
    print( COMPUTERWINS )




Mini Challenge: Random Cheers

QuestionMark1.jpg


How would you make the computer display random sentences to tell the user who wins? For example, when the user wins, the computer could sometimes say "Good job!", or "You win this one fair and square!", or "I'm afraid I lost this one!".