-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShip.cpp
51 lines (42 loc) · 766 Bytes
/
Ship.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//
// Ship.cpp
// Battleship
//
// Created by Janik Karafiat on 23.06.23.
//
#include <cstdlib>
#include <ctime>
#include "Ship.hpp"
#include "Map.hpp"
Ship::Ship(int size) : size(size), hits(size), alive(true) {}
Ship::~Ship()
{
delete this;
}
bool Ship::hit() {
if(this->alive) {
this->hits--;
std::cout << this->hits << std::endl;
if(this->hits == 0) {
this->alive = false;
return true;
}
}
return false;
}
void Ship::place(Map *map) {
bool horizontal = rand() % 2;
int tryCount = 0;
int x, y;
do {
x = rand() % map->getWidth();
y = rand() % map->getHeight();
tryCount++;
} while(!map->placeShip(this, x, y, horizontal, false));
}
bool Ship::isAlive() {
return this->alive;
}
int Ship::getSize() {
return this->size;
}