I have to Implement function rps() that takes the choice ('R', 'P', or 'S') of player 1 and the choice of player 2, and returns -1 if player 1 wins, 1 if player 2 wins, or 0 if there is a tie. I wrote out my code for comparison but its not working properly, should I implement a set or dictionary to make it properly work?
if it's java code, upload it.. I'd take a look at it.
I would use a dictionary let p1 is the choice of player 1 p2 is the choice of player 2 RPS_outcome ={SS:0, SP:-1, SR:1, PS:1, PP:0, PR:-1, RS:-1, RP:1, RR:0) then you can return RPS_outcome[p1+p2]
another option is of course to write it all out in if else statements ``` if p1 == p2: return 0 elif p1 == 'S': if p2 == 'P': return -1 else: return 1 elif p1 == 'P': if p2 == 'R': return -1 else: return 1 elif p1 == 'R': if p2 == 'S': return -1 else: return 1 ```
does java have 'dictionaries'?
@abster
I thought this course uses python, since the title is MIT 6.189 A Gentle Introduction to Programming Using Python (OCW) Subtopics I hardly know any Java, so I don't know if java has dictionaries However googling it gives me for instance http://docs.oracle.com/javase/7/docs/api/java/util/Dictionary.html So I think java indeed has dictionaries
thanks abster. I wasn't aware that this was in the python sub-catagory.. all the questions seem to meld into the same bucket lol.
Yah, a subcategory on a Java course (OCW) would be nice
Join our real-time social learning platform and learn together with your friends!