This repository has been archived by the owner on Dec 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsave.c
executable file
·138 lines (110 loc) · 3.52 KB
/
save.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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
void save_game(int ship_count, char **ships, int *sizes, char *signs, char **pgrid, char **cgrid, int lines, int columns, int turn, bool shooting, int X_shooting, int Y_shooting)
{
FILE *fp = fopen("./save", "w+");
fprintf(fp, "%d\n", ship_count);
for (int i = 0; i < ship_count; ++i)
fprintf(fp, "%s %d %c\n", ships[i], sizes[i], signs[i]);
fprintf(fp, "%d %d\n", lines, columns);
for (int i = 0; i < lines; ++i)
{
fprintf(fp, "%c", pgrid[i][0] == ' '? '.' : pgrid[i][0]);
for (int j = 1; j < columns; ++j)
fprintf(fp, " %c", pgrid[i][j] == ' '? '.' : pgrid[i][j]);
fprintf(fp, "\n");
}
for (int i = 0; i < lines; ++i)
{
fprintf(fp, "%c", cgrid[i][0] == ' '? '.' : cgrid[i][0]);
for (int j = 1; j < columns; ++j)
fprintf(fp, " %c", cgrid[i][j] == ' '? '.' : cgrid[i][j]);
fprintf(fp, "\n");
}
fprintf(fp, "%d %d %d %d\n", turn, shooting, X_shooting, Y_shooting);
fclose(fp);
}
bool load_game(int *ship_count, char ***ships, int **sizes, char **signs, char ***pgrid, char ***cgrid, int *lines, int *columns, int *turn, bool *shooting, int *X_shooting, int *Y_shooting)
{
FILE *fp = fopen("./save", "r");
if (!fp)
return false;
int previous_ship_count = *ship_count;
int n = fscanf(fp, "%d\n", ship_count);
if (n < 1) {
fclose(fp);
return false;
}
for (int i = *ship_count; i < previous_ship_count; ++i)
free((*ships)[i]);
*ships = (char **) realloc(*ships, sizeof(char *) * *ship_count);
*sizes = (int *) realloc(*sizes, sizeof(int) * *ship_count);
*signs = (char *) realloc(*signs, sizeof(char) * *ship_count);
for (int i = previous_ship_count; i < *ship_count; ++i)
(*ships)[i] = (char *) malloc(sizeof(char) * 100);
for (int i = 0; i < *ship_count; ++i)
{
n = fscanf(fp, "%s %d %c\n", (*ships)[i], &(*sizes)[i], &(*signs)[i]);
if (n < 3) {
fclose(fp);
return false;
}
}
n = fscanf(fp, "%d %d\n", lines, columns);
if (n < 2) {
fclose(fp);
return false;
}
for (int i = 0; i < *lines; ++i)
{
n = fscanf(fp, "%c", &(*pgrid)[i][0]);
if (n < 1) {
fclose(fp);
return false;
}
if ((*pgrid)[i][0] == '.')
(*pgrid)[i][0] = ' ';
for (int j = 1; j < *columns; ++j)
{
n = fscanf(fp, " %c", &(*pgrid)[i][j]);
if (n < 1) {
fclose(fp);
return false;
}
if ((*pgrid)[i][j] == '.')
(*pgrid)[i][j] = ' ';
}
fscanf(fp, "\n");
}
for (int i = 0; i < *lines; ++i)
{
n = fscanf(fp, "%c", &(*cgrid)[i][0]);
if (n < 1) {
fclose(fp);
return false;
}
if ((*cgrid)[i][0] == '.')
(*cgrid)[i][0] = ' ';
for (int j = 1; j < *columns; ++j)
{
n = fscanf(fp, " %c", &(*cgrid)[i][j]);
if (n < 1) {
fclose(fp);
return false;
}
if ((*cgrid)[i][j] == '.')
(*cgrid)[i][j] = ' ';
}
fscanf(fp, "\n");
}
int shooting_int;
n = fscanf(fp, "%d %d %d %d\n", turn, &shooting_int, X_shooting, Y_shooting);
if (n < 4) {
fclose(fp);
return false;
}
*shooting = shooting_int;
fclose(fp);
return true;
}