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 KBBGAMEDOC_H
33#define KBBGAMEDOC_H
34
35
36#include <QObject>
37
38
39#include <krandomsequence.h>
40
41
42class KBBBallsOnBoard;
43#include "kbbmainwindow.h"
44class KBBTutorial;
45
46
47
48/**
49 * @brief Game document (Logical board)
50 *
51 * The game document (=logical board) manages a game, that is:
52 * - the score
53 * - the balls the user placed on the board
54 * - the real (hidden) balls of the black box
55 */
56class KBBGameDoc : public QObject
57{
58 Q_OBJECT
59
60 public:
61 /**
62 * When a laser ray enter the black box, it exits on a defined border position, except if the laser ray hits a ball. In this case, it doesn't exit the black box. In this case, we defined the exit position as the HIT_POSITION.
63 * This position is "virtual" and must be different to all other possible "real" border positions. (That's why we defined it as a negative (<0) position).
64 */
65 static const int HIT_POSITION = -1;
66
67 static const int SCORE_LOST = 999;
68 static const int SCORE_ONE = 3;
69 static const int SCORE_TWO = 9;
70
71
72 /**
73 * @brief Constructor
74 */
75 KBBGameDoc(KBBMainWindow *parent, KBBTutorial* tutorial);
76
77
78 /**
79 * @brief Get the number of columns
80 */
81 int columns() const;
82
83 /**
84 * @brief Stop the game, show solution and compute final score
85 */
86 void gameOver();
87
88 /**
89 * @brief Check if the player started to play
90 *
91 * Check if the game is running and if the player shot at least one laser ray.
92 * Before that, the player doesn't need to confirm the end of the game, if he tries to start a new game.
93 */
94 bool gameReallyStarted();
95
96 bool mayShootRay(const int incomingPosition) const;
97
98 /**
99 * @brief Create new board game and initialize game
100 *
101 * @param balls Number of balls to place on the board
102 * @param columns Number of columns
103 * @param rows Number of rows
104 */
105 void newGame(int balls, int columns, int rows);
106
107 /**
108 * @brief Number of balls the user placed on the board
109 */
110 int numberOfBallsPlaced();
111
112 /**
113 * @brief Number of balls the user has to place on the board
114 */
115 int numberOfBallsToPlace();
116
117 /**
118 * @brief Get the number of rows
119 */
120 int rows() const;
121
122 /**
123 * @brief Get current score
124 */
125 int score();
126
127 /**
128 * @brief Shoot a ray
129 *
130 * This is the main player action. A laser ray is shot from a border position, interacts with the balls in the black box and get out (or hit a ball).
131 * This costs 1 or 2 score points, depending where the laser ray exits.
132 *
133 * @param borderPosition Incoming border position, where the laser ray starts
134 * @return Outgoing border position, where the laser leaves the black box. If the laser hits a ball, the result is HIT_POSITION (that is not a valid border position).
135 */
136 int shootRay(int borderPosition);
137
138 /**
139 * @brief Initialize the tutorial
140 */
141 void startTutorial();
142
143
144 KBBBallsOnBoard* m_balls;
145 KBBBallsOnBoard* m_ballsPlaced;
146
147
148 public slots:
149 void timeChanged();
150
151
152 signals:
153 void isRunning(bool);
154 void updateStats();
155
156
157 private:
158 void setRunning(bool r);
159
160 /**
161 * @brief Sets the score value
162 *
163 * @param n New score
164 */
165 void setScore( int n );
166
167 void clean(const int columns, const int rows);
168
169 int m_columns;
170 bool m_gameReallyStarted;
171 int m_rows;
172 KRandomSequence m_random;
173 int m_score;
174 KBBTutorial* m_tutorial;
175};
176
177#endif //KBBGAMEDOC_H
178