SVN_SILENT made messages (.desktop file)
[kdegames.git] / knetwalk / src / abstractgrid.h
blob9595f6829362382830a0ddafa9062ca46a5b0dcf
1 /*
2 Copyright 2007-2008 Fela Winkelmolen <fela.kde@gmail.com>
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.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #ifndef ABSTRACT_GRID
19 #define ABSTRACT_GRID
21 #include <QList>
22 #include "globals.h"
24 class AbstractCell
26 public:
27 AbstractCell(int index);
29 virtual ~AbstractCell() {}
31 Directions cables() const {return m_cables;}
32 int index() const {return m_index;}
33 bool isServer() const {return m_isServer;}
34 bool isConnected() const {return m_isConnected;}
35 bool hasBeenMoved() const {return m_hasBeenMoved;}
36 bool isTerminal() const;
38 // should not be used to rotate the cell
39 void setCables(Directions newCables);
40 void setServer(bool isServer);
41 virtual void setConnected(bool isConnected);
43 // sets the cell as if newly created
44 virtual void makeEmpty();
46 void emptyMove(); // sets hasBeenMoved to true
47 void rotateClockwise();
48 void rotateCounterclockwise();
49 void invert(); // rotates the cables by 180 degrees
51 // reset to the original position
52 void reset();
54 // used for debugging only
55 char *toString();
57 private:
58 int m_index;
59 Directions originalCables;
60 Directions m_cables;
61 bool m_isServer;
62 bool m_isConnected;
63 bool m_hasBeenMoved;
66 class Move
68 public:
69 enum MoveDirection {None, Left, Right, Inverted};
70 Move() {}
72 Move(int index, MoveDirection move) {
73 m_index = index;
74 m_move = move;
77 int index() const {return m_index;}
78 MoveDirection move() const {return m_move;}
80 private:
81 int m_index;
82 MoveDirection m_move;
85 typedef QList<Move> MoveList;
88 class AbstractGrid
90 public:
91 // creates a grid made of AbstractCells, which will be a valid game
92 // this is the main purpose of the class
93 AbstractGrid() {}
94 virtual ~AbstractGrid();
96 protected:
98 void initializeGrid(uint width, uint height, Wrapping w=NotWrapped);
100 // ownership remains to the AbstractGrid
101 QList<AbstractCell *> cells() const {return m_cells;}
102 int cellCount() {return m_cells.size();} // TODO: use in the cpp file
104 int width() {return m_width;}
105 int height() {return m_height;}
107 // the minimum number of moves required to solve the game
108 int minimumMoves;
110 virtual AbstractCell *newCell(int index) {return new AbstractCell(index);}
112 // updates the connections of the cells
113 // returns the indexes of the changed cells
114 QList<int> updateConnections();
116 // returns true if all terminals are connected to the server
117 bool isPossibleSolution();
119 bool allTerminalsConnected();
121 private:
122 // used for debugging only
123 void print(); // outputs the grid
125 // used when an index of a cell is required
126 static const int NO_CELL = -1;
128 QList<AbstractCell *> m_cells;
129 uint m_width;
130 uint m_height;
131 bool m_isWrapped;
133 int server_index;
135 //======== auxiliary functions ========//
136 void createGrid(); // only used for modularization
138 // adds a random direction (cable) and moves on (if possible)
139 // used when creating the grid
140 void addRandomCable(QList<uint>& list);
141 // find the index to the left/right/up/down w.r.t the index given
142 int uCell(uint cell) const;
143 int dCell(uint cell) const;
144 int lCell(uint cell) const;
145 int rCell(uint cell) const;
147 // return the opposite direction of the one given
148 // !!! a single direction is expected as parameter (not bitwise ORed!!) !!!
149 Directions invertDirection(Directions givenDirection);
151 // return the number of solutions given a few moves already done
152 int solutionCount();
154 // returns true if you can connect all terminal without usign all cables
155 // (doesn't really work as I wanted, doesn't detect most cases)
156 bool hasUnneededCables();
158 // return false if some of the moves done are clearly wrong
159 bool movesDoneArePossible();
162 #endif // ABSTRACT_GRID