-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
executable file
·161 lines (136 loc) · 2.55 KB
/
main.c
File metadata and controls
executable file
·161 lines (136 loc) · 2.55 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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
const int N = 10;
void initialize_grid(int **grid){
for (int i = 0; i < N; i++){
for (int j = 0; j < N; j++){
grid[i][j] = 0;
}
}
}
void initialize_front(char **front){
for (int i = 0; i < N; i++){
for (int j = 0; j < N; j++){
front[i][j] = 126;
}
}
}
void print_grid(int **grid){
for (int i = 0; i < N; i++){
for (int j = 0; j < N; j++){
printf("%d ", grid[j][i]);
}
printf("\n");
}
}
void print_front(char **front){
for (int i = 0; i < N; i++){
for (int j = 0; j < N; j++){
printf("%c ", front[j][i]);
}
printf("\n");
}
}
//direction
//0=east
//1=north
//2=west
//3=south
bool seed_ships_helper(int **grid, int x, int y, int ship_size, int dir){
if (ship_size == 0){
return true;
}
if (x >= 0 && x < N && y >= 0 && y < N){
if (grid[x][y] == 0){
if (dir == 0){
if (seed_ships_helper(grid, x+1, y, ship_size-1, dir)){
grid[x][y] = 1;
return true;
}
}
if (dir == 1){
if (seed_ships_helper(grid, x, y-1, ship_size-1, dir)){
grid[x][y] = 1;
return true;
}
}
if (dir == 2){
if (seed_ships_helper(grid, x-1, y, ship_size-1, dir)){
grid[x][y] = 1;
return true;
}
}
if (dir == 3){
if (seed_ships_helper(grid, x, y+1, ship_size-1, dir)){
grid[x][y] = 1;
return true;
}
}
}
return false;
}
return false;
}
void seed_ships(int **grid){
int x; int y; int dir;
int ships[5] = {5,4,3,3,2};
srand(time(NULL));
for (int i = 0; i < 5; i++){
while (true){
x = rand() % 10;
y = rand() % 10;
dir = rand() % 4;
if (seed_ships_helper(grid, x, y, ships[i], dir)){
break;
}
}
}
}
bool test_ships(int **grid){
for (int i = 0; i < N; i++){
for (int j = 0; j < N; j++){
if (grid[j][i] == 1){
return false;
}
}
}
return true;
}
void game_loop(int **grid, char **front){
int x; int y;
print_front(front);
while (true){
scanf("%d %d", &x, &y);
if (grid[x][y] == 1){
front[x][y] = 35;
}
if (grid[x][y] == 0){
front[x][y] = 42;
}
grid[x][y] = 2;
print_front(front);
if (test_ships(grid)){
printf("You win.");
return;
}
}
}
int main(){
int **grid = malloc(sizeof(*grid) * N);
for (int i = 0; i < N; i++) {
grid[i] = malloc(sizeof(*grid[i]) * N);
}
char **front = malloc(sizeof(*grid) * N);
for (int i = 0; i < N; i++) {
front[i] = malloc(sizeof(*front[i]) * N);
}
initialize_grid(grid);
initialize_front(front);
seed_ships(grid);
game_loop(grid, front);
free(grid);
free(front);
return 0;
}