-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtournament.groovy
133 lines (115 loc) · 2.49 KB
/
tournament.groovy
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
def thePlayers = [
// players here
players.Yakko,
players.Santa,
players.Faisal,
players.Fubar,
// cheatbots
// players.Matthew,
// players.Metatron,
// dummy bots
players.Bait,
players.Beatfreak,
players.DeBruijn,
players.Dunno,
players.Flat,
players.Foxtrot,
players.Goldfish,
players.Henny,
players.Lisa,
players.Paisley,
players.Pat,
players.Pie,
players.Rock,
players.Rot,
players.Shotgun
]
def rounds = 1000
def size = thePlayers.size()
def indices = 0 ..< size
def grid = ([0] * size * size).collate(size)
def playMatch = { player1, player2 ->
(1 .. rounds)
.inject([[], []], { hands, ignore ->
def hands1 = hands[0]
def hands2 = hands[1]
def hand1 = player1.hand(hands1, hands2) % 3
def hand2 = player2.hand(hands2, hands1) % 3
[hands1 + hand1, hands2 + hand2]
})
}
def playTournament = { players ->
indices.collect({ index1 ->
indices.collect({ index2 ->
// only play top-right diagonal half
if (index1 >= index2) 0 else
playMatch(players[index1], players[index2])
})
})
}
def scoreHand = { hands ->
(hands[0] - hands[1] + 4) % 3 - 1
}
def add = { x, y ->
x + y
}
def scoreMatch = { hands ->
if (hands == 0) 0 else
hands
.transpose()
.collect(scoreHand)
.inject(add)
}
def scoreMatches = { tournament ->
for (x in indices) {
for (y in indices) {
if (x < y)
grid[x][y] = scoreMatch(tournament[x][y])
else if (x > y)
grid[x][y] = -grid[y][x]
}
}
grid
}
def rankTournament = { tournament ->
tournament.collect({ player ->
player.inject(add)
})
}
def scoreTournament = { players, tournament ->
def scores = scoreMatches(tournament)
def totals = [indices, rankTournament(scores), players]
.transpose()
.sort({ a, b -> b[1] <=> a[1]})
.transpose()
def order = totals[0]
def ranks = totals[1]
def orderedPlayers = totals[2]
def newscores = order.collect({ x ->
order.collect({ y ->
if (x==y) 'x' else
scores[x][y]
})
})
[orderedPlayers, ranks, newscores]
.transpose()
}
def prettyLine = { line ->
line[0].name.split('\\.')[1].padRight(16) +
line[1].toString().padLeft(8) +
line[2].collect({ item ->
item.toString().padLeft(6)
})
.join('')
}
def printTournament = { tournament ->
'name score' +
indices.collect({ index -> (index + 1).toString().padLeft(6)}).join('') +
'\n' +
tournament
.collect(prettyLine)
.join('\n')
}
def tournament = playTournament(thePlayers)
println('Backbase RoShamBo Tournament results\n')
println(printTournament(scoreTournament(thePlayers, tournament)))