Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Saturday, January 23, 2016

Rock Paper Scissors

Rock Paper Scissors


I had a test for my comp sci class and one of the questions was to make a rock paper scissors game that if player 1 won it would return -1, if player 2 won it would return 1 and if it were a tie it would return 0. I made my program and ran it and it worked but according to my professor he said it didn't.

def rps(x,y):      player1 = -1      player2 = 1      tie = 0      'R'>'S'      'P'>'R'      'S'>'P'      if x>y:          return player1      if x

I don't see what is wrong with it? It if you do rps('R','P') then it would return -1 because x = player1 and because Rock beats Paper. Can anyone help me to see if my code is wrong?

Answer by eumiro for Rock Paper Scissors


def rps(x,y):      d = {'R': 1, 'S': 2, 'P': 3}      return ((d[x] - d[y]) + 1) % 3 - 1      for p1 in 'RPS':      for p2 in 'RPS':          print p1, p2, rps(p1, p2)  

prints

R R 0  R P 1  R S -1  P R -1  P P 0  P S 1  S R 1  S P -1  S S 0  

Answer by user2032433 for Rock Paper Scissors


There are few problems in your code:

1. The following lines don't do anything. You can't set character R to be larger than character S:

'R' > 'S'  'P' > 'R'  'S' > 'P'  

2. Cause of section 1, your if x>y: doesn't do what you think it does. It simply checks if content of x is before content of y in the alphabet. (Supposing contents of x and y are characters)


Your code is kinda hard to fix as it is, I would recommend you to approach this problem from a very different angle.

Here's a solution totally different from yours, but this one works. It simply uses many if checks to get to your result.

def rps(p1, p2):      if p1 == p2:          return 0      elif (p1 == "R" and p2 == "S")\      or (p1 == "S" and p2 == "P")\      or (p1 == "P" and p2 == "R"):          return -1      else:          return 1  

I replaced x, y with p1, p2 since they represent players' choises better imo, but if you have to use x and y, just change them back.

Answer by Argeman for Rock Paper Scissors


You can do following:

def rps(p1,p2):      retval= {          'R':{'R': 0, 'S':-1, 'P': 1},          'S':{'R': 1, 'S': 0, 'P':-1},          'P':{'R':-1, 'S': 1, 'P': 0}      }      return retval[p1][p2]  

Answer by Chris Taylor for Rock Paper Scissors


You asked:

Can anyone help me see if my code is wrong?

Yes, it is wrong. Here's why.

If you run rps('R','S') you should get 1, because rock beats paper. Similarly rps('R','P') should give -1 because paper beats rock. Both of these work in your code.

However, if you run rps('S','P') you should get 1, because scissors beats paper, but you don't - your code returns -1, which is wrong.

As eumiro pointed out in the comments, the three lines

'R'>'S'  'P'>'R'  'S'>'P'  

which I assume you think are defining the ordering to be used, don't actually do anything.

Answer by dansalmo for Rock Paper Scissors


def rps(x,y):      return [0, -1, 1]['RPS'.index(x) - 'RPS'.index(y)]  

Or if you want an interactive program:

from random import randint  ['Tie', 'I win', 'You win'][randint(0, 2) - 'RPS'.index(raw_input("Enter R, P, or S: "))]  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.