Skip to content

Instantly share code, notes, and snippets.

@bizenn
Created November 2, 2011 05:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bizenn/1332908 to your computer and use it in GitHub Desktop.
Save bizenn/1332908 to your computer and use it in GitHub Desktop.
Janken script supporting more than two hands
;;; -*- 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