-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbowling.go
114 lines (100 loc) · 2.06 KB
/
bowling.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
103
104
105
106
107
108
109
110
111
112
113
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
var endno int
func main() {
var game [ends]games
roll(10, &game[endno])
endno++
roll(7, &game[endno])
roll(2, &game[endno])
endno++
roll(8, &game[endno])
roll(2, &game[endno])
endno++
roll(1, &game[endno])
roll(6, &game[endno])
endno++
roll(9, &game[endno])
roll(0, &game[endno])
endno++
roll(8, &game[endno])
roll(1, &game[endno])
endno++
roll(0, &game[endno])
roll(10, &game[endno])
endno++
roll(6, &game[endno])
roll(2, &game[endno])
endno++
roll(10, &game[endno])
endno++
roll(10, &game[endno])
roll(3, &game[endno])
roll(10, &game[endno])
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 {
if endno == 9 && game.ball1 == 10 && game.ball2 != 0 {
game.fill = pins
} else {
game.ball2 = pins
game.score = pins + game.ball1
if game.score == maxscore {
game.outcome = spare
} else {
game.outcome = open
}
}
}
// fmt.Printf("In func roll endno = %v, outcome = %v, Ball1 = %v, Ball2 = %v, Fill = %v, pins = %v\n", endno, game.outcome, game.ball1, game.ball2, game.fill, pins)
}
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
}
}
if i == 9 {
game[i].score = game[i].score + game[i].fill
}
}
fmt.Println(game)
for i := 0; i < ends; i++ {
finalscore += game[i].score
}
return finalscore
}