-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmap_visualizer.cpp
More file actions
86 lines (71 loc) · 2.17 KB
/
Copy pathmap_visualizer.cpp
File metadata and controls
86 lines (71 loc) · 2.17 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
/*
map_visualizer - Visualize a map for the game minesweeper
It reads the map from a file, and prints the map (in a human-readable format)
onto the screen.
Usage: ./map_visualizer <map_file>
*/
#include <cstdio>
#include <random>
#include <chrono>
#include <cassert>
#include "lib/wrappers.h"
#define DISPLAY_THRESHOLD_N 2048
void usage(char* prog_name) {
printf("Usage: %s <path/to/map_file>\n", prog_name);
exit(0);
}
long N, K, logN;
char* is_mine;
char test_is_mine(long r, long c) {
if (r < 0 || c < 0 || r >= N || c >= N) return 0;
long index = (r<<logN) + c;
long number = index/8, offset = index%8;
return is_mine[number]>>offset&0x1;
}
int main(int argc, char* argv[]) {
if (argc != 2) {
usage(argv[0]);
}
FILE* map_file = fopen(argv[1], "r");
if (!map_file) {
unix_error("Failed to open the map file");
}
printf("File: %s\n", argv[1]);
if (fscanf(map_file, "%ld %ld\n", &N, &K) != 2) {
app_error("Failed to read N and K. Maybe the map file is broken?");
}
printf("Size(N): %ld\nNumber of mines(K): %ld\n", N, K);
logN = (long)(log2((double)N)+0.01);
if (N > DISPLAY_THRESHOLD_N) {
app_error("This map is too large.\n\
Displaying it may stuck or crash your terminal (especially when you are using a code server).\n\
If you really want to display it, please change DISPLAY_THRESHOLD_N in \
map_visualizer to a larger number, and recompile it.\n\
P.S. the default value of DISPLAY_THRESHOLD_N is 2048.");
}
Fseek(map_file, 0, SEEK_SET);
char c;
while ((c = fgetc(map_file)) != '\n');
is_mine = (char*)Malloc(N*N/8);
size_t byte_read = fread(is_mine, 1, N*N/8, map_file);
if (byte_read != N*N/8) {
app_error("Failed to read the map. Maybe the map file is broken?");
}
long mine_cnt = 0;
for (long r = 0; r < N; ++r) {
for (long c = 0; c < N; ++c) {
if (test_is_mine(r, c)) {
putchar('*');
mine_cnt += 1;
} else {
char cnt = test_is_mine(r-1, c-1) + test_is_mine(r-1, c) + test_is_mine(r-1, c+1)
+ test_is_mine(r, c-1) + test_is_mine(r, c) + test_is_mine(r, c+1)
+ test_is_mine(r+1, c-1) + test_is_mine(r+1, c) + test_is_mine(r+1, c+1);
putchar(cnt+'0');
}
}
putchar('\n');
}
assert(mine_cnt == K);
return 0;
}