-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtournament.js
108 lines (83 loc) · 2.11 KB
/
tournament.js
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
// #save
class Tournament {
constructor(players = 'AB'.split(''), entryfee = 1) {
this.players = players //16, 32, 64, 128
this.entryfee = entryfee
this.matches = []
this.outcomes = [players]
this.rounds = []
}
get games() {
return this.players.length - 1
}
generateMatchups(players) {
let shuffled = shuffle(players)
let matches = []
for (let i = 0; i < shuffled.length - 1; i += 2) {
matches.push({
player1: shuffled[i],
player2: shuffled[i+1],
})
}
return matches
}
determineWinners(matches) {
const outcomes = []
for (let matchup of matches) {
let winner = shuffle(Object.values(matchup))[0]
outcomes.push(winner)
}
return outcomes
}
play() {
let abc = this.generateMatchups(this.players)
return this.determineWinners(abc)
}
get length() {return this.matches.length}
run() {
while (this.players.length > 1) {
let round = this.play()
this.outcomes.push(round)
this.players = round
}
}
get getruns() {
this.run()
return this.outcomes
}
}
let t = new Tournament()
let rounds = t.getruns
function shuffle(arr) {
for (let i = arr.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[arr[i], arr[j]] = [arr[j], arr[i]];
}
return arr;
}
function clone(arr) {
return [...arr] // Why is this considered a shallow form of cloning and not a deep form?
}
function tour(players) {
let copy = clone(players) // make a copy of the initial arry
let shuffled = shuffle(copy) //shuffle the array
let matches = []
let outcomes = []
let rounds = []
while (copy.length > 1) {
for (let i = 0; i < shuffled.length - 1; i += 2) {
matches.push({
player1: shuffled[i],
player2: shuffled[i+1],
})
}
for (let matchup of matches) {
let winner = shuffle(Object.values(matchup))[0]
outcomes.push(winner)
}
rounds.push(outcomes)
copy = players
}
return rounds
// Well it can certainly be better done.
}