File indexing completed on 2024-02-25 04:08:53

0001 /*
0002     SPDX-FileCopyrightText: 2013 Jaime Torres <jtamate@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef Ships_H
0008 #define Ships_H
0009 
0010 #include <QHash>
0011 #include <QString>
0012 
0013 #include "ship.h"
0014 
0015 class Ships
0016 {
0017 private:
0018     unsigned int m_size;       // of the ships
0019     unsigned int m_number;     // number of ships of this size
0020     QString m_shipsName;       // the english singular name of the ships of this size
0021     QString m_shipsPluralName; // the english plural of the ships of this size
0022 public:
0023     Ships(): m_size(0), m_number(0), m_shipsName(), m_shipsPluralName() { }
0024     Ships(unsigned int size, unsigned int number, QString& shipsName, QString& shipsPluralName);
0025     inline unsigned int size() const { return m_size; }
0026     inline unsigned int number() const { return m_number; }
0027     inline QString shipsName() const { return m_shipsName; }
0028     inline QString pluralShipsName() const { return m_shipsPluralName; }
0029 };
0030 
0031 // This configuration is interchanged in the network games.
0032 // In the versions previous to KDE SC 4.13, only adjacent and multiple where interchanged.
0033 class BattleShipsConfiguration
0034 {
0035 private:
0036     unsigned int m_longestShip;
0037     bool m_allowAdjacentShips;
0038     unsigned int m_boardWidth;
0039     unsigned int m_boardHeight;
0040     bool m_fromXML;
0041     QHash<unsigned int,Ships> m_ships;
0042 public:
0043 
0044     explicit BattleShipsConfiguration(const bool fromXML=false);
0045     explicit BattleShipsConfiguration(unsigned int longestShipSize, const bool allowAdjacentShips, const unsigned int boardWidth, const unsigned int boardHeight, const bool fromXML=false);
0046     BattleShipsConfiguration(const BattleShipsConfiguration& copy) = default;
0047     BattleShipsConfiguration& operator=(const BattleShipsConfiguration&) = default;
0048     // does not add any ship longer than longestShip
0049     // overwrites any previous configuration for ships of the requested size
0050     BattleShipsConfiguration& addShips(unsigned int size, unsigned int number, QString shipsName, QString shipsPluralName);
0051     BattleShipsConfiguration& addShips(Ships &ships);
0052     unsigned int numberOfShipsOfSize(unsigned int size) const; // 0 if size is invalid
0053     QString nameOfShipsOfSize(unsigned int size) const; // QString() if size is invalid
0054     QString pluralNameOfShipsOfSize(unsigned int size) const; // QString() if size is invalid
0055     bool multipleShips() const; // return true if any ship size has more than one ship
0056 
0057     void setLongestShipSize(unsigned int longestShipSize);
0058     void setAllowAdjacentShips(const bool allow) { m_allowAdjacentShips = allow; }
0059     void setBoardWidth(const unsigned int boardWidth) { m_boardWidth = boardWidth; }
0060     void setBoardHeight(const unsigned int boardHeight) { m_boardWidth = boardHeight; }
0061     void setFromXML(bool fromXML) { m_fromXML=fromXML; }
0062     inline unsigned int boardWidth() const { return m_boardWidth; }
0063     inline unsigned int boardHeight() const { return m_boardHeight; }
0064     inline bool isAllowedAdjacentShips() const { return m_allowAdjacentShips; }
0065     inline bool isFromXML() const { return m_fromXML; }
0066     // ships are of 0 < size <= longestShip()
0067     inline unsigned int longestShip() const { return m_longestShip; }
0068     unsigned int totalNumberOfShipsToPlay() const;
0069     bool isAValidConfiguration() const;
0070 
0071     static BattleShipsConfiguration defaultSingleShipsConfiguration(const bool allowAdjacent, const bool fromXML = false);
0072     static BattleShipsConfiguration defaultMultipleShipsConfiguration(const bool allowAdjacent, const bool fromXML = false);
0073 };
0074 
0075 
0076 
0077 #endif // Ships_H