From f4fb60d4bf23bfe7c3cc7b218cca21c68e13860c Mon Sep 17 00:00:00 2001 From: fela Date: Mon, 10 Mar 2008 21:23:52 +0000 Subject: [PATCH] SVN_SILENT: minor refactoring git-svn-id: svn://anonsvn.kde.org/home/kde/trunk/KDE/kdegames@784240 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- knetwalk/src/abstractgrid.cpp | 63 ++++++++++++++++++++++++++----------------- knetwalk/src/abstractgrid.h | 34 ++++++++++++++--------- knetwalk/src/main.cpp | 27 ++++++++++++------- knetwalk/src/mainwindow.cpp | 32 ++++------------------ knetwalk/src/mainwindow.h | 1 - 5 files changed, 82 insertions(+), 75 deletions(-) diff --git a/knetwalk/src/abstractgrid.cpp b/knetwalk/src/abstractgrid.cpp index 8b36e3120..11e8ced3a 100644 --- a/knetwalk/src/abstractgrid.cpp +++ b/knetwalk/src/abstractgrid.cpp @@ -26,12 +26,9 @@ AbstractCell::AbstractCell(int index) + : m_index(index) { - m_index = index; - m_cables = originalCables = None; - m_isServer = false; - m_isConnected = false; - m_isInOriginalPosition = true; + makeEmpty(); } char *AbstractCell::toString() { @@ -64,7 +61,7 @@ bool AbstractCell::isTerminal() const void AbstractCell::setCables(Directions newCables) { m_cables = originalCables = newCables; - m_isInOriginalPosition = true; + m_hasBeenMoved = false; } void AbstractCell::setServer(bool isServer) @@ -77,9 +74,17 @@ void AbstractCell::setConnected(bool isConnected) m_isConnected = isConnected; } +void AbstractCell::makeEmpty() +{ + m_cables = originalCables = None; + m_isServer = false; + m_isConnected = false; + m_hasBeenMoved = false; +} + void AbstractCell::emptyMove() { - m_isInOriginalPosition = false; + m_hasBeenMoved = true; } void AbstractCell::turnClockwise() @@ -90,7 +95,7 @@ void AbstractCell::turnClockwise() if (m_cables & Down) newdirs = Directions(newdirs | Left); if (m_cables & Left) newdirs = Directions(newdirs | Up); m_cables = newdirs; - m_isInOriginalPosition = false; + m_hasBeenMoved = true; } void AbstractCell::turnCounterclockwise() @@ -101,7 +106,7 @@ void AbstractCell::turnCounterclockwise() if (m_cables & Down) newdirs = Directions(newdirs | Right); if (m_cables & Left) newdirs = Directions(newdirs | Down); m_cables = newdirs; - m_isInOriginalPosition = false; + m_hasBeenMoved = true; } void AbstractCell::invert() @@ -112,15 +117,29 @@ void AbstractCell::invert() void AbstractCell::reset() { - m_isInOriginalPosition = true; m_cables = originalCables; + m_hasBeenMoved = false; } -AbstractGrid::AbstractGrid(uint width, uint height, Wrapping wrapping) + +AbstractGrid::~AbstractGrid() +{ + while (!m_cells.isEmpty()) delete m_cells.takeFirst(); +} + +void AbstractGrid::initializeGrid(uint width, uint height, Wrapping wrapping) { + if ((width * height) != (m_width * m_height)) { + while (!m_cells.isEmpty()) delete m_cells.takeFirst(); + + for (uint index = 0; index < width*height; ++index) { + m_cells.append(newCell(index)); + } + } + m_width = width; m_height = height; m_isWrapped = wrapping; @@ -142,12 +161,6 @@ AbstractGrid::AbstractGrid(uint width, uint height, Wrapping wrapping) } } -AbstractGrid::~AbstractGrid() -{ - while (!m_cells.isEmpty()) - delete m_cells.takeFirst(); -} - void AbstractGrid::print() { system("clear"); QString str1; @@ -174,13 +187,8 @@ void AbstractGrid::print() { void AbstractGrid::createGrid() { - while (!m_cells.isEmpty()){ - delete m_cells.takeFirst(); - } - - // and create new cells for (uint i = 0; i < m_width*m_height; ++i) { - m_cells.append(new AbstractCell(i)); + m_cells[i]->makeEmpty(); } // add a random server @@ -392,6 +400,7 @@ int AbstractGrid::solutionCount() bool AbstractGrid::movesDoneArePossible() { + foreach (AbstractCell *cell, m_cells) { if (!cell->hasBeenMoved()) continue; @@ -445,7 +454,9 @@ bool AbstractGrid::movesDoneArePossible() bool AbstractGrid::hasUnneededCables() { foreach (AbstractCell *cell, m_cells) { - if (cell->isTerminal() || cell->isServer()) continue; + if (cell->isTerminal() || cell->isServer() || cell->cables() == None) { + continue; + } Directions oldCables = cell->cables(); cell->setCables(None); @@ -464,6 +475,9 @@ bool AbstractGrid::hasUnneededCables() bool AbstractGrid::isSolution() { + foreach (AbstractCell *cell, m_cells) { + cell->setConnected(false); + } updateConnections(); // return false if there is a terminal that isn't connected foreach (AbstractCell *cell, m_cells) { @@ -496,7 +510,6 @@ void AbstractGrid::updateConnections() int dindex = dCell(cell_index); int lindex = lCell(cell_index); - // TODO: aren't needed only use indexes AbstractCell *cell = m_cells[cell_index]; AbstractCell *ucell = (uindex != NO_CELL) ? m_cells[uindex] : 0; AbstractCell *rcell = (rindex != NO_CELL) ? m_cells[rindex] : 0; diff --git a/knetwalk/src/abstractgrid.h b/knetwalk/src/abstractgrid.h index 954e7501e..9442525f0 100644 --- a/knetwalk/src/abstractgrid.h +++ b/knetwalk/src/abstractgrid.h @@ -30,30 +30,35 @@ public: int index() const {return m_index;} bool isServer() const {return m_isServer;} bool isConnected() const {return m_isConnected;} - bool hasBeenMoved() const {return !m_isInOriginalPosition;} + bool hasBeenMoved() const {return m_hasBeenMoved;} bool isTerminal() const; - // only used to change the cables (not to rotate the cell!) + // should not be used to rotate the cell void setCables(Directions newCables); void setServer(bool isServer=true); void setConnected(bool isConnected=true); - void emptyMove(); // sets isInOriginalPosition to false + // sets the cell as if newly created + void makeEmpty(); + + void emptyMove(); // sets hasBeenMoved to true void turnClockwise(); void turnCounterclockwise(); void invert(); // turns the cables by 180 degrees + // reset to the original position void reset(); + // used for debugging only char *toString(); -protected: // TODO: maybe could be private.. +private: int m_index; Directions originalCables; Directions m_cables; bool m_isServer; bool m_isConnected; - bool m_isInOriginalPosition; // TODO: change to !m_hasBeenMoved + bool m_hasBeenMoved; }; class Move @@ -83,19 +88,26 @@ class AbstractGrid public: // creates a grid made of AbstractCells, which will be a valid game // this is the main purpose of the class - AbstractGrid(uint width, uint height, Wrapping w=NotWrapped); - ~AbstractGrid(); + AbstractGrid() {} + virtual ~AbstractGrid(); + +public: //TODO: make protected + void initializeGrid(uint width, uint height, Wrapping w=NotWrapped); // ownership remains to the AbstractGrid QList cells() const {return m_cells;} - void print(); // outputs the grid - //int width() {return m_width;} //int height() {return m_height;} //bool isWrapped() {return m_isWrapped;} +protected: + virtual AbstractCell *newCell(int index) {return new AbstractCell(index);} + private: + // used for debugging only + void print(); // outputs the grid + // used when an index of a cell is required static const int NO_CELL = -1; @@ -122,13 +134,11 @@ private: // !!! a single direction is expected as parameter (not bitwise ORed!!) !!! Directions invertDirection(Directions givenDirection); - - bool isValid(); - // return the number of solutions given a few moves already done int solutionCount(); // returns true if you can connect all terminal without usign all cables + // (doesn't really work as I wanted, doesn't detect most cases) bool hasUnneededCables(); // return false if some of the moves done are clearly wrong diff --git a/knetwalk/src/main.cpp b/knetwalk/src/main.cpp index ae43c8788..194f2b9d3 100644 --- a/knetwalk/src/main.cpp +++ b/knetwalk/src/main.cpp @@ -30,7 +30,7 @@ static const char description[] = I18N_NOOP("KNetWalk, a game for system administrators."); -static const char version[] = "2.0.0"; +static const char version[] = "2.5.0"; int main(int argc, char ** argv) { @@ -54,21 +54,28 @@ int main(int argc, char ** argv) KCmdLineArgs::init(argc, argv, &about); KCmdLineOptions options; - options.add("Easy", ki18n( "Start with Easy difficulty level" )); - options.add("Medium", ki18n( "Start with Medium difficulty level" )); - options.add("Hard", ki18n( "Start with Hard difficulty level" )); - options.add("VeryHard", ki18n( "Start with Very Hard difficulty level" )); + options.add("Easy", ki18n("Start with Easy difficulty level")); + options.add("Medium", ki18n("Start with Medium difficulty level")); + options.add("Hard", ki18n("Start with Hard difficulty level")); + options.add("VeryHard", ki18n("Start with Very Hard difficulty level")); KCmdLineArgs::addCmdLineOptions(options); KApplication application; KGlobal::locale()->insertCatalog("libkdegames"); KCmdLineArgs *args = KCmdLineArgs::parsedArgs(); - if (args->isSet("Easy")) Settings::setSkill(Settings::EnumSkill::Easy); - if (args->isSet("Medium")) Settings::setSkill(Settings::EnumSkill::Medium); - if (args->isSet("Hard")) Settings::setSkill(Settings::EnumSkill::Hard); - if (args->isSet("VeryHard")) Settings::setSkill(Settings::EnumSkill::VeryHard); - + if (args->isSet("Easy")) { + Settings::setSkill(Settings::EnumSkill::Easy); + } + if (args->isSet("Medium")) { + Settings::setSkill(Settings::EnumSkill::Medium); + } + if (args->isSet("Hard")) { + Settings::setSkill(Settings::EnumSkill::Hard); + } + if (args->isSet("VeryHard")) { + Settings::setSkill(Settings::EnumSkill::VeryHard); + } args->clear(); diff --git a/knetwalk/src/mainwindow.cpp b/knetwalk/src/mainwindow.cpp index 1c875767d..0e19f70cc 100644 --- a/knetwalk/src/mainwindow.cpp +++ b/knetwalk/src/mainwindow.cpp @@ -174,7 +174,8 @@ void MainWindow::setupActions() KStandardGameAction::quit(this, SLOT(close()), actionCollection()); // Settings - KStandardAction::configureNotifications(this, SLOT(configureNotifications()), actionCollection()); + KStandardAction::configureNotifications(this, + SLOT(configureNotifications()), actionCollection()); } @@ -215,7 +216,9 @@ void MainWindow::startNewGame() const int size = boardSize(); setBoardSize(size); - AbstractGrid grid(size, size, NotWrapped); + // TODO: enable wrapped + AbstractGrid grid; + grid.initializeGrid(size, size, (Wrapping)wrapped); const int start = (MasterBoardSize - size) / 2; @@ -339,31 +342,6 @@ void MainWindow::updateConnections() if (newConnections) checkIfGameEnded(); } -void MainWindow::addRandomDir(CellList& list) -{ - Cell* cell = list.first(); - Cell* ucell = uCell(cell); - Cell* rcell = rCell(cell); - Cell* dcell = dCell(cell); - Cell* lcell = lCell(cell); - - typedef QMap CellMap; - CellMap freecells; - - if (ucell && ucell->dirs() == None) freecells[Up] = ucell; - if (rcell && rcell->dirs() == None) freecells[Right] = rcell; - if (dcell && dcell->dirs() == None) freecells[Down] = dcell; - if (lcell && lcell->dirs() == None) freecells[Left] = lcell; - if (freecells.isEmpty()) return; - - CellMap::ConstIterator it = freecells.constBegin(); - for (int i = rand() % freecells.count(); i > 0; --i) ++it; - - cell->setDirs(Directions(cell->dirs() | it.key())); - it.value()->setDirs(contrdirs[it.key()]); - list.append(it.value()); -} - Cell* MainWindow::uCell(Cell* cell) const { if (cell->index() >= MasterBoardSize) { diff --git a/knetwalk/src/mainwindow.h b/knetwalk/src/mainwindow.h index c005e9232..d95e24112 100644 --- a/knetwalk/src/mainwindow.h +++ b/knetwalk/src/mainwindow.h @@ -84,7 +84,6 @@ private: bool startBrowser(const QString& url); void blink(int index); void rotate(int index, bool clockWise); - void addRandomDir(CellList& list); void dialog(const QString& caption, const QString& text); void setBoardSize(int size); -- 2.11.4.GIT