-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.c
539 lines (465 loc) · 12 KB
/
Main.c
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
#include <stdio.h>
#define _CRT_SECURE_NO_WARNINGS
#define MAP_SIZE 6
int rand(void);
/*Task 1*/
void PrintRow(int row, int values[MAP_SIZE][MAP_SIZE])
{
// print all but last column
for (int col = 0; col < MAP_SIZE - 1; col++)
{
printf("%d ", values[row][col]);
}
// print the last column
printf("%d\n", values[row][MAP_SIZE - 1]);
}
void PrintArray(int values[MAP_SIZE][MAP_SIZE])
{
for (int row = 0; row < MAP_SIZE; row++)
{
PrintRow(row, values);
}
}
/*Task 2*/
void InitialiseMap(int map[MAP_SIZE][MAP_SIZE])
{
// place biggest ship, from the top left downward
for (int row = 0; row < 5; row++)
{
map[row][0] = 5;
}
// place the next biggest ship on the bottom left
for (int col = 1; col < 5; col++)
{
map[MAP_SIZE - 1][col] = 4;
}
// place the size 3 ship near top left
for (int row = MAP_SIZE / 2 - 2; row < MAP_SIZE / 2 + 1; row++)
{
map[row][2] = 3;
}
// place size 2 ship near the top right
map[1][MAP_SIZE - 2] = 2;
map[2][MAP_SIZE - 2] = 2;
}
/*Task 3*/
void AddRandomShip(int size, int map[MAP_SIZE][MAP_SIZE])
{
int placeVertically = rand() & 1;
int rowStart = rand() % (MAP_SIZE - size + 1);
int col = rand() % (MAP_SIZE - size);
for (int row = rowStart; row < rowStart + size; row++)
{
if (placeVertically)
{
map[row][col] = size;
}
else
{
map[col][row] = size;
}
}
}
/*Task 4*/
int CountValues(int value, int map[MAP_SIZE][MAP_SIZE])
{
int count = 0;
for (int row = 0; row < MAP_SIZE; row++)
{
for (int col = 0; col < MAP_SIZE; col++)
{
if (map[row][col] == value)
{
count++;
}
}
}
return count;
}
/*TASK 5*/
int TopLeftPosition(int size, int *row, int *col, int map[MAP_SIZE][MAP_SIZE])
{
// find the first occurence of the ship
for (int i = 0; i < MAP_SIZE; i++)
{
for (int j = 0; j < MAP_SIZE; j++)
{
if (map[i][j] == size)
{
*row = i;
*col = j;
// check if square to the right is also from the same ship
if (map[i][j + 1] == size)
{
return 1;
}
// otherwise it must have been vertical
return 2;
}
}
}
}
int IsSquareOccupied(int row, int col, int map[MAP_SIZE][MAP_SIZE])
{
if (row < 0 || row >= MAP_SIZE)
{
return 0;
}
if (col < 0 || col >= MAP_SIZE)
{
return 0;
}
return map[row][col] != 0;
// return 0 <= row < MAP_SIZE && 0 <= col < MAP_SIZE && map[row][col] != 0;
}
int IsHorizontalShipValid(int size, int row, int col, int map[MAP_SIZE][MAP_SIZE])
{
// check the square to the left
if (IsSquareOccupied(row, col - 1, map))
{
return 0;
}
// check the square to the right
if (IsSquareOccupied(row, col + size, map))
{
return 0;
}
// check all squares above and below the ship
for (int j = col; j < col + size; j++)
{
if (IsSquareOccupied(row - 1, j, map) || IsSquareOccupied(row + 1, j, map))
{
return 0;
}
}
// otherwise everything was ok so it was valid
return 1;
}
int IsVerticalShipValid(int size, int row, int col, int map[MAP_SIZE][MAP_SIZE])
{
// check square above
if (IsSquareOccupied(row - 1, col, map))
{
return 0;
}
// check square below
if (IsSquareOccupied(row + size, col, map))
{
return 0;
}
// check all squares to the left and right
for (int i = row; i < row + size; i++)
{
if (IsSquareOccupied(i, col - 1, map) || IsSquareOccupied(i, col + 1, map))
{
return 0;
}
}
// otherwise everything must have been ok so it was valid
return 1;
}
int IsShipValid(int size, int map[MAP_SIZE][MAP_SIZE])
{
int row, col, orientation;
// find the ship we are checking
orientation = TopLeftPosition(size, &row, &col, map);
if (orientation == 1)
{
return IsHorizontalShipValid(size, row, col, map);
}
return IsVerticalShipValid(size, row, col, map);
}
// task 7
void InitialiseRandomMap(int map[MAP_SIZE][MAP_SIZE])
{
int isMapValid;
do
{
isMapValid = 1;
// reset the array
for (int i = 0; i < MAP_SIZE; i++)
{
for (int j = 0; j < MAP_SIZE; j++)
{
map[i][j] = 0;
}
}
// add 4 ships
for (int size = 5; size >= 2; size--)
{
AddRandomShip(size, map);
// check if the ship added was not valid
if (IsShipValid(size, map) == 0)
{
isMapValid = 0;
break;
}
}
} while (isMapValid == 0);
}
int IsShipDestroyed(int orientation, int shipSize, int rowStart, int colStart, int shots[MAP_SIZE][MAP_SIZE])
{
// check for horizontal ship
if (orientation == 1)
{
for (int col = colStart; col < colStart + shipSize; col++)
{
if (shots[rowStart][col] == 0)
{
// ship hasnt been destroyed
return 0;
}
}
// ship has been destroyed
return 1;
}
// now check for vertically oriented ship
for (int row = rowStart; row < rowStart + shipSize; row++)
{
if (shots[row][colStart] == 0)
{
// ship hasnt been destroyed
return 0;
}
}
// ship has been destroyed
return 1;
}
void DestroyShip(int orientation, int rowStart, int colStart, int shipSize, int shots[MAP_SIZE][MAP_SIZE])
{
// check for horizontal ship
if (orientation == 1)
{
for (int col = colStart; col < colStart + shipSize; col++)
{
shots[rowStart][col] = (shots[rowStart][col] % 1000) + shipSize * 1000;
}
return;
}
// now check for vertically oriented ship
for (int row = rowStart; row < rowStart + shipSize; row++)
{
shots[row][colStart] = (shots[row][colStart] % 1000) + shipSize * 1000;
}
}
void FireShot(int shots[MAP_SIZE][MAP_SIZE], int map[MAP_SIZE][MAP_SIZE], int row, int col)
{
// check if square has already been shot
if (shots[row][col] != 0)
{
return;
}
int shotCount = 0;
// find the current shot count
for (int i = 0; i < MAP_SIZE; i++)
{
for (int j = 0; j < MAP_SIZE; j++)
{
if (shots[i][j] % 1000 > shotCount)
{
shotCount = shots[i][j] % 1000;
}
}
}
// move onto the next shot
shotCount++;
int shipSize = map[row][col];
// first check if the guess doesnt hit any ship
if (shipSize == 0)
{
shots[row][col] = shotCount;
return;
}
// hit the ship
shots[row][col] = shotCount + 1000;
// now check if ship was destroyed
// find the start of the ship being hit
int orientation, rowStart, colStart;
orientation = TopLeftPosition(shipSize, &rowStart, &colStart, map);
// check if ship wasn't destroyed
if (IsShipDestroyed(orientation, shipSize, rowStart, colStart, shots) == 0)
{
return;
}
// update all values to indicate a destroyed ship
DestroyShip(orientation, rowStart, colStart, shipSize, shots);
}
int CheckGameOver(int shots[MAP_SIZE][MAP_SIZE], int map[MAP_SIZE][MAP_SIZE])
{
//find the max ship size
int max = 2;
for (int i = 0; i < MAP_SIZE; i++) {
for (int j = 0; j < MAP_SIZE; j++) {
if (map[i][j] > max) {
max = map[i][j];
}
}
}
//iterate through all ships, check if its destroyed
for (int shipSize = 2; shipSize <= max; shipSize++) {
//locate the ship
int orientation, rowStart, colStart;
orientation = TopLeftPosition(shipSize, &rowStart, &colStart, map);
//return false if the ship is not destroyed
if (IsShipDestroyed(orientation, shipSize, rowStart, colStart, shots) == 0) {
return 0;
}
}
return 1;
}
/*TESTING FUNCTIONS*/
void TestPrintArray(void)
{
int map1[MAP_SIZE][MAP_SIZE] = {0};
printf("Map 1:\n");
PrintArray(map1);
int map2[MAP_SIZE][MAP_SIZE] = {0};
for (int i = 0; i < MAP_SIZE; i++)
{
map2[MAP_SIZE - i - 1][i] = i;
}
printf("\nMap 2:\n");
PrintArray(map2);
}
void TestInitialiseMap(void)
{
int map1[MAP_SIZE][MAP_SIZE] = {0};
InitialiseMap(map1);
printf("Map: \n");
PrintArray(map1);
}
void TestAddRandomShip(void)
{
int map1[MAP_SIZE][MAP_SIZE] = {0};
int map2[MAP_SIZE][MAP_SIZE] = {0};
AddRandomShip(5, map1);
printf("Map: \n");
PrintArray(map1);
AddRandomShip(2, map2);
AddRandomShip(3, map2);
AddRandomShip(4, map2);
AddRandomShip(5, map2);
printf("Map: \n");
PrintArray(map2);
}
void TestCountValues(void)
{
int map[MAP_SIZE][MAP_SIZE] = {0};
int count, shipSize;
InitialiseMap(map);
PrintArray(map);
for (shipSize = 2; shipSize <= 5; shipSize++)
{
count = CountValues(shipSize, map);
printf("The value %d appears %d times\n", shipSize,
count);
}
}
void TestTopLeftPosition(void)
{
int map[MAP_SIZE][MAP_SIZE] = {0};
int row, col, direction, shipSize;
InitialiseMap(map);
PrintArray(map);
for (shipSize = 2; shipSize <= 5; shipSize++)
{
direction = TopLeftPosition(shipSize, &row, &col, map);
printf("Ship %d is at (%d, %d) facing %d\n", shipSize,
row, col, direction);
}
}
void TestIsShipValid(void)
{
int map[MAP_SIZE][MAP_SIZE] = {0};
int valid, shipSize;
InitialiseMap(map);
PrintArray(map);
for (shipSize = 2; shipSize <= 5; shipSize++)
{
valid = IsShipValid(shipSize, map);
printf("Is ship %d valid? %d\n", shipSize, valid);
}
// move ship 3 to invalid pos
map[1][2] = 0;
map[2][2] = 0;
map[3][2] = 0;
map[5][0] = 3;
map[5][1] = 3;
map[5][2] = 3;
PrintArray(map);
for (shipSize = 2; shipSize <= 5; shipSize++)
{
valid = IsShipValid(shipSize, map);
printf("Is ship %d valid? %d\n", shipSize, valid);
}
}
void TestOccupiedSquares(void)
{
int map[MAP_SIZE][MAP_SIZE] = {0};
InitialiseMap(map);
PrintArray(map);
for (int i = -1; i < MAP_SIZE + 1; i++)
{
for (int j = -1; j < MAP_SIZE + 1; j++)
{
printf("Is the square %d, %d occupied?: %d\n", i, j, IsSquareOccupied(i, j, map));
}
}
}
void TestInitialiseRandomMap(void)
{
int map[MAP_SIZE][MAP_SIZE] = {0};
InitialiseRandomMap(map);
PrintArray(map);
}
void TestFireShot(void)
{
int map[MAP_SIZE][MAP_SIZE] = {0};
int shots[MAP_SIZE][MAP_SIZE] = {0};
InitialiseMap(map);
printf("Map:\n");
PrintArray(map);
printf("Shots:\n");
PrintArray(shots);
FireShot(shots, map, 2, 0);
printf("Shots:\n");
PrintArray(shots);
FireShot(shots, map, 2, 1);
printf("Shots:\n");
PrintArray(shots);
FireShot(shots, map, 2, 2);
printf("Shots:\n");
PrintArray(shots);
FireShot(shots, map, 2, 3);
printf("Shots:\n");
PrintArray(shots);
FireShot(shots, map, 0, 0);
FireShot(shots, map, 1, 0);
FireShot(shots, map, 2, 0);
FireShot(shots, map, 3, 0);
FireShot(shots, map, 4, 0);
FireShot(shots, map, 5, 0);
printf("Shots:\n");
PrintArray(shots);
}
void TestCheckGameOver(void)
{
int map[MAP_SIZE][MAP_SIZE] = {0};
int shots[MAP_SIZE][MAP_SIZE] = {0};
InitialiseMap(map);
printf("Map:\n");
PrintArray(map);
printf("Shots:\n");
PrintArray(shots);
while (!CheckGameOver(shots, map))
{
FireShot(shots, map, rand() % MAP_SIZE, rand() % MAP_SIZE);
}
PrintArray(shots);
}
/*END TESTING FUNCTIONS*/
int main()
{
TestCheckGameOver();
return 0;
}