rev: 3827f53bae88e48a594610914ba74b9512ead6dd tukan/testing/oligarchy.sc -rw-r--r-- 1.5 KiB View raw Log this file
3827f53bae88 — Leonard Ritter * more work on module system 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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