File indexing completed on 2024-04-21 04:02:01

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 KBBBALLSONBOARD_H
0013 #define KBBBALLSONBOARD_H
0014 
0015 
0016 #include <QList>
0017 #include <QObject>
0018 
0019 
0020 class KBBGameDoc;
0021 
0022 
0023 
0024 #define DIM_X 0
0025 #define DIM_Y 1
0026 #define DIM_MAX 2
0027 
0028 
0029 
0030 /**
0031  * @brief Set of balls (or various objects) with positions on the board
0032  *
0033  * 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).
0034  *
0035  * It computes also the trajectory of the laser ray with the given balls.
0036  *
0037  * There are 3 different kinds of coordinates for object positions.
0038  * - 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.
0039  * - 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.
0040  * - 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.
0041  */
0042 class KBBBallsOnBoard : public QObject
0043 {
0044     Q_OBJECT
0045 
0046     public:
0047         /**
0048          * @brief Constructor
0049          */
0050         KBBBallsOnBoard(KBBGameDoc* parent, const int columns, const int rows);
0051 
0052 
0053         /**
0054          * @brief Convert (absolute) position to border position
0055          *
0056          * @param position The (absolute) position to convert.
0057          * @return The result of the conversion: border position.
0058          * @see borderPositionToAbsolutePosition(int borderPosition, int position[DIM_MAX])
0059          * @see absolutePositionToBorderPosition(int position[DIM_MAX])
0060          */
0061         int absolutePositionToBorderPosition(int position[DIM_MAX]);
0062         
0063         /**
0064          * @brief Convert (absolute) position to box position
0065          *
0066          * @param position The (absolute) position to convert.
0067          * @return The result of the conversion: box position.
0068          * @see absolutePositionToBorderPosition(int position[DIM_MAX])
0069          */
0070         int absolutePositionToBoxPosition(int position[DIM_MAX]);
0071         
0072         /**
0073          * @brief Add a ball on this board
0074          *
0075          * @param boxPosition The box position of the ball to add
0076          * @see remove(int boxPosition)
0077          */
0078         void add(int boxPosition);
0079         
0080         /**
0081          * @brief Convert border position to (abosulte) position
0082          *
0083          * @param borderPosition The border position to convert.
0084          * @param position The result of the conversion: (absolute) position.
0085          * @see borderPositionToAbsolutePosition(int position[DIM_MAX])
0086          */
0087         void borderPositionToAbsolutePosition(int borderPosition, int position[DIM_MAX]);
0088         
0089         int columns();
0090         
0091         /**
0092          * @brief Check if there is a ball at the given position in the black box
0093          *
0094          * @param boxPosition Box position to check
0095          */
0096         bool contains(int boxPosition);
0097         
0098         /**
0099          * @brief Number of balls on this board
0100          */
0101         int count();
0102         
0103         /**
0104          * @brief Define a new board and remove all balls
0105          *
0106          * @param columns Number of columns
0107          * @param rows Number of rows
0108          */
0109         void newBoard(const int columns, const int rows);
0110         
0111         /**
0112          * @brief Compares 2 boards and return the number of differences
0113          *
0114          * @param otherBoard Other set of balls in a board
0115          * @return Number of balls in the set that are not in the other given set
0116          */
0117         int numberOfBallsNotIn(KBBBallsOnBoard* otherBoard);
0118         
0119         /**
0120          * @brief Compute the opposite border position of the given position
0121          *
0122          * @param borderPosition The border position
0123          */
0124         int oppositeBorderPosition(int borderPosition);
0125         
0126         int oppositeBorderPositionWithPoints(const int borderPosition, QList<int> &points);
0127         
0128         /**
0129          * @brief Compute the trajectory of a ray with the balls of the set
0130          *
0131          * @param borderPosition The border position
0132          */
0133         void ray(const int borderPosition, QList<int> &points);
0134 
0135         /**
0136          * @brief Remove a ball on this board
0137          *
0138          * @param boxPosition The box position of the ball to be removed
0139          * @see add(int boxPosition);
0140          */
0141         void remove(int boxPosition);
0142         
0143         int rows();
0144 
0145 
0146     Q_SIGNALS:
0147         void changes();
0148 
0149 
0150     private:
0151         /**
0152          * @brief Find the position where the laser ray leaves the black box
0153          *
0154          * @param position[DIM_MAX] Current incoming (absolute) position. It can be on a border or in the black box.
0155          * @param incomingDirection[DIM_MAX] Direction to move in the black box as a vector (difference of (absolute) positions)
0156          */
0157         void getOutgoingPosition( int position[DIM_MAX], int incomingDirection[DIM_MAX], QList<int> &points );
0158         
0159         /**
0160          * @brief Check if the given (absolute) position is in the box
0161          *
0162          * @param position (Absolute) position to check
0163          */
0164         bool positionInTheBox( int position[DIM_MAX] );
0165         
0166         
0167         QList<int> m_balls;
0168         int m_columns;
0169         int m_rows;
0170 };
0171 
0172 #endif // KBBBALLSONBOARD_H