Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Python] Rock, Paper, Scissors
03-25-2010, 07:34 AM
Post: #1
[Python] Rock, Paper, Scissors
http://openbookproject.net/pybiblio/prac...issors.php

Description
Rock, paper, scissors, also know as roshambo, is a simple child's game that is frequently used to settle disputes. In the game, a rock breaks the scissors, the scissors cut the paper, and the paper covers the rock. Each option is equally likely to prevail over another. If the players choose the same object a draw is declared and the game is repeated until someone prevails. For more information than you ever thought it was possible to collect about rock, paper, scissors, check out the Web page of the World RPS Society.

In this computerized version the human player competes against the computer which chooses a rock, paper, or scissors randomly. The game proceeds until the human player quits the game or until a predetermined score is reached (e.g., 11 pts.) at which time the final tally is displayed. Solutions with fewer numbers of if statements are considered more "elegant."

Input
The human player enters the number of points required for a win. During the play of the game the human player selects whether to play a rock, paper, or scissors by using the keyboard. The human player may also end the game by pressing the Control-D sequence at any time. (Ending the game early does not allow a winner to be determined if the human player is ahead.)

Output
The program will display the winner of each roshambo round along with the running score. At the conclusion of the game, the computer will display the overall winner and the final score.

Sample session
Welcome to Rock, Paper, Scissors!

How many points are required for a win? 3

Choose (R )ock, (P)aper, or (S)cissors? r
Human: rock Computer: paper Computer wins!

Score: Human 0 Computer 1
Choose (R )ock, (P)aper, or (S)cissors? r

Human: rock Computer: scissors Human wins!

Score: Human 1 Computer 1
Choose (R )ock, (P)aper, or (S)cissors? p

Human: paper Computer: paper A draw

Score: Human 1 Computer 1
Choose (R )ock, (P)aper, or (S)cissors? s

Human: scissors Computer: paper Human wins!

Score: Human 2 Computer 1
Choose (R )ock, (P)aper, or (S)cissors? r

Human: rock Computer: scissors Human wins!

Final Score: Human 3 Computer 1


Going further
Ask the user for his or her name and use the name while the game is playing.
Display randomly chosen taunts when the computer wins.
Consult the World RPS Society Web page and try to program the computer to use some strategy.
Code:
from random import randint
def printchoice(a):
    if a=='r':
        return 'Rock'
    elif a=='p':
        return 'Paper'
    elif a=='s':
        return 'Scissors'
def work(a,b):
    if (a,b)==('r','p') or (a,b)==('p','s') or (a,b)==('s','r'):
        return 2
    elif (a,b)==('p','r') or (a,b)==('s','p') or (a,b)==('r','s'):
        return 1
    elif a==b:
        return 0

def MyPrint(ch,cc,sch,scc,name):
    w=''
    if work(ch,cc)==1:
        w=name+' wins!'
    elif work(ch,cc)==0:
        w='A draw!'
    elif work(ch,cc)==2:
        w='Computer wins!'
    print name+': %s    Computer: %s     %s'%(printchoice(ch),printchoice(cc),w)
    print ' '
    print 'Score: %d   %d'%(sch,scc)

print "Welcome to Rock, Paper, Scissors!"
name=raw_input("Enter your name: ")
No=int(raw_input("How many points are required for a win? "))
scoreH=0
scoreC=0
s='rps'
i=0
while (i!=No):
    choiceH=str(raw_input("Choose (r)ock, (r)aper, or (s)cissors? "))
    choiceC=s[randint(0,2)]
    if choiceH==choiceC:
        i-=1
    if work(choiceH,choiceC)==1:
        scoreH+=1
    elif work(choiceH,choiceC)==2:
        scoreC+=1
    MyPrint(choiceH,choiceC,scoreH,scoreC,name)
    i+=1
print 'Final Score: %s %d   Computer %d'%(name,scoreH,scoreC)
Opening and viewing the source in Python would be easier to understand.

.rar  rock_paper_scissors.rar (Size: 613 bytes / Downloads: 8)

[Image: 45669_pythonlogo.png][Image: 45668_javalogo.png]
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: