Rock Paper Scissors
December 10, 2013
This isn’t hard. But it is tedious. The call-with-current-continuation
sets up for the early loop exit when the player quits.
(define (rps)
(display "Welcome to Rock Paper Scissors. ") (newline)
(call-with-current-continuation (lambda (return)
(let ((cscore 0) (hscore 0))
(let play ((cthrow (list-ref '(rock paper scissors) (randint 3)))
(hthrow 'none))
(do () ((member hthrow '(rock paper scissors quit)))
(display "Enter R, P or S to make your throw, or Q to quit: ")
(let ((h (read)))
(case (string-ref (symbol->string h) 0)
((#\r #\R) (set! hthrow 'rock))
((#\p #\P) (set! hthrow 'paper))
((#\s #\S) (set! hthrow 'scissors))
((#\q #\Q) (display "Thanks for playing.")
(newline)(return)))))
(display " I throw ") (display cthrow)
(cond ((and (eq? cthrow 'rock) (eq? hthrow 'paper))
(set! hscore (+ hscore 1))
(display ". Your paper wraps my rock. You win."))
((and (eq? cthrow 'paper) (eq? hthrow 'rock))
(set! cscore (+ cscore 1))
(display ". My paper wraps your rock. I win."))
((and (eq? cthrow 'paper) (eq? hthrow 'scissors))
(set! hscore (+ hscore 1))
(display ". Your scissors cuts my paper. You win."))
((and (eq? cthrow 'scissors) (eq? hthrow 'paper))
(set! cscore (+ cscore 1))
(display ". My scissors cuts your paper. I win."))
((and (eq? cthrow 'scissors) (eq? hthrow 'rock))
(set! hscore (+ hscore 1))
(display ". Your rock blunts my scissors. You win."))
((and (eq? cthrow 'rock) (eq? hthrow 'scissors))
(set! cscore (+ cscore 1))
(display ". My rock blunts your scissors. I win."))
((eq? cthrow hthrow)
(display ". Our throws are the same. It's a tie.")))
(newline) (display " ")
(cond ((< cscore hscore)
(display "You are winning: ") (display hscore)
(display " to ") (display cscore) (display "."))
((< hscore cscore)
(display "I am winning: ") (display cscore)
(display " to ") (display hscore) (display "."))
(else (display "The score is tied at ")
(display cscore) (display ".")))
(newline)
(play (list-ref '(rock paper scissors) (randint 3)) 'none))))))
We used randint
from the Standard Prelude. You can run the program at http://programmingpraxis.codepad.org/LZRKWkz9.
Bonus points if the program uses a webcam to receive input?
Haskell version (requires GHC, doesn’t run in codepad :( ). Not the most beautiful program in Haskell, but nevertheless
http://codepad.org/3oEk8lKq
https://github.com/notsoluckycharm/samples/tree/master/php/rock-paper-scissors
In Python.
Badly written Clojure… /
Not keeping score, so I lose, but anyway:
#! /usr/bin/env gsi-script
(display "Content-Type: text/plain") (newline)
(newline)
(random-source-randomize! default-random-source)
(define parser '(("move=rock" . 0) ("move=paper" . 1) ("move=scissors" . 2)))
(define writer
'((0 . ((0 . "rock ties with rock") (1 . "paper wraps rock") (2 . "rock blunts scissors")))
(1 . ((1 . "paper ties with paper") (2 . "scissors cut paper")))
(2 . ((2 . "scissors tie with scissors")))))
(define (judge client server)
(cond
((= client server) " - it's a tie")
((= (modulo (+ client 1) 3) server) " - server wins")
((= client (modulo (+ server 1) 3)) " - client wins")))
(define client-move (cdr (assoc (getenv "QUERY_STRING") parser)))
(define server-move (random-integer 3))
(define lesser (cdr (assoc (min client-move server-move) writer)))
(display (list-ref '("rock! " "paper! " "scissors! ") server-move))
(display (cdr (assoc (max client-move server-move) lesser)))
(display (judge client-move server-move))
(newline)
It’s a Gambit CGI script. With a web server running locally in port 8080 and accepting CGI programs in cwd, play like this:
$ curl http://localhost:8080/rock.cgi'?'move=paper
rock! paper wraps rock - client wins
any java code for rock paper scissors.. it would be two clients and a server?
Here’s a Racket version with a bunch of different AIs to play against (or against each other): Rock-paper-scissors on jverkamp.com.
Granted, none of them are that smart (I’m considering writing one based on a Hidden Markov Model), but it’s still pretty neat.
Here is a PHP version.
Not too happy with a couple of sections, but overall not bad. https://github.com/lmon/samples/blob/master/RockPaperScissors.php