-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBattleShip.cpp
More file actions
176 lines (147 loc) · 4.91 KB
/
BattleShip.cpp
File metadata and controls
176 lines (147 loc) · 4.91 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <iostream>
using namespace std;
void createShipPos (int shipPos [4][2], int positions[8][8]);
void showBoard (int positions[8][8]);
void fire (int positions[8][8], int fireX, int fireY, int &torpedoes);
int main()
{
srand (time(NULL));
int width = 10, length = 10;
int torpedoes = 15;
int positions[8][8] = {};
int subPos [4][2] = {};
int subHit = 0;
int fireX, fireY;
bool win = false, loss = false;
for (int i = 0; i < 8; i++) {
for (int a = 0; a < 8; a++) {
positions[i][a] = 0;
}
}
createShipPos(subPos, positions);
while (torpedoes != 0 && win == false && loss == false) {
showBoard(positions);
cout << endl << torpedoes << " torpedoes remain. Fire where?";
cin >> fireX >> fireY;
fire(positions, fireX, fireY, torpedoes);
for (int i = 0; i < 4; i++) {
if (positions[subPos[i][0]][subPos[i][1]] == 3) {
subHit++;
}
}
if (subHit >= 4) {
cout << "Submarine sunk!" << endl;
win = true;
} else {
subHit = 0;
}
if (torpedoes <= 0 && win != true) {
loss = true;
}
}
if (loss == true) {
cout << "Not all ships have sunk! Out of torpedoes! You lose ;(" << endl;
} else if (win == true) {
cout << "All ships have sunk! You win :)" << endl;
}
return 0;
}
void createShipPos (int shipPos [4][2], int positions[8][8]) {
bool directionPicked = false;
int directionTry = 0;
shipPos [0] [0] = {rand() % 8};
shipPos [0] [1] = {rand() % 8};
int direction = rand() % 4 + 1;
while (directionPicked == false && directionTry < 4) {
if (direction == 1) {
if (shipPos[0][1] >= 3) {
for (int i = 1; i < 4; i++) {
shipPos[i][0] = shipPos[0][0];
shipPos[i][1] = shipPos[0][1] - i;
}
directionPicked = true;
} else {
directionTry++;
direction = 2;
}
}
if (direction == 2) {
if (shipPos[0][0] <= 4) {
for (int i = 1; i < 4; i++) {
shipPos[i][0] = shipPos[0][0] + i;
shipPos[i][1] = shipPos[0][1];
}
directionPicked = true;
} else {
directionTry++;
direction = 3;
}
}
if (direction == 3) {
if (shipPos[0][1] <= 4) {
for (int i = 1; i < 4; i++) {
shipPos[i][0] = shipPos[0][0];
shipPos[i][1] = shipPos[0][1] + i;
}
directionPicked = true;
} else {
directionTry++;
direction = 4;
}
}
if (direction == 4) {
if (shipPos[0][0] >= 3) {
for (int i = 1; i < 4; i++) {
shipPos[i][0] = shipPos[0][0] - i;
shipPos[i][1] = shipPos[0][1];
}
directionPicked = true;
} else {
directionTry++;
direction = 1;
}
}
}
if (directionTry >= 4) {
createShipPos (shipPos, positions);
}
for (int i = 0; i < 4; i++) {
positions [shipPos[i][0]] [shipPos[i][1]] = 1;
}
}
void showBoard (int positions[8][8]) {
cout << endl << " ";
for (int i = 0; i < 8; i++) {
cout << " " << i << " ";
}
for (int i = 0; i < 8; i++) {
cout << endl;
cout << i;
for (int a = 0; a < 8; a++) {
if (positions[i][a] == 0) {
cout << " . ";
} else if (positions[i][a] == 1) {
cout << " . ";
} else if (positions[i][a] == 2) {
cout << " O ";
} else if (positions[i][a] == 3) {
cout << " X ";
}
}
}
}
void fire (int positions[8][8], int fireX, int fireY, int &torpedoes) {
if (positions [fireX] [fireY] == 0) {
positions [fireX] [fireY] = 2;
torpedoes--;
cout << "Miss!" << endl;
} else if (positions [fireX] [fireY] == 1) {
positions [fireX] [fireY] = 3;
torpedoes--;
cout << "Hit!" << endl;
} else if (positions [fireX] [fireY] == 2 || positions [fireX] [fireY] == 3) {
cout << endl << "You've already shot there. " << torpedoes << " torpedoes remain. Fire where?";
cin >> fireX >> fireY;
fire(positions, fireX, fireY, torpedoes);
}
}