1 /* 2 * Copyright (C) 2004 Ivo Danihelka (ivo@danihelka.net) 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation; either version 2 of the License, or 7 * (at your option) any later version. 8 */ 9 #include "LevelCountDown.h" 10 11 #include "LevelStatus.h" 12 #include "RoomAccess.h" 13 #include "Room.h" 14 #include "StepCounter.h" 15 #include "LogicException.h" 16 #include "CountAdvisor.h" 17 18 //----------------------------------------------------------------- 19 /** 20 * Prepare countdown. 21 */ LevelCountDown(const RoomAccess * access)22LevelCountDown::LevelCountDown(const RoomAccess *access) 23 { 24 m_countdown = -1; 25 m_access = access; 26 m_levelStatus = NULL; 27 } 28 //----------------------------------------------------------------- 29 /** 30 * Resets counter. 31 * @throws LogicException when levelStatus is not filled 32 */ 33 void reset()34LevelCountDown::reset() 35 { 36 if (NULL == m_levelStatus) { 37 throw LogicException(ExInfo("level status is NULL")); 38 } 39 m_levelStatus->setRunning(true); 40 m_countdown = -1; 41 } 42 //----------------------------------------------------------------- 43 /** 44 * Countdown to zero. 45 * @param advisor advisor which know usable coundown values 46 * @return true when counter is at zero 47 */ 48 bool countDown(const CountAdvisor * advisor)49LevelCountDown::countDown(const CountAdvisor *advisor) 50 { 51 bool result = false; 52 if (m_countdown < 0) { 53 setCountDown(advisor); 54 } 55 else if (m_countdown > 0) { 56 m_countdown--; 57 } 58 else { 59 result = true; 60 } 61 return result; 62 } 63 //----------------------------------------------------------------- 64 void setCountDown(const CountAdvisor * advisor)65LevelCountDown::setCountDown(const CountAdvisor *advisor) 66 { 67 if (m_access->const_room()->isSolved()) { 68 m_countdown = advisor->getCountForSolved(); 69 } 70 else if (m_access->const_room()->cannotMove()) { 71 m_countdown = advisor->getCountForWrong(); 72 } 73 else { 74 m_countdown = -1; 75 } 76 } 77 //----------------------------------------------------------------- 78 bool isFinishedEnough() const79LevelCountDown::isFinishedEnough() const 80 { 81 return m_countdown == 0 && m_access->const_room()->isSolved(); 82 } 83 //----------------------------------------------------------------- 84 bool isWrongEnough() const85LevelCountDown::isWrongEnough() const 86 { 87 return m_countdown == 0 && m_access->const_room()->cannotMove() && 88 !m_access->const_room()->isSolved(); 89 } 90 //----------------------------------------------------------------- 91 /** 92 * Write best solution to the file. 93 * Save moves and models state. 94 */ 95 void saveSolution()96LevelCountDown::saveSolution() 97 { 98 m_levelStatus->setComplete(); 99 std::string current_moves = 100 m_access->const_room()->stepCounter()->getMoves(); 101 m_levelStatus->writeSolvedMoves(current_moves); 102 } 103 //----------------------------------------------------------------- 104 /** 105 * Creates next state or returns NULL. 106 * @return returns NULL when only quitState() is needed 107 */ 108 GameState * createNextState()109LevelCountDown::createNextState() 110 { 111 return m_levelStatus->createPoster(); 112 } 113 114