1 | // |
2 | // KBlackBox |
3 | // |
4 | // A simple game inspired by an emacs module |
5 | // |
6 | /*************************************************************************** |
7 | * Copyright (c) 1999-2000, Robert Cimrman * |
8 | * cimrman3@students.zcu.cz * |
9 | * * |
10 | * Copyright (c) 2007, Nicolas Roffet * |
11 | * nicolas-kde@roffet.com * |
12 | * * |
13 | * * |
14 | * This program is free software; you can redistribute it and/or modify * |
15 | * it under the terms of the GNU General Public License as published by * |
16 | * the Free Software Foundation; either version 2 of the License, or * |
17 | * (at your option) any later version. * |
18 | * * |
19 | * This program is distributed in the hope that it will be useful, * |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
22 | * GNU General Public License for more details. * |
23 | * * |
24 | * You should have received a copy of the GNU General Public License * |
25 | * along with this program; if not, write to the * |
26 | * Free Software Foundation, Inc., * |
27 | * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA * |
28 | ***************************************************************************/ |
29 | |
30 | |
31 | |
32 | #ifndef KBBBALLSONBOARD_H |
33 | #define KBBBALLSONBOARD_H |
34 | |
35 | |
36 | #include <QList> |
37 | #include <QObject> |
38 | |
39 | |
40 | class KBBGameDoc; |
41 | |
42 | |
43 | |
44 | #define DIM_X 0 |
45 | #define DIM_Y 1 |
46 | #define DIM_MAX 2 |
47 | |
48 | |
49 | |
50 | /** |
51 | * @brief Set of balls (or various objects) with positions on the board |
52 | * |
53 | * The set of balls manages the position and the number of the balls. The balls can be placed of any kind (placed by the player or the hiden balls to find for instance). |
54 | * |
55 | * It computes also the trajectory of the laser ray with the given balls. |
56 | * |
57 | * There are 3 different kinds of coordinates for object positions. |
58 | * - The 1st one is the (absolute) position in 2 dimensions between (0,0) and (2 + m_columns + 2, 2 + m_rows + 2). It is used to manage the positions of the graphic elements but also to calculate the laser ray trajectory. |
59 | * - The 2nd one is the border position in 1 dimension between 0 and (2 * m_rows + 2 * m_columns -1). Only borders can be managed with this coordinate. |
60 | * - The 3rd one is the box position in 1 dimension between 0 and (m_columns*m_rows - 1). It is used to manage the postion of the balls in the black box. |
61 | */ |
62 | class KBBBallsOnBoard : public QObject |
63 | { |
64 | Q_OBJECT |
65 | |
66 | public: |
67 | /** |
68 | * @brief Constructor |
69 | */ |
70 | KBBBallsOnBoard(KBBGameDoc* parent, const int columns, const int rows); |
71 | |
72 | |
73 | /** |
74 | * @brief Convert (absolute) position to border position |
75 | * |
76 | * @param position The (absolute) position to convert. |
77 | * @return The result of the conversion: border position. |
78 | * @see borderPositionToAbsolutePosition(int borderPosition, int position[DIM_MAX]) |
79 | * @see absolutePositionToBorderPosition(int position[DIM_MAX]) |
80 | */ |
81 | int absolutePositionToBorderPosition(int position[DIM_MAX]); |
82 | |
83 | /** |
84 | * @brief Convert (absolute) position to box position |
85 | * |
86 | * @param position The (absolute) position to convert. |
87 | * @return The result of the conversion: box position. |
88 | * @see absolutePositionToBorderPosition(int position[DIM_MAX]) |
89 | */ |
90 | int absolutePositionToBoxPosition(int position[DIM_MAX]); |
91 | |
92 | /** |
93 | * @brief Add a ball on this board |
94 | * |
95 | * @param boxPosition The box position of the ball to add |
96 | * @see remove(int boxPosition) |
97 | */ |
98 | void add(int boxPosition); |
99 | |
100 | /** |
101 | * @brief Convert border position to (abosulte) position |
102 | * |
103 | * @param borderPosition The border position to convert. |
104 | * @param position The result of the conversion: (absolute) position. |
105 | * @see borderPositionToAbsolutePosition(int position[DIM_MAX]) |
106 | */ |
107 | void borderPositionToAbsolutePosition(int borderPosition, int position[DIM_MAX]); |
108 | |
109 | int columns(); |
110 | |
111 | /** |
112 | * @brief Check if there is a ball at the given position in the black box |
113 | * |
114 | * @param boxPosition Box position to check |
115 | */ |
116 | bool contains(int boxPosition); |
117 | |
118 | /** |
119 | * @brief Number of balls on this board |
120 | */ |
121 | int count(); |
122 | |
123 | /** |
124 | * @brief Define a new board and remove all balls |
125 | * |
126 | * @param columns Number of columns |
127 | * @param rows Number of rows |
128 | */ |
129 | void newBoard(const int columns, const int rows); |
130 | |
131 | /** |
132 | * @brief Compares 2 boards and return the number of differences |
133 | * |
134 | * @param otherBoard Other set of balls in a board |
135 | * @return Number of balls in the set that are not in the other given set |
136 | */ |
137 | int numberOfBallsNotIn(KBBBallsOnBoard* otherBoard); |
138 | |
139 | /** |
140 | * @brief Compute the opposite border position of the given position |
141 | * |
142 | * @param borderPosition The border position |
143 | */ |
144 | int oppositeBorderPosition(int borderPosition); |
145 | |
146 | int oppositeBorderPositionWithPoints(const int borderPosition, QList<int> &points); |
147 | |
148 | /** |
149 | * @brief Compute the trajectory of a ray with the balls of the set |
150 | * |
151 | * @param borderPosition The border position |
152 | */ |
153 | void ray(const int borderPosition, QList<int> &points); |
154 | |
155 | /** |
156 | * @brief Remove a ball on this board |
157 | * |
158 | * @param boxPosition The box position of the ball to be removed |
159 | * @see add(int boxPosition); |
160 | */ |
161 | void remove(int boxPosition); |
162 | |
163 | int rows(); |
164 | |
165 | |
166 | signals: |
167 | void changes(); |
168 | |
169 | |
170 | private: |
171 | /** |
172 | * @brief Find the position where the laser ray leaves the black box |
173 | * |
174 | * @param position[DIM_MAX] Current incoming (absolute) position. It can be on a border or in the black box. |
175 | * @param incomingDirection[DIM_MAX] Direction to move in the black box as a vector (difference of (absolute) positions) |
176 | */ |
177 | void getOutgoingPosition( int position[DIM_MAX], int incomingDirection[DIM_MAX], QList<int> &points ); |
178 | |
179 | /** |
180 | * @brief Check if the given (absolute) position is in the box |
181 | * |
182 | * @param position (Absolute) position to check |
183 | */ |
184 | bool positionInTheBox( int position[DIM_MAX] ); |
185 | |
186 | |
187 | QList<int> m_balls; |
188 | int m_columns; |
189 | int m_rows; |
190 | }; |
191 | |
192 | #endif // KBBBALLSONBOARD_H |
193 | |