Macroware Technology

Welcome Portlet


Welcome to Macroware Technology Blog.

The purposes of this engineering blog:

- Share knowledge
- Learn
- Have fun
- Document what I was doing / thinking

Search Box

 

Private Area

Mailing List

Engineering Education

Engineering Organizations

Professional Career Networking Groups

Electronics Hobby

Trade Publications

Python Scripting - Rock, Scissors, Paper

posted Tuesday, 2 December 2008

During a visit to my parents home for Thanksgiving, I discovered an old book of BASIC language computer games that I had as a kid [1].  I typed many of these games in to a TI-99 computer and a Commodore 128 computer as a kid.  They are all text based games, some of them pretty simple, and some a bit more complicated.  What better way to continue learning Python than by translating some of these classic games into Python script language, and provide some thought filled, and not multi-media overstimulating entertainment for the kids.  I started with a rock, scissors, paper game from page 137 of [1].  The user must choose rock, scissors, or paper and then the computer makes its own random choice and the rules of rock beats scissors, scissors beats paper, and paper beats rock are used to determine the winner.  In this script I made use of a user defined function to implement the rock, scissors, paper rule, and also used random number generator functions in the random module.  The source code is given at the end.

 

[1] Ahl, David H. 1978. Basic Computer Games Microcomputer Edition.  Workman Publishing: New York

 

 

# Adapted from "Rock, Scissors, Paper" on page 137 of:
#   Ahl, David H., Steve North, and George Beker. 1978. "Basic Computer Games Microcomputer Edition."
#   Workman Publishing: New York.
#
# Function to determine winner of a rock, scissors paper, round
#    Choice = players choice (1=Rock, 2=Scissors, 3=Paper)
#    Returns result (0 = tie, 1 = computer wins, 2 = player wins)
import sys
import random


def rsp(choice):
  computer_choice = random.randint(1,3)
  if (computer_choice == 1):
    print '...ROCK'
  elif (computer_choice == 2):
    print '...SCISSORS'
  else:
    print '...PAPER'

  if (choice == computer_choice):
    result = 0                   # tie
  elif ((choice-computer_choice == -1) or (choice-computer_choice == 2)):
    result = 2                   # player wins
  else:
    result = 1                   # computer wins
   
  return result
 

random.seed()               # initialize random number generator to system timer
computer_wins = 0
player_wins = 0
tie_games = 0
number_games = 11

print 'GAME OF ROCK, SCISSORS, PAPER'
print 'ADAPTED FROM CREATIVE COMPUTING  MORRISTOWN, NEW JERSEY BASIC SOURCE CODE'
print '\n\n\n'

while number_games > 10:
  number_games = int(raw_input('How many games?  '))
  if number_games > 10:
    print 'SORRY, BUT WE ARE NOT ALLOWED TO PLAY THAT MANY.'
   
for i in range(1,number_games+1):
  print '\n\nGAME NUMBER ', i
  player_choice = 0
  while (player_choice < 1 or player_choice > 3):
    print '1=ROCK, 2=SCISSORS, 3=PAPER'
    player_choice = int(raw_input('1...2...3...WHAT IS YOUR CHOICE? '))
   
  print 'THIS IS MY CHOICE...' 
  game_result = rsp(player_choice)
  if (game_result == 0):
    print 'TIE GAME. NO WINNER.'
    tie_games = tie_games + 1
  elif (game_result == 1):
    print 'WOW! I WIN!'
    computer_wins = computer_wins + 1
  else:
    print 'YOU WIN!!'
    player_wins = player_wins + 1
   
print '\n\n\nHERE IS THE FINAL GAME SCORE:'
print 'I HAVE WON ', computer_wins, ' GAME(S)'
print 'YOU HAVE WON ', player_wins, ' GAME(S)'
print 'AND ', tie_games, ' GAME(S) ENDED IN A TIE.'
print 'press any key to end...'
temp = sys.stdin.read(1)