@@ -167,45 +167,42 @@ void LightsOutGame::eventOccured(const SDL_Event* const event) {
167167
168168
169169void LightsOutGame::getMoveHint (unsigned int & suggestedX, unsigned int & suggestedY) const {
170+ bool won = winningState ();
171+ if (won) {
172+ std::clog << SDL_GetTicks () << " (" << this << " ): Game has already been won, providing bogus hint of (0,0)." << std::endl;
173+ suggestedX = 0 ;
174+ suggestedY = 0 ;
175+ return ;
176+ }
177+
170178 SDL_mutexP (paintMutex);
171- for (unsigned int y=0 ; y<lights->getHeight (); y++) {
172- for (unsigned int x=0 ; x<lights->getWidth (); x++) {
173- if (lights->getTile (x,y)->object ->isLightOn ()) {
174- if (y+1 != lights->getHeight ()) {
175- // "Chase the lights"
176- suggestedX = x;
177- suggestedY = y+1 ;
178- SDL_mutexV (paintMutex);
179- return ;
180- }
181- else {
182- // Fixing the top
183- suggestedY = 0 ;
184- switch (x) {
185- case 0 : suggestedX = 1 ; SDL_mutexV (paintMutex); return ;
186- case 1 : suggestedX = 0 ; SDL_mutexV (paintMutex); return ;
187- case 2 : suggestedX = 3 ; SDL_mutexV (paintMutex); return ;
188- }
189- }
190- }
179+ bool moveAvailable = false ;
180+ for (unsigned int x=0 ; x<lights->getWidth (); x++) {
181+ for (unsigned int y=0 ; y<lights->getHeight (); y++) {
182+ moveAvailable = moveAvailable || lights->getTile (x,y)->object ->shouldPress ();
191183 }
192184 }
193-
194- // There's no reasonable "hint" for a board that has already won ;)
195- if (winningState ()) {
196- std::clog << SDL_GetTicks () << " (" << this << " ): Game has already been won, providing bogus hint of (0,0)." << std::endl;
185+ SDL_mutexV (paintMutex);
186+ if (!moveAvailable) {
187+ std::clog << SDL_GetTicks () << " (" << this << " ): Game not won, yet no move available?? Providing bogus hint of (0,0)." << std::endl;
197188 suggestedX = 0 ;
198189 suggestedY = 0 ;
199- SDL_mutexV (paintMutex);
200190 return ;
201191 }
202192
203- // Unsolvable
204- // How on earth we got here after the constructor ensured
205- // the board was solvable is beyond me.
206- std::cerr << " Game is not solvable, cannot give move hint." << std::endl;
193+ SDL_mutexP (paintMutex);
194+ int x=0 ;
195+ int y=0 ;
196+ do {
197+ x = rand ()%lights->getWidth ();
198+ y = rand ()%lights->getHeight ();
199+ } while (!(lights->getTile (x,y)->object ->shouldPress ()));
207200 SDL_mutexV (paintMutex);
208- throw 10 ;
201+
202+ suggestedX = x;
203+ suggestedY = y;
204+
205+ return ;
209206}
210207
211208
@@ -402,6 +399,7 @@ void LightsOutGame::pressButton(unsigned int x, unsigned int y) {
402399
403400 moves++;
404401
402+ lights->getTile (x,y)->object ->press ();
405403 toggleLight (x,y);
406404 toggleLight (x-1 ,y);
407405 toggleLight (x+1 ,y);
0 commit comments