-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added the ultimatum game and roshambo
- Loading branch information
Showing
5 changed files
with
120 additions
and
4 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
(in-package #:tpd2.game.roshambo) | ||
|
||
(defvar *objects* '(rock paper scissors)) | ||
|
||
(defgame roshambo () | ||
() | ||
(defplayer () | ||
(choice)) | ||
(:game-name "Rock, Paper, Scissors") | ||
(:game-description | ||
(with-ml-output | ||
"Two players simultaneously choose either rock, paper or scissors. Rock blunts scissors, scissors cut paper, and paper covers rock. Also called roshambo." (<br) | ||
"Read more at " | ||
(<a :href "http://en.wikipedia.org/wiki/Rock,_Paper,_Scissors" "Wikipedia") "."))) | ||
|
||
(my-defun roshambo 'play () | ||
(with-game | ||
(my new-state) | ||
(with-join-spawn/cc () | ||
(loop for p in (my players) | ||
do | ||
(let-current-values (p) | ||
(spawn/cc () | ||
(setf (its choice p) (my secret-move :select p `(:one ,@*objects*))))))) | ||
|
||
(loop for p in (my players) | ||
do (my announce :select :player p :selection (its choice p))) | ||
|
||
(let ((winner | ||
(without-call/cc | ||
(flet ((v (c) | ||
(position c *objects*))) | ||
(loop for p in (my players) | ||
for vp = (v (its choice p)) | ||
thereis (loop for q in (my players) | ||
for vq = (v (its choice q)) | ||
thereis (when (= vp (mod (1+ vq) (length *objects*))) | ||
p))))))) | ||
(cond (winner | ||
(my finished :winner winner)) | ||
(t | ||
(my finished :result :draw)))))) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
(in-package #:tpd2.game.ultimatum) | ||
|
||
(defvar *max-pot* 20) | ||
(defvar *min-pot* 1) | ||
(defvar *max-penalty* 4) | ||
(defvar *min-penalty* 0) | ||
|
||
(defgame ultimatum (coin-game) | ||
((pot (random-between *min-pot* *max-pot*)) | ||
(penalty (random-between *min-penalty* *max-penalty*))) | ||
(defplayer () | ||
((demand 0))) | ||
(:game-name "The Ultimatum Game") | ||
(:game-description | ||
(with-ml-output | ||
"There is a pot of coins to be shared. The first player decides how to share them. The second player can accept the choice, and receive the allotment, or reject it, in which case the players will both be fined." (<br) | ||
"Read more at " | ||
(<a :href "http://en.wikipedia.org/wiki/Ultimatum_game" "Wikipedia") "."))) | ||
|
||
(my-defun ultimatum 'play () | ||
(with-game | ||
(my new-state) | ||
(destructuring-bind (proposer acceptor) | ||
(random-shuffle (my players)) | ||
(with-its-type (proposer ultimatum-player) | ||
(with-its-type (acceptor ultimatum-player) | ||
|
||
(let ((demand (my move :select-demand proposer `(:integer 0 ,(my pot))))) | ||
(let ((ok (my move :cooperate acceptor :boolean))) | ||
(cond (ok | ||
(its give-coins proposer demand) | ||
(its give-coins acceptor (- (my pot) demand)) | ||
(my finished :result :sharing)) | ||
(t | ||
(its give-coins proposer (- (my penalty))) | ||
(its give-coins acceptor (- (my penalty))) | ||
(my finished :result :penalty)))))))))) | ||
|
||
(my-defun ultimatum 'object-to-ml () | ||
(flet ((coins (c) (format nil "~R coin~:P" c))) | ||
(with-ml-output | ||
(call-next-method) | ||
(<h3 "Pot: " (my pot) ", penalty: " (my penalty) ".") | ||
(<p "The first player demands some portion of " (coins (my pot)) ". The second player will get the rest, " | ||
"but if he or she is unhappy with this division then both players lose " (coins (my penalty)) ". ")))) | ||
|
||
|
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