-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatch.groovy
51 lines (42 loc) · 1.09 KB
/
match.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
// this is you
def you = players.Player1
def opponent = players.Rock
def rounds = 100
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 scoreHand = { hand1, hand2 ->
(hand1 - hand2 + 4) % 3 - 1
}
def add = { x, y ->
x + y
}
def scoreMatch = { hands1, hands2 ->
[hands1, hands2]
.transpose()
.collect({ hand ->
scoreHand(hand[0], hand[1])
})
}
def match = playMatch(you, opponent)
def yourhands = match[0];
def opponenthands = match[1]
def scorelist = scoreMatch(yourhands, opponenthands)
def score = scorelist.inject(add)
println('match results\n')
println(you.name.split('\\.')[1].padRight(16) + yourhands.join(''))
println(opponent.name.split('\\.')[1].padRight(16) + opponenthands.join(''))
println('score'.padRight(16) + scorelist.collect({ s ->
if (s == -1) '-' else
if (s == 1) '+' else
' '
}).join(''))
println()
println('total'.padRight(16) + score)