-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
270 lines (209 loc) · 6.16 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
//
// main.c
// battleships
//
// Created by Radek on 3/1/20.
// Copyright © 2020 Radek. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <string.h>
#define EMPTY '.'
#define MISSED 'M'
#define HIT 'H'
void initBoard(char gameTable[10][10]);
void printBoard(char gameTable[10][10]);
struct shipStruct {
int health;
int size;
char name[20];
};
struct gameStats {
int missilesFired;
int shipHits;
int shipMisses;
};
void initializeAllShips(struct shipStruct allShips[5], char grid [10][10]);
int checkForWrap(int xx, int yy, int direction, int size);
int checkNoCollision(char grid[10][10], int xx, int yy, int direction, int size);
void placeShip(struct shipStruct aShip, char grid[10][10], int shipId);
void fire(int xx, int yy, char gameTable[10][10], struct shipStruct allShips[5],struct gameStats);
void printTheGame(char gameTable[10][10], struct shipStruct allShips[5],struct gameStats game);
int main(int argc, const char * argv[]) {
srand(time(0));
printf("\n");
char gameTable[10][10] = {0};
//fk
int j;
struct shipStruct allShips[5];
struct gameStats game;
game.shipHits=0;
game.shipMisses=0;
game.missilesFired=11;
initBoard(gameTable);
//printBoard(gameTable);
initializeAllShips(allShips, gameTable);
printBoard(gameTable);
// getUser target xx,yy
printf("[MAIN] Calling fire [5,5]\n");
fire(5, 5, gameTable, allShips,game);
printf("\n");
printf("[MAIN] Ship Hits %d \n",game.shipHits);
printf("[Main] Ship Miss %d \n",game.shipMisses);
printf("[MAIN] Ship Miss %d \n",game.missilesFired);
printBoard(gameTable);
printTheGame(gameTable, allShips,game);
return 0;
}
void fire(int xx, int yy, char gameTable[10][10], struct shipStruct allShips[5],struct gameStats game) {
printf("_______________\n");
printf("%d %d %d\n", game.missilesFired, game.shipMisses, game.shipHits);
printf("[FIRE] Taking a shot\n");
int i = gameTable[xx][yy] - '0';
game.missilesFired++;
printf("[FIRE] Ship Miss %d \n",game.missilesFired);
if (i >= 0 && i <= 4) {
printf("[FIRE] We whacked ship index %d\n", i);
printf("[FIRE] Health was %d\n", allShips[i].health);
allShips[i].health--;
printf("[FIRE] Health is %d\n", allShips[i].health);
gameTable[xx][yy] = HIT;
game.shipHits++;
printf("[FIRE] Ship Hits %d \n",game.shipHits);
return;
}
gameTable[xx][yy] = MISSED;
game.shipMisses++;
printf("[FIRE] Ship Miss %d \n",game.shipMisses);
}
void placeShip(struct shipStruct aShip, char grid[10][10], int shipId) {
//
int placed = 0;
//printf("[PLACE SHIP] We're inside\n");
//printBoard(grid);
do {
//printf("[PLACE SHIP] INSIDE DO with ship size = %d\n",aShip.size);
int xx = rand() % 10;
int yy = rand() % 10;
int direction = rand() % 4;
printf("[PLACE SHIP] Testing x=%d y=%d, dir=%d\n", xx, yy, direction);
if ( checkForWrap(xx, yy, direction, aShip.size) == 1) {
printf("[PLACE SHIP] Check for wrap was TRUE\n");
if ( checkNoCollision(grid, xx, yy, direction, aShip.size) == 1 ) {
//place ship
for (int i = 0; i < aShip.size; i++) {
printf("[PLACE SHIP] About to place %d [i=%d] at x=%d, y=%d\n", aShip.size + '0', i, xx, yy);
grid[xx][yy] = shipId + '0';
// grid[xx][yy] = 0xFF;
if (direction == 0) xx++; //right
if (direction == 2) xx--; //left
if (direction == 3) yy--; //up
if (direction == 1) yy++; //down
//printBoard(grid);
}
placed = 1;
printf("[PLACE SHIP] PLACED ship\n");
}
}
} while (!placed);
}
int checkForWrap(int xx, int yy, int direction, int size) {
if (direction == 0 && xx + size >= 9) return 0;
if (direction == 2 && xx - size <= 0) return 0;
if (direction == 1 && yy + size >= 9) return 0;
if (direction == 3 && yy - size <= 0) return 0;
return 1;
}
int checkNoCollision(char grid[10][10], int xx, int yy, int direction, int size) {
while (size > 0) {
if (grid[xx][yy] != EMPTY) {
printf("[CHECK NO COLL] Collision!!!!!!\n");
return 0;
}
if (direction == 0) xx++; //right
if (direction == 2) xx--; //left
if (direction == 3) yy--; //up
if (direction == 1) yy++; //down
size--;
}
printf("[CHECK NO COLL] No collision\n");
return 1;
}
void initializeAllShips(struct shipStruct allShips[5], char grid[10][10]) {
printf("\n");
allShips[0].size = 5;
strcpy(allShips[0].name, "Air Force Academy");
allShips[0].health = 5;
allShips[1].size = 4;
allShips[1].health = 4;
strcpy(allShips[1].name, "Valencia Destroyer");
allShips[2].size = 3;
allShips[2].health = 3;
strcpy(allShips[2].name, "Eskimo University");
allShips[3].size = 3;
allShips[3].health = 3;
strcpy(allShips[3].name, "Seminole State ");
allShips[4].size = 2;
allShips[4].health = 2;
strcpy(allShips[4].name, "Deland High School");
for (int i = 0; i <= 4; i++) {
printf("INITALLSHIPS: calling placeShip with i=%d\n", i);
placeShip( allShips[i], grid, i);
}
}
void initBoard(char gameTable[10][10]) {
for (int y = 0; y < 10; y++) {
for (int x = 0; x < 10; x++) {
//gameTable[x][y]=x+10*y;
gameTable[x][y] = EMPTY;
}
}
}
void printBoard(char gameTable[10][10]) {
for (int y = 0; y < 10; y++) {
for (int x = 0; x < 10; x++) {
// printf("%i ",gameTable[x][y]);
printf("%c ", gameTable[x][y]);
}
printf("\n");
}
}
void printTheGame(char gameTable[10][10], struct shipStruct allShips[5],struct gameStats game){
printf(" ");
for (int i = 'A'; i<='J'; i++){
printf("%c ",i);
}
printf("\n");
for (int y=1; y<=10; y++){
printf("[");
if(y<10) printf("0");
printf("%d] ",y);
for(int x=0; x<10;x++){
char c = gameTable[x][y-1];
if (c>='0' && c<='4') c=EMPTY;
printf("%c ",c);
}
printf("\n");
}
for(int i =0; i <5; i++){
printf("%s\t", allShips[i].name);
if(allShips[i].health > 0){
printf("floating");
}
else{
printf("sunk");
}
if(i == 0){
printf("\tMisseles Fired\t%d",game.missilesFired);
}
if(i==2){
printf("\tShip Hits\t%d",game.shipHits);
}
if(i==4){
printf("\tShip Misses\t%d",game.shipMisses);
}
printf("\n");
}
}