@@ -7,7 +7,8 @@ pub enum Error {
7
7
pub struct BowlingGame {
8
8
frames : Vec < Frame > ,
9
9
previous_roll : Option < u16 > ,
10
- fill_ball1 : Option < u16 >
10
+ fill_ball1 : Option < u16 > ,
11
+ fill_ball2 : Option < u16 >
11
12
}
12
13
13
14
impl BowlingGame {
@@ -16,19 +17,31 @@ impl BowlingGame {
16
17
frames : vec ! ( ) ,
17
18
previous_roll : None ,
18
19
fill_ball1 : None ,
20
+ fill_ball2 : None ,
19
21
}
20
22
}
21
23
22
24
pub fn roll ( & mut self , pins : u16 ) -> Result < ( ) , Error > {
23
- // allower the user to roll again if they scored a strike or spare in the last frame.
24
- if self . frames . last ( ) . is_some_and ( |x| x. is_spare ( ) ) {
25
- self . fill_ball1 = Some ( pins) ;
26
- } else if self . is_game_over ( ) {
25
+ if self . is_game_over ( ) {
27
26
return Err ( Error :: GameComplete )
28
27
}
29
28
if pins > 10 {
30
29
return Err ( Error :: NotEnoughPinsLeft )
31
30
}
31
+
32
+ // Handle fill balls
33
+ if self . frames . last ( ) . is_some_and ( |x| x. is_spare ( ) ) {
34
+ self . fill_ball1 = Some ( pins) ;
35
+ }
36
+ if self . frames . last ( ) . is_some_and ( |x| x. is_strike ( ) ) {
37
+ if self . fill_ball1 . is_none ( ) {
38
+ self . fill_ball1 = Some ( pins) ;
39
+ } else {
40
+ self . fill_ball2 = Some ( pins) ;
41
+ }
42
+ }
43
+
44
+ // Handle normal rolls
32
45
if pins == 10 {
33
46
let frame = Frame :: new ( pins, None ) ;
34
47
self . frames . push ( frame) ;
@@ -52,8 +65,7 @@ impl BowlingGame {
52
65
for ( i, frame) in self . frames . iter ( ) . enumerate ( ) {
53
66
let next_frame = self . frames . get ( i+1 ) ;
54
67
if next_frame. is_none ( ) {
55
- // TODO: implement fill balls
56
- total += frame. score ( self . fill_ball1 . unwrap_or ( 0 ) , 0 ) ;
68
+ total += frame. score ( self . fill_ball1 . unwrap_or ( 0 ) , self . fill_ball2 . unwrap_or ( 0 ) ) ;
57
69
} else {
58
70
let next_roll1 = next_frame. unwrap ( ) . roll1 ;
59
71
let next_roll2 = next_frame. unwrap ( ) . roll2 . unwrap ( ) ;
@@ -70,6 +82,9 @@ impl BowlingGame {
70
82
if self . frames . last ( ) . is_some_and ( |x| x. is_spare ( ) ) && self . fill_ball1 . is_none ( ) {
71
83
return false
72
84
}
85
+ if self . frames . last ( ) . is_some_and ( |x| x. is_strike ( ) ) && self . fill_ball2 . is_none ( ) {
86
+ return false
87
+ }
73
88
return true
74
89
}
75
90
}
0 commit comments