1 /*
2     SPDX-FileCopyrightText: 2013 Jaime Torres <jtamate@gmail.com>
3 
4     SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #ifndef Ships_H
8 #define Ships_H
9 
10 #include <QHash>
11 #include <QString>
12 
13 #include "ship.h"
14 
15 class Ships
16 {
17 private:
18     unsigned int m_size;       // of the ships
19     unsigned int m_number;     // number of ships of this size
20     QString m_shipsName;       // the english singular name of the ships of this size
21     QString m_shipsPluralName; // the english plural of the ships of this size
22 public:
Ships()23     Ships(): m_size(0), m_number(0), m_shipsName(), m_shipsPluralName() { }
24     Ships(unsigned int size, unsigned int number, QString& shipsName, QString& shipsPluralName);
size()25     inline unsigned int size() const { return m_size; }
number()26     inline unsigned int number() const { return m_number; }
shipsName()27     inline QString shipsName() const { return m_shipsName; }
pluralShipsName()28     inline QString pluralShipsName() const { return m_shipsPluralName; }
29 };
30 
31 // This configuration is interchanged in the network games.
32 // In the versions previous to KDE SC 4.13, only adjacent and multiple where interchanged.
33 class BattleShipsConfiguration
34 {
35 private:
36     unsigned int m_longestShip;
37     bool m_allowAdjacentShips;
38     unsigned int m_boardWidth;
39     unsigned int m_boardHeight;
40     bool m_fromXML;
41     QHash<unsigned int,Ships> m_ships;
42 public:
43 
44     explicit BattleShipsConfiguration(const bool fromXML=false);
45     explicit BattleShipsConfiguration(unsigned int longestShipSize, const bool allowAdjacentShips, const unsigned int boardWidth, const unsigned int boardHeight, const bool fromXML=false);
46     BattleShipsConfiguration(const BattleShipsConfiguration& copy) = default;
47     BattleShipsConfiguration& operator=(const BattleShipsConfiguration&) = default;
48     // does not add any ship longer than longestShip
49     // overwrites any previous configuration for ships of the requested size
50     BattleShipsConfiguration& addShips(unsigned int size, unsigned int number, QString shipsName, QString shipsPluralName);
51     BattleShipsConfiguration& addShips(Ships &ships);
52     unsigned int numberOfShipsOfSize(unsigned int size) const; // 0 if size is invalid
53     QString nameOfShipsOfSize(unsigned int size) const; // QString() if size is invalid
54     QString pluralNameOfShipsOfSize(unsigned int size) const; // QString() if size is invalid
55     bool multipleShips() const; // return true if any ship size has more than one ship
56 
57     void setLongestShipSize(unsigned int longestShipSize);
setAllowAdjacentShips(const bool allow)58     void setAllowAdjacentShips(const bool allow) { m_allowAdjacentShips = allow; }
setBoardWidth(const unsigned int boardWidth)59     void setBoardWidth(const unsigned int boardWidth) { m_boardWidth = boardWidth; }
setBoardHeight(const unsigned int boardHeight)60     void setBoardHeight(const unsigned int boardHeight) { m_boardWidth = boardHeight; }
setFromXML(bool fromXML)61     void setFromXML(bool fromXML) { m_fromXML=fromXML; }
boardWidth()62     inline unsigned int boardWidth() const { return m_boardWidth; }
boardHeight()63     inline unsigned int boardHeight() const { return m_boardHeight; }
isAllowedAdjacentShips()64     inline bool isAllowedAdjacentShips() const { return m_allowAdjacentShips; }
isFromXML()65     inline bool isFromXML() const { return m_fromXML; }
66     // ships are of 0 < size <= longestShip()
longestShip()67     inline unsigned int longestShip() const { return m_longestShip; }
68     unsigned int totalNumberOfShipsToPlay() const;
69     bool isAValidConfiguration() const;
70 
71     static BattleShipsConfiguration defaultSingleShipsConfiguration(const bool allowAdjacent, const bool fromXML = false);
72     static BattleShipsConfiguration defaultMultipleShipsConfiguration(const bool allowAdjacent, const bool fromXML = false);
73 };
74 
75 
76 
77 #endif // Ships_H
78