-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.c
100 lines (83 loc) · 2.3 KB
/
game.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
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <ctype.h>
#include "game.h"
struct game G;
void game_read(void)
{
int i, j;
assert(scanf(" %d %d", &G.gi.start[0], &G.gi.start[1]) == 2);
assert(scanf(" %d %d", &G.gi.end[0], &G.gi.end[1]) == 2);
assert(scanf(" %d", &G.gi.num_prizes) == 1);
G.gi.num_prizes_orig = G.gi.num_prizes;
G.gi.prizes = malloc(sizeof(int) * 2 * G.gi.num_prizes);
G.gi.prizes_visited = malloc(sizeof(int) * G.gi.num_prizes);
for (i = 0; i < G.gi.num_prizes; i++) {
int x, y;
assert(scanf(" %d %d", &x, &y) == 2);
G.gi.prizes[i][0] = x;
G.gi.prizes[i][1] = y;
}
assert(scanf(" %d %d", &G.gi.map_size[0], &G.gi.map_size[1]) == 2);
G.gi.map = malloc(sizeof(char) * G.gi.map_size[0] * G.gi.map_size[1]);
G.gi.mapv = malloc(sizeof(int) * G.gi.map_size[0] * G.gi.map_size[1]);
G.gi.mapw = malloc(sizeof(int) * G.gi.map_size[0] * G.gi.map_size[1]);
/* read tile values */
for (j = 0; j < G.gi.map_size[0]; j++) {
for (i = 0; i < G.gi.map_size[1]; i++) {
int map_index = i + j * G.gi.map_size[1];
char c;
do {
c = getchar();
} while (!isalnum(c));
c = tolower(c);
G.gi.map[map_index] = c;
switch (c) {
case 'x':
G.gi.mapv[map_index] = 0;
G.gi.mapw[map_index] = -1;
break;
case 's':
G.gi.mapv[map_index] = 1;
G.gi.mapw[map_index] = 1;
break;
case 'a':
G.gi.mapv[map_index] = 2;
G.gi.mapw[map_index] = 4;
break;
case 'r':
G.gi.mapv[map_index] = 3;
G.gi.mapw[map_index] = 10;
break;
case 'p':
G.gi.mapv[map_index] = 4;
G.gi.mapw[map_index] = 20;
break;
}
}
}
}
void game_print(void)
{
int i, j;
printf("start (%2d,%2d)\n", G.gi.start[0], G.gi.start[1]);
printf("end (%2d,%2d)\n", G.gi.end[0], G.gi.end[1]);
printf("num prizes %2d\n", G.gi.num_prizes);
for (i = 0; i < G.gi.num_prizes; i++) {
printf("\t(%2d,%2d)\n", G.gi.prizes[i][0], G.gi.prizes[i][1]);
}
printf("map_size (%2d,%2d)\n", G.gi.map_size[0], G.gi.map_size[1]);
for (i = 0; i < G.gi.map_size[0]; i++) {
for (j = 0; j < G.gi.map_size[1]; j++) {
int map_index = i + j * G.gi.map_size[1];
printf("%c", G.gi.map[map_index]);
}
printf(" |");
for (j = 0; j < G.gi.map_size[1]; j++) {
int map_index = i + j * G.gi.map_size[1];
printf("%3d", G.gi.mapv[map_index]);
}
printf("\n");
}
}