Testing for Rock, Paper, Scissors win with simple arithmetic

Feb 14, 2006 12:12

Let Paper, Rock and Scissors be represented by 0, 1, and 2 respectively and let the two players be a and b. Win states can be found by subtracting b from a. If the result is -1 or 2, a wins. If the result is -2 or 1, then b wins. If the numbers are equal, it's obviously a tie.

Leave a comment

Comments 3

synphreak February 14 2006, 22:30:16 UTC
#define Paper 0
#define Rock 1
#define Scissors 2

int a = GetRPSInput();
int b = GetRPSInput();

char winner;

switch (a - b)
{
case -1:
case 2:
winner='a';
break;
case 1:
case -2:
winner='b';
break;
default:
winner='t';
}

// bored

Reply

wisedonkey February 15 2006, 17:06:37 UTC
Or in Lisp:

(defun test-for-win (a b)
(let ((r (- a b)))
(cond
((= a b) :tie)
((or (= r -1) (= r 2)) :a)
(t :b))))

Reply

Jake's crappy implementation, converted to Lisp wisedonkey February 15 2006, 17:13:06 UTC


(defun brute-for-win (a b)
(cond
((= a b) :tie)
((or
(and (= a 0) (= b 1))
(and (= a 1) (= b 2))
(and (= a 2) (= b 0))) :a)
(t :b)))

Reply


Leave a comment

Up