Difference between revisions of "CSC111 Top-Down Rock-Paper-Scissors"

From dftwiki3
Jump to: navigation, search
(Step 4)
(Video Intoduction)
 
(9 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
--[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 19:15, 5 March 2014 (EST)
 
--[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 19:15, 5 March 2014 (EST)
 
----
 
----
 +
<bluebox>
 +
This page demonstrates the user to ''top-down'' design with a simple example of Rock-Paper-Scissors in Python.
 +
</bluebox>
 +
 +
=Video Introduction=
 +
<br />
 +
<center><videoflash>oV6z_hGFtSQ</videoflash></center>
 +
<br />
 +
 
=Starting Point=
 
=Starting Point=
 
<br />
 
<br />
Line 223: Line 232:
 
=Step 5=
 
=Step 5=
 
<br />
 
<br />
<source lang="python" highlight=2-6>
+
<source lang="python" highlight=10-14>
 
# rock, paper, scissors
 
# rock, paper, scissors
 
# D. Thiebaut
 
# D. Thiebaut
Line 291: Line 300:
  
 
</source>
 
</source>
 +
<br />
 +
;Output
 +
 +
>>>
 +
I win this round!
 +
I win this round!
 +
I win this round!
 +
I win!
 +
>>>
 +
 +
<br />
 +
=Step 6=
 +
<br />
 +
In this step we flesh out the '''pickALetter()''' function.  It won't be robust yet, but at least it will do what we need: get a letter from the user and return it to the caller:
 +
<br />
 +
<source lang="python" highlight=11-14>
 +
# rock, paper, scissors
 +
# D. Thiebaut
 +
# A top-down approach
 +
 +
HUMAN    = "H"
 +
COMPUTER = "C"
 +
TIE      = "T"
 +
 +
# pickALetter: prompts the user for a letter, and doe not return
 +
# until it is valid.
 +
def pickALetter():
 +
    ans = input( "> " ).upper().strip()
 +
    return ans
 +
 +
def pickRandom():
 +
    return "S"
 +
 +
# playRound: plays a round between human and computer.
 +
# returns the winner of the round (HUMAN or COMPUTER)
 +
def playRound():
 +
    human = pickALetter()
 +
    computer = pickRandom()
 +
 +
    if human==computer:
 +
        print( "It's a tie!" )
 +
        return TIE
 +
   
 +
    if ( human=="P" and computer=="S" ) \
 +
    or ( human=="R" and computer=="P" ) \
 +
    or ( human=="S" and computer=="R" ):
 +
        print( "I win this round!" )
 +
        return COMPUTER
 +
 +
    print( "You win this round!" )
 +
    return HUMAN
 +
 +
# printOutcome: displays the winner of the game
 +
def printOutcome( humanCount, computerCount ):
 +
    if humanCount > computerCount:
 +
        print( "You win!" )
 +
    else:
 +
        print( "I win!" )
 +
   
 +
def main():
 +
    # counters for winning rounds
 +
    humanCount    = 0
 +
    computerCount = 0
 +
 +
    # play rounds
 +
    while ( abs( humanCount - computerCount ) < 3 ):
 +
        # play one round between computer and human
 +
        winner = playRound()
 +
 +
        # see if human wins
 +
        if winner == HUMAN:
 +
            humanCount += 1
 +
 +
        # see if computer wins
 +
        if winner == COMPUTER:
 +
            computerCount += 1
 +
 +
        # if neither, it's a tie and we don't increment
 +
        # counters at all
 +
 +
    # loop is over. We announce the winner
 +
    printOutcome( humanCount, computerCount )
 +
 +
main()
 +
</source>
 +
<br />
 +
 +
 +
 +
<br />
 +
=Step 7=
 +
<br />
 +
In this step we flesh out '''pickRandom()'''.  For this we must import a couple functions from the random library, and make pickRandom() pick a letter at random.
 +
<br />
 +
<source lang="python" highlight=4-6,18-22>
 +
# rock, paper, scissors
 +
# D. Thiebaut
 +
# A top-down approach
 +
from random import choice
 +
from random import seed
 +
seed()
 +
 +
HUMAN    = "H"
 +
COMPUTER = "C"
 +
TIE      = "T"
 +
 +
# pickALetter: prompts the user for a letter, and doe not return
 +
# until it is valid.
 +
def pickALetter():
 +
    ans = input( "> " ).upper().strip()
 +
    return ans
 +
 +
def pickRandom():
 +
    OPTIONS = ["R", "S", "P" ]
 +
    answer = choice( OPTIONS )
 +
    print( "Computer picked: ", answer )
 +
    return answer
 +
 +
# playRound: plays a round between human and computer.
 +
# returns the winner of the round (HUMAN or COMPUTER)
 +
def playRound():
 +
    human = pickALetter()
 +
    computer = pickRandom()
 +
 +
    if human==computer:
 +
        print( "It's a tie!" )
 +
        return TIE
 +
   
 +
    if ( human=="P" and computer=="S" ) \
 +
    or ( human=="R" and computer=="P" ) \
 +
    or ( human=="S" and computer=="R" ):
 +
        print( "I win this round!" )
 +
        return COMPUTER
 +
 +
    print( "You win this round!" )
 +
    return HUMAN
 +
 +
# printOutcome: displays the winner of the game
 +
def printOutcome( humanCount, computerCount ):
 +
    if humanCount > computerCount:
 +
        print( "You win!" )
 +
    else:
 +
        print( "I win!" )
 +
   
 +
def main():
 +
    # counters for winning rounds
 +
    humanCount    = 0
 +
    computerCount = 0
 +
 +
    # play rounds
 +
    while ( abs( humanCount - computerCount ) < 3 ):
 +
        # play one round between computer and human
 +
        winner = playRound()
 +
 +
        # see if human wins
 +
        if winner == HUMAN:
 +
            humanCount += 1
  
 +
        # see if computer wins
 +
        if winner == COMPUTER:
 +
            computerCount += 1
 +
 +
        # if neither, it's a tie and we don't increment
 +
        # counters at all
 +
 +
    # loop is over. We announce the winner
 +
    printOutcome( humanCount, computerCount )
 +
 +
main()
 +
</source>
 +
<br />
 +
=Step 8=
 +
<br />
 +
In this step we add robustness to the program and make '''pickALetter()''' robust to invalid inputs.
 +
<br />
 +
<source lang="python" highlight=15-17>
 +
# rock, paper, scissors
 +
# D. Thiebaut
 +
# A top-down approach
 +
from random import choice
 +
from random import seed
 +
seed()
 +
 +
HUMAN    = "H"
 +
COMPUTER = "C"
 +
TIE      = "T"
 +
 +
# pickALetter: prompts the user for a letter, and doe not return
 +
# until it is valid.
 +
def pickALetter():
 +
    ans = ""
 +
    while ans != "P" and ans != "S" and ans != "R":
 +
        ans = input( "> " ).upper().strip()
 +
       
 +
    return ans
 +
 +
def pickRandom():
 +
    OPTIONS = ["R", "S", "P" ]
 +
    answer = choice( OPTIONS )
 +
    print( "Computer picked: ", answer )
 +
    return answer
 +
 +
# playRound: plays a round between human and computer.
 +
# returns the winner of the round (HUMAN or COMPUTER)
 +
def playRound():
 +
    human = pickALetter()
 +
    computer = pickRandom()
 +
 +
    if human==computer:
 +
        print( "It's a tie!" )
 +
        return TIE
 +
   
 +
    if ( human=="P" and computer=="S" ) \
 +
    or ( human=="R" and computer=="P" ) \
 +
    or ( human=="S" and computer=="R" ):
 +
        print( "I win this round!" )
 +
        return COMPUTER
 +
 +
    print( "You win this round!" )
 +
    return HUMAN
 +
 +
# printOutcome: displays the winner of the game
 +
def printOutcome( humanCount, computerCount ):
 +
    if humanCount > computerCount:
 +
        print( "You win!" )
 +
    else:
 +
        print( "I win!" )
 +
   
 +
def main():
 +
    # counters for winning rounds
 +
    humanCount    = 0
 +
    computerCount = 0
 +
 +
    # play rounds
 +
    while ( abs( humanCount - computerCount ) < 3 ):
 +
        # play one round between computer and human
 +
        winner = playRound()
 +
 +
        # see if human wins
 +
        if winner == HUMAN:
 +
            humanCount += 1
 +
 +
        # see if computer wins
 +
        if winner == COMPUTER:
 +
            computerCount += 1
 +
 +
        # if neither, it's a tie and we don't increment
 +
        # counters at all
 +
 +
    # loop is over. We announce the winner
 +
    printOutcome( humanCount, computerCount )
 +
 +
main()
 +
 +
</source>
 +
<br />
 +
 +
<br />
 +
=Step 9=
 +
<br />
 +
<source lang="python" highlight=53-57,60-61>
 +
# rock, paper, scissors
 +
# D. Thiebaut
 +
# A top-down approach
 +
from random import choice
 +
from random import seed
 +
seed()
 +
 +
HUMAN    = "H"
 +
COMPUTER = "C"
 +
TIE      = "T"
 +
 +
# pickALetter: prompts the user for a letter, and doe not return
 +
# until it is valid.
 +
def pickALetter():
 +
    ans = ""
 +
    while ans != "P" and ans != "S" and ans != "R":
 +
        ans = input( "> " ).upper().strip()
 +
       
 +
    return ans
 +
 +
def pickRandom():
 +
    OPTIONS = ["R", "S", "P" ]
 +
    answer = choice( OPTIONS )
 +
    print( "Computer picked: ", answer )
 +
    return answer
 +
 +
# playRound: plays a round between human and computer.
 +
# returns the winner of the round (HUMAN or COMPUTER)
 +
def playRound():
 +
    human = pickALetter()
 +
    computer = pickRandom()
 +
 +
    if human==computer:
 +
        print( "It's a tie!" )
 +
        return TIE
 +
   
 +
    if ( human=="P" and computer=="S" ) \
 +
    or ( human=="R" and computer=="P" ) \
 +
    or ( human=="S" and computer=="R" ):
 +
        print( "I win this round!" )
 +
        return COMPUTER
 +
 +
    print( "You win this round!" )
 +
    return HUMAN
 +
 +
# printOutcome: displays the winner of the game
 +
def printOutcome( humanCount, computerCount ):
 +
    if humanCount > computerCount:
 +
        print( "You win!" )
 +
    else:
 +
        print( "I win!" )
 +
 +
# Greetings(): greets the user and invite her to play
 +
# the game.
 +
def greetings():
 +
    print( "Welcome to Rock-Scissors-Paper!" )
 +
    print( "-------------------------------" )
 +
   
 +
def main():
 +
    # greets user
 +
    greetings()
 +
   
 +
    # counters for winning rounds
 +
    humanCount    = 0
 +
    computerCount = 0
 +
 +
    # play rounds
 +
    while ( abs( humanCount - computerCount ) < 3 ):
 +
        # play one round between computer and human
 +
        winner = playRound()
 +
 +
        # see if human wins
 +
        if winner == HUMAN:
 +
            humanCount += 1
 +
 +
        # see if computer wins
 +
        if winner == COMPUTER:
 +
            computerCount += 1
 +
 +
        # if neither, it's a tie and we don't increment
 +
        # counters at all
 +
 +
    # loop is over. We announce the winner
 +
    printOutcome( humanCount, computerCount )
 +
 +
main()
 +
 +
</source>
 +
<br />
 
=Final Program=
 
=Final Program=
 
<br />
 
<br />

Latest revision as of 09:52, 7 March 2014

--D. Thiebaut (talk) 19:15, 5 March 2014 (EST)


This page demonstrates the user to top-down design with a simple example of Rock-Paper-Scissors in Python.

Video Introduction



Starting Point


This is the very first "draft". It won't compile, but it contains the logic of the game.

# rock, paper, scissors
# D. Thiebaut
# A top-down approach

HUMAN    = "H"
COMPUTER = "C"

def main():
    # counters for winning rounds
    humanCount    = 0
    computerCount = 0

    # play rounds
    while ( abs( humanCount - computerCount ) < 3 ):
        # play one round between computer and human
        winner = playRound()

        # see if human wins
        if winner == HUMAN:
            humanCount += 1

        # see if computer wins
        if winner == COMPUTER:
            computerCount += 1

        # if neither, it's a tie and we don't increment
        # counters at all

    # loop is over. We announce the winner
    printOutcome( humanCount, computerCount )

main()


Step 2


We add empty functions to allow the interpreter to parse our program. Note that the functions do not do anything, just the bare minimum of what they are supposed to do.

# rock, paper, scissors
# D. Thiebaut
# A top-down approach

HUMAN    = "H"
COMPUTER = "C"

# playRound: plays a round between human and computer.
# returns the winner of the round (HUMAN or COMPUTER)
def playRound():
    print( "round" )
    return HUMAN

# printOutcome: displays the winner of the game 
def printOutcome( humanCount, computerCount ):
    print( humanCount, computerCount )
    
def main():
    # counters for winning rounds
    humanCount    = 0
    computerCount = 0

    # play rounds
    while ( abs( humanCount - computerCount ) < 3 ):
        # play one round between computer and human
        winner = playRound()

        # see if human wins
        if winner == HUMAN:
            humanCount += 1

        # see if computer wins
        if winner == COMPUTER:
            computerCount += 1

        # if neither, it's a tie and we don't increment
        # counters at all

    # loop is over. We announce the winner
    printOutcome( humanCount, computerCount )

main()
Output of the program
>>>
Round
Round
Round
3 0
>>> 

Step 3


We take one of the functions, the simplest one to expand, and do so. Here, it's printOutcome() that we work on.

# rock, paper, scissors
# D. Thiebaut
# A top-down approach

HUMAN    = "H"
COMPUTER = "C"

# playRound: plays a round between human and computer.
# returns the winner of the round (HUMAN or COMPUTER)
def playRound():
    print( "Round" )
    return HUMAN

# printOutcome: displays the winner of the game 
def printOutcome( humanCount, computerCount ):
    if humanCountn > computerCount:
        print( "You win!" )
    else:
        print( "I win!" )
    
def main():
    # counters for winning rounds
    humanCount    = 0
    computerCount = 0

    # play rounds
    while ( abs( humanCount - computerCount ) < 3 ):
        # play one round between computer and human
        winner = playRound()

        # see if human wins
        if winner == HUMAN:
            humanCount += 1

        # see if computer wins
        if winner == COMPUTER:
            computerCount += 1

        # if neither, it's a tie and we don't increment
        # counters at all

    # loop is over. We announce the winner
    printOutcome( humanCount, computerCount )

main()


Step 4


In this step we flesh out playRound(). As we write it, we decide to create two new functions. The code below will not pass the interpreter, which will flag errors, but it's a logical step forward for us.

# rock, paper, scissors
# D. Thiebaut
# A top-down approach

HUMAN    = "H"
COMPUTER = "C"
TIE      = "T"

# playRound: plays a round between human and computer.
# returns the winner of the round (HUMAN or COMPUTER)
def playRound():
    human = pickALetter()
    computer = pickRandom()

    if human==computer:
        print( "It's a tie!" )
        return TIE
    
    if ( human=="P" and computer=="S" )
    or ( human=="R" and computer=="P" )
    or ( human=="S" and computer=="R" ):
        print( "I win this round!" )
        return COMPUTER

    print( "You win this round!" )
    return HUMAN

# printOutcome: displays the winner of the game 
def printOutcome( humanCount, computerCount ):
    if humanCountn > computerCount:
        print( "You win!" )
    else:
        print( "I win!" )
    
def main():
    # counters for winning rounds
    humanCount    = 0
    computerCount = 0

    # play rounds
    while ( abs( humanCount - computerCount ) < 3 ):
        # play one round between computer and human
        winner = playRound()

        # see if human wins
        if winner == HUMAN:
            humanCount += 1

        # see if computer wins
        if winner == COMPUTER:
            computerCount += 1

        # if neither, it's a tie and we don't increment
        # counters at all

    # loop is over. We announce the winner
    printOutcome( humanCount, computerCount )

main()


Step 5


# rock, paper, scissors
# D. Thiebaut
# A top-down approach

HUMAN    = "H"
COMPUTER = "C"
TIE      = "T"

def pickALetter():
    return "P"

def pickRandom():
    return "S"

# playRound: plays a round between human and computer.
# returns the winner of the round (HUMAN or COMPUTER)
def playRound():
    human = pickALetter()
    computer = pickRandom()

    if human==computer:
        print( "It's a tie!" )
        return TIE
    
    if ( human=="P" and computer=="S" ) \
    or ( human=="R" and computer=="P" ) \
    or ( human=="S" and computer=="R" ):
        print( "I win this round!" )
        return COMPUTER

    print( "You win this round!" )
    return HUMAN

# printOutcome: displays the winner of the game 
def printOutcome( humanCount, computerCount ):
    if humanCount > computerCount:
        print( "You win!" )
    else:
        print( "I win!" )
    
def main():
    # counters for winning rounds
    humanCount    = 0
    computerCount = 0

    # play rounds
    while ( abs( humanCount - computerCount ) < 3 ):
        # play one round between computer and human
        winner = playRound()

        # see if human wins
        if winner == HUMAN:
            humanCount += 1

        # see if computer wins
        if winner == COMPUTER:
            computerCount += 1

        # if neither, it's a tie and we don't increment
        # counters at all

    # loop is over. We announce the winner
    printOutcome( humanCount, computerCount )

main()


Output
>>> 
I win this round!
I win this round!
I win this round!
I win!
>>>


Step 6


In this step we flesh out the pickALetter() function. It won't be robust yet, but at least it will do what we need: get a letter from the user and return it to the caller:

# rock, paper, scissors
# D. Thiebaut
# A top-down approach

HUMAN    = "H"
COMPUTER = "C"
TIE      = "T"

# pickALetter: prompts the user for a letter, and doe not return
# until it is valid.
def pickALetter():
    ans = input( "> " ).upper().strip()
    return ans

def pickRandom():
    return "S"

# playRound: plays a round between human and computer.
# returns the winner of the round (HUMAN or COMPUTER)
def playRound():
    human = pickALetter()
    computer = pickRandom()

    if human==computer:
        print( "It's a tie!" )
        return TIE
    
    if ( human=="P" and computer=="S" ) \
    or ( human=="R" and computer=="P" ) \
    or ( human=="S" and computer=="R" ):
        print( "I win this round!" )
        return COMPUTER

    print( "You win this round!" )
    return HUMAN

# printOutcome: displays the winner of the game 
def printOutcome( humanCount, computerCount ):
    if humanCount > computerCount:
        print( "You win!" )
    else:
        print( "I win!" )
    
def main():
    # counters for winning rounds
    humanCount    = 0
    computerCount = 0

    # play rounds
    while ( abs( humanCount - computerCount ) < 3 ):
        # play one round between computer and human
        winner = playRound()

        # see if human wins
        if winner == HUMAN:
            humanCount += 1

        # see if computer wins
        if winner == COMPUTER:
            computerCount += 1

        # if neither, it's a tie and we don't increment
        # counters at all

    # loop is over. We announce the winner
    printOutcome( humanCount, computerCount )

main()




Step 7


In this step we flesh out pickRandom(). For this we must import a couple functions from the random library, and make pickRandom() pick a letter at random.

# rock, paper, scissors
# D. Thiebaut
# A top-down approach
from random import choice
from random import seed
seed()

HUMAN    = "H"
COMPUTER = "C"
TIE      = "T"

# pickALetter: prompts the user for a letter, and doe not return
# until it is valid.
def pickALetter():
    ans = input( "> " ).upper().strip()
    return ans

def pickRandom():
    OPTIONS = ["R", "S", "P" ]
    answer = choice( OPTIONS )
    print( "Computer picked: ", answer )
    return answer

# playRound: plays a round between human and computer.
# returns the winner of the round (HUMAN or COMPUTER)
def playRound():
    human = pickALetter()
    computer = pickRandom()

    if human==computer:
        print( "It's a tie!" )
        return TIE
    
    if ( human=="P" and computer=="S" ) \
    or ( human=="R" and computer=="P" ) \
    or ( human=="S" and computer=="R" ):
        print( "I win this round!" )
        return COMPUTER

    print( "You win this round!" )
    return HUMAN

# printOutcome: displays the winner of the game 
def printOutcome( humanCount, computerCount ):
    if humanCount > computerCount:
        print( "You win!" )
    else:
        print( "I win!" )
    
def main():
    # counters for winning rounds
    humanCount    = 0
    computerCount = 0

    # play rounds
    while ( abs( humanCount - computerCount ) < 3 ):
        # play one round between computer and human
        winner = playRound()

        # see if human wins
        if winner == HUMAN:
            humanCount += 1

        # see if computer wins
        if winner == COMPUTER:
            computerCount += 1

        # if neither, it's a tie and we don't increment
        # counters at all

    # loop is over. We announce the winner
    printOutcome( humanCount, computerCount )

main()


Step 8


In this step we add robustness to the program and make pickALetter() robust to invalid inputs.

# rock, paper, scissors
# D. Thiebaut
# A top-down approach
from random import choice
from random import seed
seed()

HUMAN    = "H"
COMPUTER = "C"
TIE      = "T"

# pickALetter: prompts the user for a letter, and doe not return
# until it is valid.
def pickALetter():
    ans = ""
    while ans != "P" and ans != "S" and ans != "R":
        ans = input( "> " ).upper().strip()
        
    return ans

def pickRandom():
    OPTIONS = ["R", "S", "P" ]
    answer = choice( OPTIONS )
    print( "Computer picked: ", answer )
    return answer

# playRound: plays a round between human and computer.
# returns the winner of the round (HUMAN or COMPUTER)
def playRound():
    human = pickALetter()
    computer = pickRandom()

    if human==computer:
        print( "It's a tie!" )
        return TIE
    
    if ( human=="P" and computer=="S" ) \
    or ( human=="R" and computer=="P" ) \
    or ( human=="S" and computer=="R" ):
        print( "I win this round!" )
        return COMPUTER

    print( "You win this round!" )
    return HUMAN

# printOutcome: displays the winner of the game 
def printOutcome( humanCount, computerCount ):
    if humanCount > computerCount:
        print( "You win!" )
    else:
        print( "I win!" )
    
def main():
    # counters for winning rounds
    humanCount    = 0
    computerCount = 0

    # play rounds
    while ( abs( humanCount - computerCount ) < 3 ):
        # play one round between computer and human
        winner = playRound()

        # see if human wins
        if winner == HUMAN:
            humanCount += 1

        # see if computer wins
        if winner == COMPUTER:
            computerCount += 1

        # if neither, it's a tie and we don't increment
        # counters at all

    # loop is over. We announce the winner
    printOutcome( humanCount, computerCount )

main()



Step 9


# rock, paper, scissors
# D. Thiebaut
# A top-down approach
from random import choice
from random import seed
seed()

HUMAN    = "H"
COMPUTER = "C"
TIE      = "T"

# pickALetter: prompts the user for a letter, and doe not return
# until it is valid.
def pickALetter():
    ans = ""
    while ans != "P" and ans != "S" and ans != "R":
        ans = input( "> " ).upper().strip()
        
    return ans

def pickRandom():
    OPTIONS = ["R", "S", "P" ]
    answer = choice( OPTIONS )
    print( "Computer picked: ", answer )
    return answer

# playRound: plays a round between human and computer.
# returns the winner of the round (HUMAN or COMPUTER)
def playRound():
    human = pickALetter()
    computer = pickRandom()

    if human==computer:
        print( "It's a tie!" )
        return TIE
    
    if ( human=="P" and computer=="S" ) \
    or ( human=="R" and computer=="P" ) \
    or ( human=="S" and computer=="R" ):
        print( "I win this round!" )
        return COMPUTER

    print( "You win this round!" )
    return HUMAN

# printOutcome: displays the winner of the game 
def printOutcome( humanCount, computerCount ):
    if humanCount > computerCount:
        print( "You win!" )
    else:
        print( "I win!" )

# Greetings(): greets the user and invite her to play
# the game.
def greetings():
    print( "Welcome to Rock-Scissors-Paper!" )
    print( "-------------------------------" )
    
def main():
    # greets user
    greetings()
    
    # counters for winning rounds
    humanCount    = 0
    computerCount = 0

    # play rounds
    while ( abs( humanCount - computerCount ) < 3 ):
        # play one round between computer and human
        winner = playRound()

        # see if human wins
        if winner == HUMAN:
            humanCount += 1

        # see if computer wins
        if winner == COMPUTER:
            computerCount += 1

        # if neither, it's a tie and we don't increment
        # counters at all

    # loop is over. We announce the winner
    printOutcome( humanCount, computerCount )

main()


Final Program


# rock, paper, scissors
# D. Thiebaut
# A top-down approach

from random import seed
from random import choice
seed()

HUMAN    = "H"
COMPUTER = "C"
TIE      = "T"

# pickAletter: lets user pick a letter that is 'R', 'S', or 'P'
# and keeps on prompting until the letter is correctly entered.
def pickALetter():
    answer = " "
    while answer != "P" and answer != "S" and answer != "R":
        answer = input( "> " ).upper().strip()
        
    return answer

# pickRandom: picks a random letter in 'R', 'S', 'P'.
def pickRandom():
    OPTIONS = [ 'R', 'S', 'P' ]
    answer = choice( OPTIONS )
    #print( "Computer picked:" , answer )
    return answer

# playRound: plays a round between computer and human.  Human
# picks a letter, computer picks a letter at random.
# displays outcome of the game.  Returns winner
def playRound():
    computer = pickRandom()
    human    = pickALetter()

    # tell the user what the computer picked
    print( "I had picked", computer, end=": " )
    
    # checks for ties
    if human==computer:
        print( "It's a tie!" )
        return TIE

    # look at conditions where computer wins
    if (human=="P" and computer=="S")  \
    or (human=="R" and computer=="P")  \
    or (human=="S" and computer=="R"):
        print( "I win this round" )
        return COMPUTER

    # human must have won
    print( "You win this round!" )
    return HUMAN

        
# printOutcome: gets the count of rounds won by two users, and
# displays winner.
def printOutcome( humanCount, computerCount ):
    if humanCount > computerCount:
        print( "You win!" )
    else:
        print( "I win!" )    


def greetings():
    print( "Welcome to Rock-Scissors-Paper!" )
    print( "-------------------------------" )
    
# main: plays several rounds of the rock-paper-scissors game
# until one player gets 3 more points than the other.  Then
# displays the winner.
def main():
    # greets the user, displays the rules
    greetings()
    
    # counters for winning rounds
    humanCount    = 0
    computerCount = 0

    while ( abs( humanCount - computerCount ) < 3 ):
        winner = playRound()
        if winner == HUMAN:
            humanCount += 1
        if winner == COMPUTER:
            computerCount += 1
        
    
    printOutcome( humanCount, computerCount )


main()