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
#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
(defun test-for-win (a b)
(let ((r (- a b)))
(cond
((= a b) :tie)
((or (= r -1) (= r 2)) :a)
(t :b))))
Reply
(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