Created
December 1, 2019 17:12
-
-
Save paniq/b3a2e4836950b1e76aa8866862e233e1 to your computer and use it in GitHub Desktop.
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
using import ..tukan.random | |
local rnd : (Random) | |
'seed rnd 1234 | |
let NUM_PLAYERS = 4 | |
let NUM_ROUNDS = 32 | |
let START_MONEY = 100 | |
local pots = | |
arrayof i32 | |
va-map | |
inline (i) START_MONEY | |
va-range NUM_PLAYERS | |
local rounds_played = 0 | |
:: done | |
for i in (range NUM_ROUNDS) | |
rounds_played += 1 | |
print "round" (i + 1) | |
for a in (range NUM_PLAYERS) | |
if ((pots @ a) == 0) | |
# player is broke | |
continue; | |
let ofs = ('range rnd (NUM_PLAYERS - 1)) | |
let b = | |
loop (k = 0) | |
if (k == NUM_PLAYERS) | |
# no players left to play with | |
merge done | |
b := (a + 1 + k + ofs) % NUM_PLAYERS | |
if ((b != a) & ((pots @ b) != 0)) | |
break b | |
k + 1 | |
assert (a != b) | |
smaller-pot := (min (pots @ a) (pots @ b)) | |
half := (smaller-pot + 1) // 2 | |
winner := ('range rnd 2) | |
won := winner == 0 | |
print "player" (a + 1) "played with player" (b + 1) "and" | |
? won "won" "lost" | |
half | |
"points" | |
half := (? won half -half) | |
pots @ a += half | |
pots @ b -= half | |
done :: | |
print "outcome after" rounds_played "rounds:" | |
for i in (range NUM_PLAYERS) | |
print "player" (i + 1) "has" (pots @ i) "points" | |
""""example output: | |
outcome after 22 rounds: | |
player 1 has 0 points | |
player 2 has 0 points | |
player 3 has 0 points | |
player 4 has 400 points |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment