[Tutor] rockpaperscissors

ahimsa ahimsa@onetel.net.uk
Mon Jan 27 14:25:05 2003


Hello Alan
On Mon, 2003-01-27 at 12:57, alan.gauld@bt.com wrote:

> Its not too bad given the size of the problem. You can of course 
> build on all sorts of extra sophistication, but its gardly worth 
> it IMHO.

It is functional, but only just. The code is messy and long-winded and
as both Sean and Magnus have already suggested, designing a smaller
function to do the task is just better a programming tactic.

> One area that might be more e;egant is to drive the results() function 
> from a data structure:
> 
> # results is a dictionary of dictionaries
> outcomes = ["Scissors cuts Paper", "Paper wraps Rock", "Rock smashes
> Scissors"]
> winners = ["Drawn game", "Computer won", "You won"]
> results = { 1: { 2: outcomes[1] + winners[1],
>                  3: outcomes[2] + winners[2] },
>             2: { 1: outcomes[1] + winners[2],
>                  3: outcomes[0] + winners[1]},
>             3: { 1: outcomes[2] + winners[1],
>                  2: outcomes[0] + winners[2]}
>           }
> 
> Now your function just becomes a case of looking up the dictionary 
> and printing the result. But its not that much prettier and only 
> saves a tiny bit of maintenance effort.

Hmmm ... It took me a while but I think that I can follow what you're
suggesting here. No offence, but it still looks a little clumsy or
awkward, which is one of the things I didn't feel satisfied about with
the example code I presented ... that and also because I couldn't add
the score counter to it. 


> There are 3 outcomes - Computer Wins, User wins, or draw. 
> You can return that from the return function by making the 
> return value a tuple containing the result code and result 
> string. 
> 
> Represent that with 0,1,2 - which coincidentally(!) are the 
> indices of the winners string list above, making translation 
> easy...

This is true enough ... coincidentally??? ;-)

> Then use a line in the loop like:
> 
> if result[0] == -1: draw += 1
> elif result[0] == 0: computer += 1
> else user += 1

This bottom section might do the trick actually, so I'm going to file
this part away along with a couple of the other suggestions and play
around with it some more and see what I can get to work. Thanks for your
suggestion Alan.

Best wishes

Andrew

-- 
ahimsa <ahimsa@onetel.net.uk>