-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbowling_temp.go
102 lines (91 loc) · 1.7 KB
/
bowling_temp.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
95
96
97
98
99
100
101
102
package main
import (
"fmt"
)
type games struct {
ball1 int
ball2 int
fill int
score int
outcome string
}
const ends = 10
const maxscore = 10
const strike = "Strike"
const spare = "Spare"
const open = "Open"
var game [ends]games
var rollno int
func main() {
var game [ends]games
endno := 0
roll(8, &game[endno])
roll(2, &game[endno])
endno++
roll(7, &game[endno])
roll(1, &game[endno])
endno++
roll(10, &game[endno])
endno++
roll(10, &game[endno])
endno++
roll(3, &game[endno])
roll(7, &game[endno])
endno++
roll(5, &game[endno])
roll(4, &game[endno])
endno++
roll(5, &game[endno])
roll(4, &game[endno])
endno++
roll(10, &game[endno])
endno++
roll(10, &game[endno])
endno++
roll(10, &game[endno])
roll(10, &game[endno])
roll(10, &game[endno])
//fmt.Println(game)
finalscore := score(game)
fmt.Println(finalscore)
}
func roll(pins int, game *games) {
if game.outcome == "" {
game.ball1 = pins
game.score = pins
if game.score == maxscore {
game.outcome = strike
} else {
game.outcome = "Ball1"
}
} else {
game.ball2 = pins
game.score = pins + game.ball1
if game.score == maxscore {
game.outcome = spare
} else {
game.outcome = open
}
}
}
func score(game [10]games) int {
finalscore := 0
for i := 0; i < ends; i++ {
if game[i].outcome == spare {
game[i].score = game[i].score + game[i+1].ball1
}
if game[i].outcome == strike {
game[i].score = game[i].score + game[i+1].ball1
if game[i+1].outcome == strike {
game[i].score = game[i].score + game[i+2].ball1
} else {
game[i].score = game[i].score + game[i+1].ball2
}
}
}
fmt.Println(game)
for i := 0; i < ends; i++ {
finalscore += game[i].score
}
return finalscore
}