@@ -60,48 +60,43 @@ impl Game {
60
60
}
61
61
}
62
62
63
- fn start_game_if_needed ( & mut self ) {
64
- if self . state != GameState :: NotStarted {
65
- return ;
63
+ fn start_game_if_needed ( & mut self ) -> Result < ( ) , & ' static str > {
64
+ match self . state {
65
+ GameState :: Started => Ok ( ( ) ) ,
66
+ GameState :: NotStarted => {
67
+ self . stopwatch . start ( ) ;
68
+ self . state = GameState :: Started ;
69
+ Ok ( ( ) )
70
+ }
71
+ GameState :: Stopped { win : _ } => Err ( GAME_IS_ALREADY_STOPPED_ERROR ) ,
66
72
}
67
-
68
- self . stopwatch . start ( ) ;
69
- self . state = GameState :: Started ;
70
73
}
71
74
72
75
fn stop_game ( & mut self , win : bool ) {
73
76
self . stopwatch . stop ( ) ;
74
77
self . state = GameState :: Stopped { win } ;
75
78
}
76
79
77
- fn is_running ( & self ) -> bool {
78
- self . state == GameState :: Started
79
- }
80
-
81
80
fn execute_open (
82
81
& mut self ,
83
82
open_func : impl Fn ( & mut dyn Table ) -> Result < OpenInfo , & ' static str > ,
84
83
) -> Result < OpenInfo , & ' static str > {
85
- self . start_game_if_needed ( ) ;
86
-
87
- if self . is_running ( ) {
88
- let open_info = open_func ( & mut * self . table ) ?;
89
- match open_info. result {
90
- OpenResult :: WINNER => {
91
- self . state = GameState :: Stopped { win : true } ;
92
- self . stop_game ( true ) ;
93
- }
94
- OpenResult :: Boom => {
95
- self . state = GameState :: Stopped { win : false } ;
96
- self . stop_game ( false ) ;
97
- }
98
- _ => ( ) ,
99
- } ;
84
+ self . start_game_if_needed ( ) ?;
100
85
101
- Ok ( open_info)
102
- } else {
103
- Err ( GAME_IS_ALREADY_STOPPED_ERROR )
104
- }
86
+ let open_info = open_func ( & mut * self . table ) ?;
87
+ match open_info. result {
88
+ OpenResult :: WINNER => {
89
+ self . state = GameState :: Stopped { win : true } ;
90
+ self . stop_game ( true ) ;
91
+ }
92
+ OpenResult :: Boom => {
93
+ self . state = GameState :: Stopped { win : false } ;
94
+ self . stop_game ( false ) ;
95
+ }
96
+ _ => ( ) ,
97
+ } ;
98
+
99
+ Ok ( open_info)
105
100
}
106
101
107
102
pub fn open ( & mut self , row : SizeType , col : SizeType ) -> Result < OpenInfo , & ' static str > {
@@ -121,13 +116,9 @@ impl Game {
121
116
row : SizeType ,
122
117
col : SizeType ,
123
118
) -> Result < FlagResult , & ' static str > {
124
- self . start_game_if_needed ( ) ;
119
+ self . start_game_if_needed ( ) ? ;
125
120
126
- if self . is_running ( ) {
127
- self . table . toggle_flag ( row, col)
128
- } else {
129
- Err ( GAME_IS_ALREADY_STOPPED_ERROR )
130
- }
121
+ self . table . toggle_flag ( row, col)
131
122
}
132
123
133
124
pub fn width ( & self ) -> SizeType {
0 commit comments