00001 /* 00002 * Copyright (C) 2004 Ivo Danihelka (ivo@danihelka.net) 00003 * 00004 * This program is free software; you can redistribute it and/or modify 00005 * it under the terms of the GNU General Public License as published by 00006 * the Free Software Foundation; either version 2 of the License, or 00007 * (at your option) any later version. 00008 */ 00009 #include "LevelCountDown.h" 00010 00011 #include "LevelStatus.h" 00012 #include "RoomAccess.h" 00013 #include "Room.h" 00014 #include "StepCounter.h" 00015 #include "LogicException.h" 00016 #include "CountAdvisor.h" 00017 00018 //----------------------------------------------------------------- 00019 /** 00020 * Prepare countdown. 00021 */ 00022 LevelCountDown::LevelCountDown(const RoomAccess *access) 00023 { 00024 m_countdown = -1; 00025 m_access = access; 00026 m_levelStatus = NULL; 00027 } 00028 //----------------------------------------------------------------- 00029 /** 00030 * Resets counter. 00031 * @throws LogicException when levelStatus is not filled 00032 */ 00033 void 00034 LevelCountDown::reset() 00035 { 00036 if (NULL == m_levelStatus) { 00037 throw LogicException(ExInfo("level status is NULL")); 00038 } 00039 m_levelStatus->setRunning(true); 00040 m_countdown = -1; 00041 } 00042 //----------------------------------------------------------------- 00043 /** 00044 * Countdown to zero. 00045 * @param advisor advisor which known usable coundown values 00046 * @return true when counter is at zero 00047 */ 00048 bool 00049 LevelCountDown::countDown(const CountAdvisor *advisor) 00050 { 00051 bool result = false; 00052 if (m_countdown < 0) { 00053 setCountDown(advisor); 00054 } 00055 else if (m_countdown > 0) { 00056 m_countdown--; 00057 } 00058 else { 00059 result = true; 00060 } 00061 return result; 00062 } 00063 //----------------------------------------------------------------- 00064 void 00065 LevelCountDown::setCountDown(const CountAdvisor *advisor) 00066 { 00067 if (m_access->const_room()->isSolved()) { 00068 m_countdown = advisor->getCountForSolved(); 00069 } 00070 else if (m_access->const_room()->cannotMove()) { 00071 m_countdown = advisor->getCountForWrong(); 00072 } 00073 else { 00074 m_countdown = -1; 00075 } 00076 } 00077 //----------------------------------------------------------------- 00078 bool 00079 LevelCountDown::isFinishedEnough() const 00080 { 00081 return m_countdown == 0 && m_access->const_room()->isSolved(); 00082 } 00083 //----------------------------------------------------------------- 00084 bool 00085 LevelCountDown::isWrongEnough() const 00086 { 00087 return m_countdown == 0 && m_access->const_room()->cannotMove() && 00088 !m_access->const_room()->isSolved(); 00089 } 00090 //----------------------------------------------------------------- 00091 /** 00092 * Write best solution to the file. 00093 * Save moves and models state. 00094 */ 00095 void 00096 LevelCountDown::saveSolution() 00097 { 00098 m_levelStatus->setComplete(); 00099 std::string current_moves = 00100 m_access->const_room()->stepCounter()->getMoves(); 00101 m_levelStatus->writeSolvedMoves(current_moves); 00102 } 00103 //----------------------------------------------------------------- 00104 /** 00105 * Creates next state or returns NULL. 00106 * @return returns NULL when only quitState() is needed 00107 */ 00108 GameState * 00109 LevelCountDown::createNextState() 00110 { 00111 return m_levelStatus->createPoster(); 00112 } 00113