-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathship.cpp
More file actions
52 lines (44 loc) · 1.05 KB
/
Copy pathship.cpp
File metadata and controls
52 lines (44 loc) · 1.05 KB
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
#include "ship.h"
#include <string>
using namespace std;
//2 separate constructors for vert/horiz ships
//horizontal
Ship::Ship(int x, int y, int length, string h, string name)
{
//will be called when placing ships
for (int i = x; i < x + length; i++) {
pieces.push_back(pair<int,int>(i,y));
}
this->name = name;
//?? v
h = "";
life = length;
}
//vertical
Ship::Ship(int x, int y, int length, string name) {
for (int i = y; i < y + length; i++) {
pieces.push_back(pair<int,int>(x,i));
}
//?? v
this->name = name;
life = length;
}
Ship::Ship() {
}
Ship::~Ship()
{
}
int Ship::check(int x, int y) {
//if coords are in our vector of pairs, return 2(hit), then lose life
//otherwise return 1(miss)
if(std::find(pieces.begin(), pieces.end(), pair<int,int>(x,y)) != pieces.end()) {
if (life > 0) life -= 1;
if (life == 0) cout << "You sunk the opponent's " << name << endl << endl;
return 2;
} else {
return 1;
}
}
int Ship::getLife() {
return life;
}