Rock, Paper, Scissors. Determine win/loss/tie using math?
Asked Answered
D

6

7

So I was writing a rock paper scissors game when I came to writing this function:

a is player one's move, b is player two's move. All I need to figure out is if player one won, lost, or tied.

//rock=0, paper=1, scissors=2
processMove(a, b) {
    if(a == b) ties++;
    else {
             if(a==0 && b==2) wins++;
        else if(a==0 && b==1) losses++;
        else if(a==1 && b==2) losses++;
        else if(a==1 && b==0) wins++;
        else if(a==2 && b==1) wins++;
        else if(a==2 && b==0) losses++;
    }
}

My question is: What's the most elegant way this function can be written?

Edit: I'm looking for a one-liner.

Distillery answered 7/7, 2012 at 17:21 Comment(2)
Looking for readable correct code is much better than looking for one-liners.Obligee
Have a look at my answer to a similar question: https://mcmap.net/q/1116288/-scalable-solution-for-rock-paper-scissorCapsulate
C
18
if (a == b) ties++;
else if ((a - b) % 3 == 1) wins++;
else losses++;

I need to know exactly which language you are using to turn it into a strictly one-liner...

For JavaScript (or other languages with strange Modulus) use:

if (a == b) ties++;
else if ((a - b + 3) % 3 == 1) wins++;
else losses++;
Comedic answered 7/7, 2012 at 17:50 Comment(4)
rock vs scissors doesn't work: (0 - 2 % 3) = -2. Using javascriptDistillery
Looks like it's missing a pair of parenthesis: ((a-b) % 3 == 1). In C operators modulo is higher in order of operations than subtraction.Trigeminal
It's not a strange modulus its: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… > While in most languages, % is a remainder operator, in some (e.g. Python, Perl) it is a modulo operator. For two values of the same sign, the two are equivalent, but when the operands are of different signs, the modulo result always has the same sign as the divisor, while the remainder has the same sign as the dividend, which can make them differ by one unit of the divisor.Turpin
For languages where % is a remainder operator, use ((n % d) + d) % d or in this case (((a - b) % 3) + 3) % 3Turpin
A
8

A 3x3 matrix would be "more elegant", I suppose.

char result = "TWLLTWWLT".charAt(a * 3 + b);

(Edited: Forgot that a and b were already zero-origin.)

Amply answered 7/7, 2012 at 17:29 Comment(0)
C
2

I suppose you could use the terniary operator like this -

if (b==0) a==1? wins++ : loss++;

if (b==1) a==1? loss++ : wins++;

if (b==2) a==1? loss++ : wins++;
Crookes answered 7/7, 2012 at 17:32 Comment(0)
H
2

You can do it with a simple mathematical formula to get the result and then compare with if like this:

var moves = {
  'rock': 0, 
  'paper': 1,
  'scissors': 2
};
var result = {
  'wins': 0,
  'losses': 0,
  'ties': 0
};
var processMove = function (a, b) {
  var processResult = (3 + b - a) % 3;
  if (!processResult) {
    ++result['ties'];
  } else if(1 == processResult) {
    ++result['losses'];
  } else {
    ++result['wins'];
  }
  return result;
};

jsFiddle Demo


One line processMove function without return:

var processMove = function (a, b) {
  ((3 + b - a) % 3) ? 1 == ((3 + b - a) % 3) ? ++result.losses : ++result.wins : ++result.ties;
};
Hallway answered 7/7, 2012 at 18:13 Comment(0)
D
1

how do you do it in java?

result = (comp - x ) % 3 ;

System.out.println (result);
 if (result == 0 )// if the game is tie
 {
     System.out.println ("A Tie!") ;
 }

 else if (result == 1 || result == 2 )
 {
    //System.out.println (user + " " +   "beats" + " " + computer_choice + " you win" );
     System.out.println ("comp win");
 }

 else
 {
     System.out.println ("you win");
    //System.out.println (computer_choice  + " " +  "beats" + " " + user + "you lose");
 }
Dacy answered 18/10, 2014 at 16:51 Comment(0)
S
-1

Maybe this will come in handy someone (JavaScript):

if (a == b) ties++;
else if ((a - b) == 1 || (b - a) == 2) losses++;
else wins++;

PHP:

function check($a, $b) {
    if ($a == $b) {
        return "Tie";
    }
    if (($a - $b) == 1 || ($b - $a) == 2) {
        return "You Lose";
    } else 
        return "You Win";
}
Superposition answered 17/4, 2023 at 13:42 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.