00001 /* 00002 * Sudoku: A plug-in for the Video Disk Recorder 00003 * 00004 * Copyright (C) 2008-2010, Thomas Günther <tom@toms-cafe.de> 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License along 00017 * with this program; if not, write to the Free Software Foundation, Inc., 00018 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 00019 */ 00020 00021 #include "history.h" 00022 #include "puzzle.h" 00023 00024 using namespace Sudoku; 00025 00026 00027 //--- class Sudoku::History ---------------------------------------------------- 00028 00029 /** Constructor */ 00030 History::History() 00031 { 00032 executed = 0; 00033 } 00034 00035 /** Destructor */ 00036 History::~History() 00037 { 00038 reset(); 00039 } 00040 00041 /** Reset the history. */ 00042 void History::reset() 00043 { 00044 for (unsigned int pos = history.size(); pos > 0; --pos) 00045 { 00046 // Remove object created outside of History 00047 delete history.back(); 00048 history.pop_back(); 00049 } 00050 executed = 0; 00051 } 00052 00053 /** Current move in the history */ 00054 Move* History::current() 00055 { 00056 if (movesExecuted()) 00057 return history[executed - 1]; 00058 return 0; 00059 } 00060 00061 /** Add a new move. */ 00062 void History::add(Move* move) 00063 { 00064 if (move) 00065 { 00066 for (unsigned int pos = history.size(); pos > executed; --pos) 00067 { 00068 // Remove object created outside of History 00069 delete history.back(); 00070 history.pop_back(); 00071 } 00072 history.push_back(move); 00073 ++executed; 00074 } 00075 } 00076 00077 /** Set previous move as current. */ 00078 void History::backward() 00079 { 00080 if (movesExecuted()) 00081 --executed; 00082 } 00083 00084 /** Set next move as current. */ 00085 void History::forward() 00086 { 00087 if (movesToExecute()) 00088 ++executed; 00089 } 00090 00091 /** Are there executed moves in the history? */ 00092 bool History::movesExecuted() const 00093 { 00094 return executed > 0; 00095 } 00096 00097 /** Are there moves to execute in the history? */ 00098 bool History::movesToExecute() const 00099 { 00100 return executed < history.size(); 00101 } 00102 00103 00104 //--- class Sudoku::SetMove ---------------------------------------------------- 00105 00106 /** Constructor */ 00107 SetMove::SetMove(PuzzleGame& puzzle, unsigned int number) : 00108 puzzle(puzzle) 00109 { 00110 pos = puzzle.get_pos(); 00111 old_number = puzzle.get(pos); 00112 new_number = number; 00113 } 00114 00115 /** Do the move. */ 00116 void SetMove::execute() 00117 { 00118 puzzle.set(pos, new_number); 00119 puzzle.set_pos(pos); 00120 } 00121 00122 /** Undo the move. */ 00123 void SetMove::takeBack() 00124 { 00125 puzzle.set(pos, old_number); 00126 puzzle.set_pos(pos); 00127 }