Difference between revisions of "CSC111 Top-Down Rock-Paper-Scissors"
(→Final Program) |
(→Final Program) |
||
Line 379: | Line 379: | ||
+ | |||
+ | <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=10-12> | ||
+ | # 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 ) | ||
+ | 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 /> | ||
=Final Program= | =Final Program= |
Revision as of 10:42, 7 March 2014
--D. Thiebaut (talk) 19:15, 5 March 2014 (EST)
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 )
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()
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()