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

From dftwiki3
Jump to: navigation, search
(Version 2)
(Version 2)
Line 104: Line 104:
 
</source>
 
</source>
 
<br />
 
<br />
 +
=Version 3=
 +
<br />
 +
Even more tight, and still very efficient.
 +
<br />
 +
<source lang="python">
 +
# 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 )
 +
   
 +
           
 +
 +
</source>
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
[[Category:CSC111]][[Category:Python]][[Category:Exercises]]

Revision as of 10:41, 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 )