File indexing completed on 2022-11-23 10:43:24

0001 /*
0002     KBlackBox - A simple game inspired by an emacs module
0003 
0004     SPDX-FileCopyrightText: 1999-2000 Robert Cimrman <cimrman3@students.zcu.cz>
0005     SPDX-FileCopyrightText: 2007 Nicolas Roffet <nicolas-kde@roffet.com>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 
0011 
0012 #ifndef KBBGAMEDOC_H
0013 #define KBBGAMEDOC_H
0014 
0015 #include <QObject>
0016 #include <QRandomGenerator>
0017 
0018 class KBBBallsOnBoard;
0019 #include "kbbmainwindow.h"
0020 class KBBTutorial;
0021 
0022 
0023 
0024 /**
0025  * @brief Game document (Logical board)
0026  *
0027  * The game document (=logical board) manages a game, that is:
0028  *    - the score
0029  *    - the balls the user placed on the board
0030  *    - the real (hidden) balls of the black box
0031  */
0032 class KBBGameDoc : public QObject
0033 {
0034     Q_OBJECT
0035 
0036     public:
0037         /**
0038          * 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.
0039          * 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).
0040          */
0041         static const int HIT_POSITION = -1;
0042 
0043         static const int SCORE_LOST = 999;
0044         static const int SCORE_ONE = 3;
0045         static const int SCORE_TWO = 9;
0046 
0047 
0048         /**
0049          * @brief Constructor
0050          */
0051         KBBGameDoc(KBBMainWindow *parent, KBBTutorial* tutorial);
0052 
0053 
0054         /**
0055          * @brief Get the number of columns
0056          */
0057         int columns() const;
0058 
0059         /**
0060          * @brief Stop the game, show solution and compute final score
0061          */
0062         void gameOver();
0063         
0064         /**
0065          * @brief Check if the player started to play
0066          * 
0067          * Check if the game is running and if the player shot at least one laser ray.
0068          * Before that, the player doesn't need to confirm the end of the game, if he tries to start a new game.
0069          */
0070         bool gameReallyStarted();
0071         
0072         bool mayShootRay(const int incomingPosition) const;
0073 
0074         /**
0075          * @brief Create new board game and initialize game
0076          * 
0077          * @param balls Number of balls to place on the board
0078          * @param columns Number of columns
0079          * @param rows Number of rows
0080          */
0081         void newGame(int balls, int columns, int rows);
0082         
0083         /**
0084          * @brief Number of balls the user placed on the board
0085          */
0086         int numberOfBallsPlaced();
0087 
0088         /**
0089          * @brief Number of balls the user has to place on the board
0090          */
0091         int numberOfBallsToPlace();
0092 
0093         /**
0094          * @brief Get the number of rows
0095          */
0096         int rows() const;
0097 
0098         /**
0099          * @brief Get current score
0100          */
0101         int score();
0102 
0103         /**
0104          * @brief Shoot a ray
0105          * 
0106          * 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).
0107          * This costs 1 or 2 score points, depending where the laser ray exits.
0108          *
0109          * @param borderPosition Incoming border position, where the laser ray starts
0110          * @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). 
0111          */
0112         int shootRay(int borderPosition);
0113 
0114         /**
0115          * @brief Initialize the tutorial
0116          */
0117         void startTutorial();
0118 
0119 
0120         KBBBallsOnBoard* m_balls;
0121         KBBBallsOnBoard* m_ballsPlaced;
0122 
0123 
0124     public Q_SLOTS:
0125         void timeChanged();
0126 
0127 
0128     Q_SIGNALS:
0129         void isRunning(bool);
0130         void updateStats();
0131 
0132 
0133     private:
0134         void setRunning(bool r);
0135 
0136         /**
0137          * @brief Sets the score value
0138          *
0139          * @param n New score
0140          */
0141         void setScore( int n );
0142 
0143         void clean(const int columns, const int rows);
0144 
0145         int m_columns;
0146         bool m_gameReallyStarted;
0147         int m_rows;
0148         QRandomGenerator m_random;
0149         int m_score;
0150         KBBTutorial* m_tutorial;
0151 };
0152 
0153 #endif //KBBGAMEDOC_H