2010年6月20日 星期日

[Question 5] Rock, Paper, Scissors







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

1. Ask the user for his or her name and use the name while the game is playing.
2. Display randomly chosen taunts when the computer wins.
3. Consult the World RPS Society Web page and try to program the computer to use some strategy.




#! /usr/bin/env python
import random

print 'Welcome to Rock, Paper, Scissors!'
get_point = int(raw_input('How many points are required for a win?'))



# computer part
human_get_point = 0
computer_get_point = 0


while 1 :
if ((computer_get_point >= get_point) or (human_get_point>=get_point)):
break
choose = raw_input('Choose (R)ock, (P)aper, or (S)cissors? ')
#human part

if ( choose == 'r' or choose == 'R' ):
human_choose_roshambo = 1
elif(choose == 'p' or choose == 'P'):
human_choose_roshambo = 2
elif(choose == 's' or choose == 'S'):
human_choose_roshambo = 3
#This is random a Rock,Paper,Scissor
while 1 :
computer_random=int(random.random()*10)
if ( computer_random < 4 or computer_random > 0 ):
computer_roshambo = computer_random
break

if (human_choose_roshambo == computer_roshambo):
print 'A draw'
print ' Score: Human %2d Computer %2d A draw'%(human_get_point,computer_get_point)

elif (human_choose_roshambo > computer_roshambo):
human_get_point = human_get_point + 1
print ' Human wins! '
print ' Score: Human %2d Computer %2d Human wins!'%(human_get_point,computer_get_point)

elif (human_choose_roshambo < computer_roshambo):
computer_get_point = computer_get_point + 1
print 'Computer wins!'
print ' Score: Human %2d Computer %2d Computer wins!'%(human_get_point,computer_get_point)

Related Posts:

0 意見:

張貼留言