File indexing completed on 2024-02-18 04:04:55

0001 /*
0002     SPDX-FileCopyrightText: 2007 Paolo Capriotti <p.capriotti@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "ship.h"
0008 
0009 #include "knavalbattle_debug.h"
0010 
0011 Ship::Ship(unsigned int size, Direction direction, Coord position)
0012 : m_size(size)
0013 , m_position(position)
0014 , m_direction(direction)
0015 {
0016     m_life = size;
0017 }
0018 
0019 const Coord Ship::position() const
0020 {
0021     return m_position;
0022 }
0023 
0024 void Ship::setPosition(const Coord & position)
0025 {
0026     m_position = position;
0027 }
0028 
0029 void Ship::changeDirection()
0030 {
0031     m_direction = m_direction == TOP_DOWN ? LEFT_TO_RIGHT : TOP_DOWN;
0032 }
0033 
0034 Coord Ship::increment(Direction direction)
0035 {
0036     return direction == TOP_DOWN ? Coord(0, 1) : Coord(1, 0);
0037 }
0038 
0039 Coord Ship::decrement(Direction direction)
0040 {
0041     return direction == TOP_DOWN ? Coord(0, -1) : Coord(-1, 0);
0042 }
0043 
0044 Coord Ship::incrementPerpendicular(Direction direction)
0045 {
0046     return direction == TOP_DOWN ? Coord(1, 0) : Coord(0, 1);
0047 }
0048 
0049 Coord Ship::decrementPerpendicular(Direction direction)
0050 {
0051     return direction == TOP_DOWN ? Coord(-1, 0) : Coord(0, -1);
0052 }
0053 
0054 Coord Ship::increment() const
0055 {
0056     return increment(m_direction);
0057 }
0058 
0059 Coord Ship::decrement() const
0060 {
0061     return decrement(m_direction);
0062 }
0063 
0064 Coord Ship::incrementPerpendicular() const
0065 {
0066     return incrementPerpendicular(m_direction);
0067 }
0068 
0069 Coord Ship::decrementPerpendicular() const
0070 {
0071     return decrementPerpendicular(m_direction);
0072 }
0073 
0074 void Ship::decLife()
0075 {
0076     Q_ASSERT(alive());
0077     m_life--;
0078 }
0079 
0080 bool Ship::alive() const
0081 {
0082     return m_life > 0;
0083 }
0084 
0085