Created
November 2, 2011 05:04
-
-
Save bizenn/1332908 to your computer and use it in GitHub Desktop.
Janken script supporting more than two hands
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; -*- mode: scheme; coding: utf-8 -*- | |
(use util.match) | |
(define (which-win hand-left hand-right) | |
(match (list hand-left hand-right) | |
[(or ('g 'g) ('p 'p) ('c 'c)) #f] | |
[(or ('g 'p) ('p 'c) ('c 'g)) hand-right] | |
[(or ('p 'g) ('c 'p) ('g 'c)) hand-left] | |
[else (errorf "Unknown hand: left ~a, right ~a" hand-left hand-right)])) | |
;; gosh> (janken 'g 'p) | |
;; p | |
;; gosh> (janken 'g 'p 'g) | |
;; p | |
;; gosh> (janken 'g 'p 'g 'c) | |
;; #f | |
(define (janken . args) | |
(let1 t (make-hash-table 'eq?) | |
(for-each (cut hash-table-put! t <> #t) args) | |
(let1 hands (hash-table-keys t) | |
(if (= 2 (length hands)) | |
(apply which-win hands) | |
#f)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment