author | viric@llimona |
Thu, 18 May 2006 23:33:03 +0200 | |
changeset 22 | d473e153a1de |
parent 19 | d46184674fe7 |
permissions | -rw-r--r-- |
6 | 1 |
#include <stdio.h> |
2 |
#include <string.h> |
|
3 |
#include <stdlib.h> |
|
4 |
#include <unistd.h> |
|
5 |
#include <signal.h> |
|
6 |
#include <assert.h> |
|
7 |
#include "general.h" |
|
8 |
||
9 |
/* Things related to the showing of the status */ |
|
10 |
float percent_to_show = 0; |
|
11 |
int depth_to_show = 0; |
|
12 |
||
13 |
int max_depth = 0; |
|
14 |
int min_depth_period = 0; |
|
15 |
int max_depth_period = 0; |
|
14
086a72472196
Now the segfault problem for actual_map is solved in os.c
viric@llimona
parents:
12
diff
changeset
|
16 |
struct Map * actual_map = NULL; |
6 | 17 |
|
19
d46184674fe7
Added a variable managing the output file handle (stdout,stderr)
viric@llimona
parents:
14
diff
changeset
|
18 |
static FILE *info_handle; |
d46184674fe7
Added a variable managing the output file handle (stdout,stderr)
viric@llimona
parents:
14
diff
changeset
|
19 |
|
6 | 20 |
|
21 |
void ReadMap(struct Map *M, char *FileName) |
|
22 |
{ |
|
23 |
FILE *Fitxer; |
|
24 |
int i,j; |
|
25 |
||
26 |
if(!(Fitxer = fopen(FileName, "r"))) |
|
27 |
{ |
|
28 |
printf("Error opening %s!", FileName); |
|
29 |
exit(1); |
|
30 |
} |
|
31 |
||
32 |
M->SizeX=0; |
|
33 |
M->SizeY=0; |
|
34 |
while (!feof(Fitxer)) |
|
35 |
{ |
|
36 |
fgets(M->Cells[M->SizeY], MAX_X, Fitxer); |
|
37 |
M->SizeY++; |
|
38 |
} |
|
39 |
M->SizeY--; |
|
40 |
M->SizeX = strlen(M->Cells[0]) - 1; |
|
41 |
||
42 |
M->NumPlatforms = 0; |
|
43 |
M->NumBoxesInPlatform = 0; |
|
44 |
M->NumBoxes = 0; |
|
45 |
for (j = 0; j<M->SizeY; j++) |
|
46 |
for (i=0; i<M->SizeX; i++) |
|
47 |
{ |
|
48 |
if (M->Cells[j][i] == MAN) |
|
49 |
{ |
|
50 |
M->Man.x = i; M->Man.y = j; |
|
51 |
M->Cells[M->Man.y][M->Man.x] = BLANK; |
|
52 |
} |
|
53 |
||
54 |
if (M->Cells[j][i] == PLATFORM) |
|
55 |
M->NumPlatforms++; |
|
56 |
else if (M->Cells[j][i] == BOXINPLATFORM) |
|
57 |
{ |
|
58 |
M->Cells[j][i] = PLATFORM; |
|
59 |
||
60 |
M->NumPlatforms++; |
|
61 |
M->NumBoxesInPlatform++; |
|
62 |
||
63 |
M->Box[M->NumBoxes].x = i; |
|
64 |
M->Box[M->NumBoxes].y = j; |
|
65 |
M->NumBoxes++; |
|
66 |
} else if (M->Cells[j][i] == BOX) |
|
67 |
{ |
|
68 |
M->Cells[j][i] = BLANK; |
|
69 |
||
70 |
M->Box[M->NumBoxes].x = i; |
|
71 |
M->Box[M->NumBoxes].y = j; |
|
72 |
M->NumBoxes++; |
|
73 |
} else if (M->Cells[j][i] == CORNER) |
|
74 |
{ |
|
75 |
M->Cells[j][i] = CORNER; |
|
76 |
} else if (M->Cells[j][i] != WALL) |
|
77 |
{ |
|
78 |
if ( (M->Cells[j][i-1] == WALL && |
|
79 |
M->Cells[j-1][i] == WALL) || |
|
80 |
(M->Cells[j][i-1] == WALL && |
|
81 |
M->Cells[j+1][i] == WALL) || |
|
82 |
(M->Cells[j][i+1] == WALL && |
|
83 |
M->Cells[j-1][i] == WALL) || |
|
84 |
(M->Cells[j][i+1] == WALL && |
|
85 |
M->Cells[j+1][i] == WALL)) |
|
86 |
M->Cells[j][i] = CORNER; |
|
87 |
} |
|
88 |
||
89 |
} |
|
90 |
||
91 |
} |
|
92 |
||
93 |
||
94 |
void ShowMap (const struct Map *M) |
|
95 |
{ |
|
96 |
struct Map Temp; |
|
97 |
int i,j; |
|
98 |
||
99 |
CopyMap(&Temp, M); |
|
100 |
||
101 |
Temp.Cells[Temp.Man.y][Temp.Man.x] = MAN; |
|
102 |
||
103 |
for (i = 0; i < Temp.NumBoxes; i++) |
|
104 |
{ |
|
105 |
if (Temp.Cells[Temp.Box[i].y][Temp.Box[i].x] == PLATFORM) |
|
106 |
Temp.Cells[Temp.Box[i].y][Temp.Box[i].x] =BOXINPLATFORM; |
|
107 |
else |
|
108 |
Temp.Cells[Temp.Box[i].y][Temp.Box[i].x] = BOX; |
|
109 |
} |
|
110 |
||
111 |
for (j = 0; j<Temp.SizeY; j++) |
|
112 |
{ |
|
113 |
for (i=0; i<Temp.SizeX; i++) |
|
19
d46184674fe7
Added a variable managing the output file handle (stdout,stderr)
viric@llimona
parents:
14
diff
changeset
|
114 |
fprintf(info_handle,"%c", Temp.Cells[j][i]); |
d46184674fe7
Added a variable managing the output file handle (stdout,stderr)
viric@llimona
parents:
14
diff
changeset
|
115 |
fprintf(info_handle,"\n"); |
6 | 116 |
} |
117 |
||
118 |
#if 0 |
|
8
b41a580b3abe
The code compiles with pedantic without warnings.
viric@llimona
parents:
6
diff
changeset
|
119 |
/* Print Where the man can move */ |
6 | 120 |
for (j = 0; j<Temp.SizeY; j++) |
121 |
{ |
|
122 |
for (i=0; i<Temp.SizeX; i++) |
|
123 |
printf("%c", Temp.ManMoves[j][i]); |
|
124 |
printf("\n"); |
|
125 |
} |
|
126 |
#endif |
|
127 |
||
19
d46184674fe7
Added a variable managing the output file handle (stdout,stderr)
viric@llimona
parents:
14
diff
changeset
|
128 |
fprintf(info_handle,"Man is at (%i,%i)\n", Temp.Man.x, Temp.Man.y); |
d46184674fe7
Added a variable managing the output file handle (stdout,stderr)
viric@llimona
parents:
14
diff
changeset
|
129 |
fprintf(info_handle,"Platforms: %i, BoxesInPlatform: %i\n", Temp.NumPlatforms, |
6 | 130 |
Temp.NumBoxesInPlatform); |
131 |
||
132 |
} |
|
133 |
||
10 | 134 |
void PrintMove(const struct BoxMove b) |
6 | 135 |
{ |
19
d46184674fe7
Added a variable managing the output file handle (stdout,stderr)
viric@llimona
parents:
14
diff
changeset
|
136 |
fprintf(info_handle,"Box: %i, Direction: {%i,%i}\n", b.box, b.dir.x, b.dir.y); |
6 | 137 |
} |
138 |
||
12
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
139 |
void show_percent_and_map() |
6 | 140 |
{ |
19
d46184674fe7
Added a variable managing the output file handle (stdout,stderr)
viric@llimona
parents:
14
diff
changeset
|
141 |
fprintf(info_handle, "Percent: %2.12f, depth: %i-%i\n", percent_to_show, |
6 | 142 |
min_depth_period, max_depth_period); |
14
086a72472196
Now the segfault problem for actual_map is solved in os.c
viric@llimona
parents:
12
diff
changeset
|
143 |
if(actual_map != NULL) |
086a72472196
Now the segfault problem for actual_map is solved in os.c
viric@llimona
parents:
12
diff
changeset
|
144 |
ShowMap(actual_map); |
19
d46184674fe7
Added a variable managing the output file handle (stdout,stderr)
viric@llimona
parents:
14
diff
changeset
|
145 |
fflush(info_handle); |
6 | 146 |
min_depth_period = MAX_STEPS; |
147 |
max_depth_period = 0; |
|
12
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
148 |
} |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
149 |
|
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
150 |
static void show_percent_callback(const int parameter) |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
151 |
{ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
152 |
show_percent_and_map(); |
6 | 153 |
alarm(ALARM_SECONDS); |
154 |
} |
|
155 |
||
156 |
static void program_alarm() |
|
157 |
{ |
|
158 |
struct sigaction my_action; |
|
159 |
int result; |
|
160 |
||
161 |
my_action.sa_handler = show_percent_callback; |
|
162 |
my_action.sa_flags = 0; |
|
163 |
memset(&my_action.sa_mask, 0, sizeof(my_action.sa_mask)); |
|
164 |
||
165 |
result = sigaction(SIGALRM, &my_action, NULL); |
|
166 |
assert(result == 0); |
|
167 |
||
168 |
alarm(1); |
|
169 |
} |
|
170 |
||
171 |
void init_os() |
|
172 |
{ |
|
19
d46184674fe7
Added a variable managing the output file handle (stdout,stderr)
viric@llimona
parents:
14
diff
changeset
|
173 |
info_handle = stdout; |
12
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
174 |
#ifndef DEBUG |
6 | 175 |
program_alarm(); |
12
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
176 |
#endif |
6 | 177 |
} |
12
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
178 |