Difference between revisions of "CSC111 Top-Down Rock-Paper-Scissors"
(Created page with "--~~~~ ---- <br /> <source lang="python"> # rock, paper, scissors # D. Thiebaut # A top-down approach from random import seed from random import choice seed() HUMAN = "H"...") |
|||
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) | ||
---- | ---- | ||
+ | =Starting Point= | ||
<br /> | <br /> | ||
+ | This is the very first "draft". It won't compile, but it contains the logic of the game. | ||
+ | <br /> | ||
+ | <source lang="python"> | ||
+ | # 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() | ||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | =Step 2= | ||
+ | <br /> | ||
+ | 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. | ||
+ | <br /> | ||
+ | <source lang="python" highlight=8-16> | ||
+ | # 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(): | ||
+ | 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() | ||
+ | |||
+ | </source> | ||
+ | |||
<source lang="python"> | <source lang="python"> | ||
# rock, paper, scissors | # rock, paper, scissors |
Revision as of 09:23, 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():
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()
# 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()