[Tutor] rock, paper, scissors (ro sham bo) part deux

Sean 'Shaleh' Perry shalehperry@attbi.com
Mon Feb 3 23:47:07 2003


--------------Boundary-00=_49PRW8AZUA06T1D25V41
Content-Type: text/plain;
  charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

On Monday 03 February 2003 14:41, ahimsa wrote:
> Sean, Magnus, Alan, Bob and Gregor
>
> At the risk of belabouring this thread, I wanted to update you with
> developments on my earlier requests for input and since you all so
> graciously contributed something to helping me, I wanted to share where
> it is at (to date).

nah, this is good for the list.

One thing to remember is there are two "customers" of python tutor:

a) the people asking questions today
b) the people looking for answers tomorrow by reading the archives.

Since you asked for "how would you do it?", enclosed is a simple version =
that=20
points you there.  Feel free to ask on the list about anything you do not=
=20
understand.

You also asked about storing scores between games.  Look into "pickle" an=
d=20
pickling.

--------------Boundary-00=_49PRW8AZUA06T1D25V41
Content-Type: text/x-python;
  charset="iso-8859-1";
  name="roshambo.py"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="roshambo.py"

#!/usr/bin/python

##
#
# written by Sean 'Shaleh' Perry 2003
#
# permission is given to use, modify, redistribute this code without limitation
# this code is without warranty
#
##

import random

outcomes = { "rock": "scissors",
             "paper": "rock",
             "scissors": "paper" }

def decide_outcome(player1, player2):
    if outcomes[player1] == player2:
        return 1
    elif outcomes[player2] == player1:
        return 2
    else:
        return 0

def random_player_input():
    return random.choice(outcomes.keys())

if __name__ == '__main__':
    score = [0, 0, 0]

    get_player1_choice = random_player_input
    get_player2_choice = random_player_input
    
    rounds = 3
    counter = 0

    while counter < rounds:
        player1 = get_player1_choice()
        player2 = get_player2_choice()
        winner = decide_outcome(player1, player2)
        score[winner] += 1
        counter += 1

    print score

--------------Boundary-00=_49PRW8AZUA06T1D25V41--