File indexing completed on 2024-09-15 03:44:39
0001 /* 0002 SPDX-FileCopyrightText: 2007 Paolo Capriotti <p.capriotti@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #ifndef Sea_H 0008 #define Sea_H 0009 0010 #include <QList> 0011 #include <QObject> 0012 0013 #include "ship.h" 0014 #include "ships.h" 0015 #include "hitinfo.h" 0016 0017 class BattleField; 0018 0019 class Sea : public QObject 0020 { 0021 Q_OBJECT 0022 public: 0023 enum Status 0024 { 0025 PLACING_SHIPS, 0026 PLAYING, 0027 A_WINS, 0028 B_WINS 0029 }; 0030 enum Player 0031 { 0032 PLAYER_A = 0, 0033 PLAYER_B = 1, 0034 NO_PLAYER = -1 0035 }; 0036 private: 0037 Coord m_size; 0038 Player m_turn; 0039 BattleField* m_fields[2]; 0040 QList<Ship *> m_enemyShips; 0041 QList<Ship *> m_myShips; 0042 Status m_status; 0043 BattleShipsConfiguration m_battle_ships_configuration; 0044 0045 inline BattleField* currentField() const { return m_fields[m_turn]; } 0046 inline BattleField* otherField() const { return m_fields[opponent(m_turn)]; } 0047 0048 void checkGameOver(); 0049 public: 0050 Sea(QObject* parent, const BattleShipsConfiguration& battleShipsConfiguration); 0051 ~Sea() override; 0052 0053 bool canAddShip(Player p, const Coord& pos, int size, Ship::Direction direction) const; 0054 bool canAddShipOfSize(Player p, int size) const; 0055 void add(Player p, int n); 0056 void add(Player p, Ship* ship); 0057 void addBorder(Player p, const Coord& pos); 0058 bool canHit(Player p, const Coord& pos) const; 0059 HitInfo hit(const Coord& pos); 0060 void forceHit(const Coord& pos, const HitInfo& info); 0061 void startPlaying(); 0062 void abort(Player p); 0063 const Element& at(Sea::Player player, const Coord& pos) const; 0064 bool valid(Sea::Player, const Coord& pos) const; 0065 void switchTurn(); 0066 bool isNearShip(Sea::Player, const Coord& pos) const; 0067 void allowAdjacentShips(const bool allow_adjacent_ships); 0068 void clear(Sea::Player); 0069 void setBattleShipsConfiguration(const BattleShipsConfiguration& configuration); 0070 0071 const QList<Ship *> enemyShips() const; 0072 const QList<Ship *> myShips() const; 0073 0074 inline Status status() const { return m_status; } 0075 inline Player turn() const { return m_turn; } 0076 static Player opponent(Player p); 0077 inline Coord size() const { return m_size; } 0078 inline bool isAdjacentShipsAllowed() const { return m_battle_ships_configuration.isAllowedAdjacentShips(); } 0079 inline bool isSeveralShipsAllowed() const { return m_battle_ships_configuration.multipleShips(); } 0080 inline const BattleShipsConfiguration* battleShipsConfiguration() const { return &m_battle_ships_configuration; } 0081 }; 0082 0083 #endif // Sea_H 0084