forked from PKPL/ScorchedEarth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maps_load.c
101 lines (94 loc) · 2.51 KB
/
maps_load.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
//---------------------------- Load Maps ---------------------------------
// Loading maps
// PORTUGUESE TEAM
//------------------------------------------------------------------------
#include "maps_load.h"
void test_maps_load ()
{
int x, y;
int map_layout[MAX_X][MAX_Y] = {{0}}; // just for testing
load_Map (map_layout);
}
extern int map_layout[MAX_X][MAX_Y];
void load_Map (int map_layout[MAX_X][MAX_Y])
{
char option; // Because we will be different names for each map, we need to ask the name of the map that the user wants to load.
static bool existing_Map = false; // This variable allows to know if one map was already loaded. So, in order to this information will be not destroyed , we putted 'static int'
if (existing_Map != 0)
{
option = option_User ("You already loaded one map, do you want to load another and forget the last");
if (option == 'Y')
{
reading_from_File (map_layout);
existing_Map = true;
}
}
else
{
reading_from_File (map_layout);
existing_Map = true;
}
}
char option_User (char *str)
{
char option;
printf ("%s?\n(Y/N)", str);
do
{
fflush (stdin);
option = getchar ();
if (option != 'Y' && option != 'N')
{
printf ("Only 'Y' or 'N'\n");
}
}
while (option != 'Y' && option != 'N');
return option;
}
void read_String(char str[], int max)
{
char * ptr = NULL;
do
{
fgets(str, max, stdin);
ptr = strchr(str, '\n');
if (ptr != NULL)
{
*ptr = '\0';
}
if (strlen (str) == 0)
{
printf ("Write the name of the map.\n");
}
else
{
strcat (str, ".txt"); //concatenating the name; in this case, because it is a text file we concatenate ".txt"
}
}
while (strlen (str) == 0);
}
void reading_from_File (int map_layout [MAX_X][MAX_Y])
{
int x, y;
char name_File_Map[MAX_NAME_FILE];
FILE *map_Load = NULL;
fflush (stdin);
printf ("Name of the map to load: ");
read_String(name_File_Map, MAX_NAME_FILE);
map_Load = fopen (name_File_Map, "r");
if (map_Load == NULL)
{
fprintf (stderr, "Error on trying to load the map file");
}
else
{
for (y = 0; y < MAX_Y; y++)
{
for (x = 0; x < MAX_X; x++)
{
fscanf (map_Load, "%d", map_layout[x][y]);
}
}
fclose (map_Load);
}
}