File indexing completed on 2024-04-14 04:00:28

0001 /*
0002     SPDX-FileCopyrightText: 2013 Jaime Torres <jtamate@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #include "ships.h"
0007 
0008 Ships::Ships(unsigned int size, unsigned int number, QString& shipsName, QString& shipsPluralName)
0009 :m_size(size)
0010 ,m_number(number)
0011 ,m_shipsName(shipsName)
0012 ,m_shipsPluralName(shipsPluralName)
0013 {
0014 }
0015 
0016 BattleShipsConfiguration::BattleShipsConfiguration(const bool fromXML)
0017 : m_longestShip(0)
0018 , m_allowAdjacentShips(true)
0019 , m_boardWidth(0)
0020 , m_boardHeight(0)
0021 , m_fromXML(fromXML)
0022 , m_ships()
0023 {
0024 }
0025 
0026 BattleShipsConfiguration::BattleShipsConfiguration(unsigned int longestShipSize, const bool allowAdjacentShips, const unsigned int boardWidth, const unsigned int boardHeight, const bool fromXML)
0027 : m_longestShip(longestShipSize)
0028 , m_allowAdjacentShips(allowAdjacentShips)
0029 , m_boardWidth(boardWidth)
0030 , m_boardHeight(boardHeight)
0031 , m_fromXML(fromXML)
0032 , m_ships()
0033 {
0034 }
0035 
0036 void BattleShipsConfiguration::setLongestShipSize(unsigned int longestShipSize)
0037 { 
0038     m_longestShip = longestShipSize; 
0039     m_ships.reserve(m_longestShip);
0040 }
0041 
0042 BattleShipsConfiguration& BattleShipsConfiguration::addShips(unsigned int size, unsigned int number, QString shipsName, QString shipsPluralName)
0043 {
0044     if ( size<=m_longestShip )
0045     {
0046         Ships toInsert(size, number, shipsName, shipsPluralName);
0047         m_ships[size]=toInsert;
0048     }
0049     return *this;
0050 }
0051 
0052 BattleShipsConfiguration& BattleShipsConfiguration::addShips(Ships& ships)
0053 {
0054     if ( ships.size()<=m_longestShip )
0055     {
0056         m_ships[ships.size()]=ships;
0057     }
0058     return *this;
0059 }
0060 
0061 unsigned int BattleShipsConfiguration::numberOfShipsOfSize(unsigned int size) const
0062 {
0063     return size <= m_longestShip ? m_ships[size].number() : 0;
0064 }
0065 
0066 QString BattleShipsConfiguration::nameOfShipsOfSize(unsigned int size) const
0067 {
0068     return size <= m_longestShip ? m_ships[size].shipsName() : QString();
0069 }
0070 
0071 QString BattleShipsConfiguration::pluralNameOfShipsOfSize(unsigned int size) const
0072 {
0073     return size <= m_longestShip ? m_ships[size].pluralShipsName() : QString();
0074 }
0075 
0076 bool BattleShipsConfiguration::multipleShips() const
0077 {
0078     bool res = false;
0079     for (unsigned int i=0; i<m_longestShip; i++)
0080     {
0081         res = res || (m_ships[i].number()>1);
0082     }
0083     return res;
0084 }
0085 
0086 bool BattleShipsConfiguration::isAValidConfiguration() const
0087 {
0088     if ( m_longestShip==0 || m_boardHeight==0 || m_boardWidth==0 
0089         || m_longestShip>qMax<unsigned int>(m_boardHeight, m_boardWidth) )
0090     {
0091         return false;
0092     }
0093     for (unsigned int size=1; size <= m_longestShip; size++)
0094     {
0095         if ( m_ships[size].number() ==0 )
0096         {
0097             return false;
0098         }
0099     }
0100     return true;
0101 }
0102 
0103 unsigned int BattleShipsConfiguration::totalNumberOfShipsToPlay() const
0104 {
0105     unsigned int sum=0;
0106     for (unsigned int size=1; size <= m_longestShip; size++)
0107     {
0108         sum += m_ships[size].number();
0109     }
0110     return sum;
0111 }
0112 
0113 
0114 BattleShipsConfiguration BattleShipsConfiguration::defaultSingleShipsConfiguration(const bool allowAdjacent, const bool fromXML)
0115 {
0116     BattleShipsConfiguration res(4, allowAdjacent, 10, 10, fromXML);
0117     return res.addShips(1, 1, QStringLiteral("minesweeper"), QStringLiteral("minesweepers"))
0118               .addShips(2, 1, QStringLiteral("frigate"), QStringLiteral("frigates"))
0119               .addShips(3, 1, QStringLiteral("cruise"), QStringLiteral("cruises"))
0120               .addShips(4, 1, QStringLiteral("carrier"), QStringLiteral("carriers"));
0121 }
0122 
0123 BattleShipsConfiguration BattleShipsConfiguration::defaultMultipleShipsConfiguration(const bool allowAdjacent, const bool fromXML)
0124 {
0125     BattleShipsConfiguration res(4, allowAdjacent, 10, 10, fromXML);
0126     return res.addShips(1, 4, QStringLiteral("minesweeper"), QStringLiteral("minesweepers"))
0127               .addShips(2, 3, QStringLiteral("frigate"), QStringLiteral("frigates"))
0128               .addShips(3, 2, QStringLiteral("cruise"), QStringLiteral("cruises"))
0129               .addShips(4, 1, QStringLiteral("carrier"), QStringLiteral("carriers"));
0130 }