1 /*
2     SPDX-FileCopyrightText: 2013 Jaime Torres <jtamate@gmail.com>
3 
4     SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 #include "ships.h"
7 
Ships(unsigned int size,unsigned int number,QString & shipsName,QString & shipsPluralName)8 Ships::Ships(unsigned int size, unsigned int number, QString& shipsName, QString& shipsPluralName)
9 :m_size(size)
10 ,m_number(number)
11 ,m_shipsName(shipsName)
12 ,m_shipsPluralName(shipsPluralName)
13 {
14 }
15 
BattleShipsConfiguration(const bool fromXML)16 BattleShipsConfiguration::BattleShipsConfiguration(const bool fromXML)
17 : m_longestShip(0)
18 , m_allowAdjacentShips(true)
19 , m_boardWidth(0)
20 , m_boardHeight(0)
21 , m_fromXML(fromXML)
22 , m_ships()
23 {
24 }
25 
BattleShipsConfiguration(unsigned int longestShipSize,const bool allowAdjacentShips,const unsigned int boardWidth,const unsigned int boardHeight,const bool fromXML)26 BattleShipsConfiguration::BattleShipsConfiguration(unsigned int longestShipSize, const bool allowAdjacentShips, const unsigned int boardWidth, const unsigned int boardHeight, const bool fromXML)
27 : m_longestShip(longestShipSize)
28 , m_allowAdjacentShips(allowAdjacentShips)
29 , m_boardWidth(boardWidth)
30 , m_boardHeight(boardHeight)
31 , m_fromXML(fromXML)
32 , m_ships()
33 {
34 }
35 
setLongestShipSize(unsigned int longestShipSize)36 void BattleShipsConfiguration::setLongestShipSize(unsigned int longestShipSize)
37 {
38     m_longestShip = longestShipSize;
39     m_ships.reserve(m_longestShip);
40 }
41 
addShips(unsigned int size,unsigned int number,QString shipsName,QString shipsPluralName)42 BattleShipsConfiguration& BattleShipsConfiguration::addShips(unsigned int size, unsigned int number, QString shipsName, QString shipsPluralName)
43 {
44     if ( size<=m_longestShip )
45     {
46         Ships toInsert(size, number, shipsName, shipsPluralName);
47         m_ships[size]=toInsert;
48     }
49     return *this;
50 }
51 
addShips(Ships & ships)52 BattleShipsConfiguration& BattleShipsConfiguration::addShips(Ships& ships)
53 {
54     if ( ships.size()<=m_longestShip )
55     {
56         m_ships[ships.size()]=ships;
57     }
58     return *this;
59 }
60 
numberOfShipsOfSize(unsigned int size) const61 unsigned int BattleShipsConfiguration::numberOfShipsOfSize(unsigned int size) const
62 {
63     return size <= m_longestShip ? m_ships[size].number() : 0;
64 }
65 
nameOfShipsOfSize(unsigned int size) const66 QString BattleShipsConfiguration::nameOfShipsOfSize(unsigned int size) const
67 {
68     return size <= m_longestShip ? m_ships[size].shipsName() : QString();
69 }
70 
pluralNameOfShipsOfSize(unsigned int size) const71 QString BattleShipsConfiguration::pluralNameOfShipsOfSize(unsigned int size) const
72 {
73     return size <= m_longestShip ? m_ships[size].pluralShipsName() : QString();
74 }
75 
multipleShips() const76 bool BattleShipsConfiguration::multipleShips() const
77 {
78     bool res = false;
79     for (unsigned int i=0; i<m_longestShip; i++)
80     {
81         res = res || (m_ships[i].number()>1);
82     }
83     return res;
84 }
85 
isAValidConfiguration() const86 bool BattleShipsConfiguration::isAValidConfiguration() const
87 {
88     if ( m_longestShip==0 || m_boardHeight==0 || m_boardWidth==0
89         || m_longestShip>qMax<unsigned int>(m_boardHeight, m_boardWidth) )
90     {
91         return false;
92     }
93     for (unsigned int size=1; size <= m_longestShip; size++)
94     {
95         if ( m_ships[size].number() ==0 )
96         {
97             return false;
98         }
99     }
100     return true;
101 }
102 
totalNumberOfShipsToPlay() const103 unsigned int BattleShipsConfiguration::totalNumberOfShipsToPlay() const
104 {
105     unsigned int sum=0;
106     for (unsigned int size=1; size <= m_longestShip; size++)
107     {
108         sum += m_ships[size].number();
109     }
110     return sum;
111 }
112 
113 
defaultSingleShipsConfiguration(const bool allowAdjacent,const bool fromXML)114 BattleShipsConfiguration BattleShipsConfiguration::defaultSingleShipsConfiguration(const bool allowAdjacent, const bool fromXML)
115 {
116     BattleShipsConfiguration res(4, allowAdjacent, 10, 10, fromXML);
117     return res.addShips(1, 1, QStringLiteral("minesweeper"), QStringLiteral("minesweepers"))
118               .addShips(2, 1, QStringLiteral("frigate"), QStringLiteral("frigates"))
119               .addShips(3, 1, QStringLiteral("cruise"), QStringLiteral("cruises"))
120               .addShips(4, 1, QStringLiteral("carrier"), QStringLiteral("carriers"));
121 }
122 
defaultMultipleShipsConfiguration(const bool allowAdjacent,const bool fromXML)123 BattleShipsConfiguration BattleShipsConfiguration::defaultMultipleShipsConfiguration(const bool allowAdjacent, const bool fromXML)
124 {
125     BattleShipsConfiguration res(4, allowAdjacent, 10, 10, fromXML);
126     return res.addShips(1, 4, QStringLiteral("minesweeper"), QStringLiteral("minesweepers"))
127               .addShips(2, 3, QStringLiteral("frigate"), QStringLiteral("frigates"))
128               .addShips(3, 2, QStringLiteral("cruise"), QStringLiteral("cruises"))
129               .addShips(4, 1, QStringLiteral("carrier"), QStringLiteral("carriers"));
130 }
131