-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscenario.go
94 lines (85 loc) · 1.97 KB
/
scenario.go
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
package main
/**
* Defines a a collection of teams that can co-exsits.
* @module Scenario
*/
import (
"fmt"
"strings"
)
type Scenario struct {
delta int
high int
id string
low int
teams []Team
}
/**
* Creates a senerio object given the teams.
* @constructs
* @param {Array.<Team>} teams
* @returns {Scenario}
*/
func newScenario(teams []Team) Scenario{
sortTeams(&teams)
numTeams := len(teams)
lowIndex := 0
highIndex := 0
id := ""
for i := 0; i < numTeams; i++ {
id += teams[i].id
if teams[lowIndex].score > teams[i].score {
lowIndex = i
}
if teams[highIndex].score < teams[i].score {
highIndex = i
}
}
return Scenario{
delta: teams[highIndex].score - teams[lowIndex].score,
high: teams[highIndex].score,
id: id,
teams: teams,
low: teams[lowIndex].score,
}
}
/**
* Sort the scenarios by the difference between the worst and best teams scores
* @param {ref:Array.<Team>}
*/
func sortTeams(t *[]Team) {
n:= len(*t)
var iMin int = 0
for j := 0; j < n-1; j++ {
iMin = j
for i := j + 1; i < n; i++ {
if (*t)[i].id < (*t)[iMin].id {
iMin = i
}
}
if iMin != j {
(*t)[j], (*t)[iMin] = (*t)[iMin], (*t)[j]
}
}
}
/**
* Builds a string for displaying the players information.
* @returns {String} result
*/
func (s *Scenario) toString() (result string) {
lineLength := len(s.teams[0].players) * 14 + 20
dashLine := strings.Repeat("-", lineLength) + "\n"
for i, t := range s.teams {
sum := 0
result += fmt.Sprintf("| Team %1d| ", i)
for _, p := range(t.players) {
sum += p.score
result += fmt.Sprintf("%-10s | ", p.name)
}
result += "Score: " + fmt.Sprint(sum) + "|\n" + dashLine
}
stats := "| Stats | " + fmt.Sprintf("Low:%-7dHigh:%-7dDelta:%-7d", s.low, s.high, s.delta)
stats += strings.Repeat(" ", (lineLength - len(stats)) - 1) + "|\n"
result = dashLine + stats + dashLine + result + "\n\n"
return
}