<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2900.2873" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face="Courier New" size=2>Let me start by saying that your code is
pretty </FONT></DIV>
<DIV><FONT face="Courier New" size=2>much OK as is. Indeed even using clssses
for an </FONT></DIV>
<DIV><FONT face="Courier New" size=2>RPS game is probably overkill to some
extent.</FONT></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><FONT face="Courier New" size=2>What follows should be seen more as some
ideas </FONT></DIV>
<DIV><FONT face="Courier New" size=2>for experimentation and building towards a
more </FONT></DIV>
<DIV><FONT face="Courier New" size=2>flexible/expandable architecture for future
</FONT></DIV>
<DIV><FONT face="Courier New" size=2>projects. Its really about introducing some
</FONT></DIV>
<DIV><FONT face="Courier New" size=2>alternative approaches and increasing
consistency.</FONT></DIV>
<DIV><FONT face="Courier New" size=2>It is definitely not "better" - and indeed
could </FONT></DIV>
<DIV><FONT face="Courier New" size=2>definitely be improved in several
ways.</FONT></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><FONT face="Courier New" size=2>> class Human:<BR>> def
__init__(self):<BR>> self.points =
0<BR>> self.choice = " "<BR>> <BR>>
def plays(self):<BR>> fromUser =
raw_input("Pick (R)ock, (P)aper,
or<BR>>
(S)cissors! ")<BR>> translate = {'r':'rock',
's':'scissors',<BR>>
'p':'paper'} <BR>>
try:<BR>> self.choice =
translate[fromUser.lower()]<BR>> except
KeyError:<BR>> print 'Invalid
Response'<BR>> <BR>> class Computer:<BR>> def
__init__(self):<BR>> self.points =
0<BR>> self.choice = " "<BR>>
<BR>> def plays(self):<BR>>
comp_choice = random.randint(0,2)<BR>> if comp_choice
== 0:<BR>> self.choice =
'rock'<BR>> elif comp_choice ==
1:<BR>> self.choice =
'paper'<BR>>
else:<BR>> self.choice =
'scissors'<BR></DIV></FONT>
<DIV><FONT face="Courier New" size=2>While its of little real advantage for
this program </FONT></DIV>
<DIV><FONT face="Courier New" size=2>I'd be </FONT><FONT face="Courier New"
size=2>tempted </FONT><FONT face="Courier New" size=2>to introduce a Player
superclass to avoid </FONT></DIV>
<DIV><FONT face="Courier New" size=2>code duplication. </FONT><FONT
face="Courier New" size=2>The protocol of both classes </FONT></DIV>
<DIV><FONT face="Courier New" size=2>here is the same and they both pass the
"is-a" </FONT></DIV>
<DIV><FONT face="Courier New" size=2>test for inheritance - ie. they are both
Players.</FONT></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><FONT face="Courier New" size=2>class Player:</FONT></DIV>
<DIV><FONT face="Courier New" size=2> def
__init__(self):</FONT></DIV>
<DIV><FONT face="Courier New" size=2> self.points =
0<BR> self.choice = " "</FONT></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><FONT face="Courier New" size=2> def getChoice(self):
pass # implemented in subclass</FONT></DIV>
<DIV><FONT face="Courier New" size=2><BR> def
plays(self):</FONT></DIV>
<DIV><FONT face="Courier New" size=2> choice =
self.getChoice()<BR> if choice ==
0:<BR> self.choice =
'rock'<BR> elif choice ==
1:<BR> self.choice =
'paper'<BR>
else:<BR> self.choice =
'scissors'<BR></DIV></FONT>
<DIV><FONT face="Courier New" size=2>Notice I put the plays function in the
Player class.</FONT></DIV>
<DIV><FONT face="Courier New" size=2>By unifying the representation of choice we
can use </FONT></DIV>
<DIV><FONT face="Courier New" size=2>that function for both and just implement a
</FONT></DIV>
<DIV><FONT face="Courier New" size=2>relatively simple getChoice method in each
subclass </FONT></DIV>
<DIV><FONT face="Courier New" size=2>and we are done... (In fact we could
simplify still </FONT></DIV>
<DIV><FONT face="Courier New" size=2>further by storing the choice in its
numeric form </FONT></DIV>
<DIV><FONT face="Courier New" size=2>and using a class level dictionary to
translate </FONT></DIV>
<DIV><FONT face="Courier New" size=2>that to a string for printing - although in
fact </FONT></DIV>
<DIV><FONT face="Courier New" size=2>you never print what the choices were -
maybe you </FONT></DIV>
<DIV><FONT face="Courier New" size=2>should?)</FONT></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><FONT face="Courier New" size=2>class Human(Player):</FONT></DIV>
<DIV><FONT face="Courier New" size=2> def
getChoice(self):</FONT></DIV>
<DIV><FONT face="Courier New" size=2> choice =
raw_input('Choice - rps: ').lower()</FONT></DIV>
<DIV><FONT face="Courier New" size=2> if choice in
'rps':</FONT></DIV>
<DIV><FONT face="Courier New"
size=2> return
{'r':0,'p':1,'s':2}[choice]</FONT></DIV>
<DIV><FONT face="Courier New" size=2> else: raise
ValueError</FONT></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><FONT face="Courier New" size=2>class Computer(Player):</FONT></DIV>
<DIV><FONT face="Courier New" size=2> def
getChoice(self):</FONT></DIV>
<DIV><FONT face="Courier New" size=2> return
randrange(0,3)</FONT></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><FONT face="Courier New" size=2>> <BR>> def compare_objects(human,
computer):<BR>> print "Human picked ",
human.choice<BR>> print "Computer picked",
computer.choice<BR>> results = { 'rock' : {'rock' : 'draw',
'paper':
0,<BR>> 'scissors':
1},<BR>>
'paper' : {'rock' : 1, 'paper': 'draw',
'scissors':<BR>>
0},<BR>>
'scissors' : {'rock' : 0, 'paper' : 1, 'scissors'
:<BR>> 'draw'}<BR>>
}<BR>> <BR>> outcome =
results[human.choice][computer.choice]<BR>> if outcome ==
0:<BR>> print "Computer
Wins!"<BR>> computer.points = computer.points +
1<BR>> elif outcome ==
1:<BR>> print "Human
Wins!"<BR>> human.points = human.points +
1<BR>> else:<BR>> print
"Draw!"</FONT></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><FONT face="Courier New" size=2>By using the number format of storage and
the </FONT></DIV>
<DIV><FONT face="Courier New" size=2>dictionary in the class you can simplify
that</FONT></DIV>
<DIV><FONT face="Courier New" size=2>as in show below using
lists.</FONT><FONT><BR><BR><FONT face="Courier New" size=2>> I actually
figured out how to build the 2x2 matrix. <BR>> I'm quite proud of this.
</FONT></FONT></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><FONT face="Courier New" size=2>A nested dictionary is one way. Another is
to use numbers </FONT></DIV>
<DIV><FONT face="Courier New" size=2>to represent the choices and outcomes and
create a nested </FONT></DIV>
<DIV><FONT face="Courier New" size=2>list structure </FONT><FONT
face="Courier New" size=2>nested list</FONT></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><FONT face="Courier New" size=2>rps =
{0:'Rock',1:'Paper',2:'Scissors'}</FONT></DIV>
<DIV><FONT face="Courier New" size=2>draw,loss,win = 0,1,2</FONT></DIV>
<DIV><FONT face="Courier New" size=2>outcomes = [ [0,1,2], [2,0,1], [1,2,0]
]</FONT></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><FONT face="Courier New" size=2>computer = randrange(0,3)</FONT></DIV>
<DIV><FONT face="Courier New" size=2>human = randrange(0,3)</FONT></DIV>
<DIV><FONT face="Courier New" size=2>result = outcomes[computer][human]
</FONT></DIV>
<DIV><FONT face="Courier New" size=2>if result == draw: print
'Draw'</FONT></DIV>
<DIV><FONT face="Courier New" size=2>elif result == loss: print 'Computer lost
with', rps[computer]</FONT></DIV>
<DIV><FONT face="Courier New" size=2>elif result == win: print 'Computer won
with', rps[computer]</FONT></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><FONT face="Courier New" size=2>The dictionary is more verbose but does
have more readable</FONT></DIV>
<DIV><FONT face="Courier New" size=2>code.</FONT></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><FONT face="Courier New" size=2>I hope somebody finds at least some of that
interesting! :-)</FONT></DIV>
<DIV><FONT face="Courier New" size=2>I guess what I'm really trying to show is
how </FONT></DIV>
<DIV><FONT face="Courier New" size=2>choosing different data structures makes a
big </FONT></DIV>
<DIV><FONT face="Courier New" size=2>difference to how the code
looks.</FONT></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><FONT face="Courier New" size=2>Alan G.</FONT></DIV></BODY></HTML>