-
Notifications
You must be signed in to change notification settings - Fork 0
/
saper_finish_windows.c
233 lines (209 loc) · 6.05 KB
/
saper_finish_windows.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include<stdio.h>
#include<stdbool.h>
#include<time.h>
#include<conio.h>
#include<stdlib.h>
#define BOARD_WIDTH 10
#define BOARD_HEIGHT 10
#define BOMBS 10
int X_dir = 0, Y_dir = 0;
int bomb_count = 0;
bool game_over = false;
bool win = false;
bool the_end = false;
struct square {
int value;
bool isVisible;
bool isFlagged;
bool has_bomb;
} board[10][10] = {0};
void bomb_generator();
void graphics();
void value_generator();
//char getch();
void movement();
void visibility(int, int);
void results();
float best_time(float);
int main()
{
bomb_generator();
value_generator();
graphics();
clock_t start = clock();
while(!the_end){
movement();
}
system("cls");
if(win){
float win_time = (1000*(clock()-start))/CLOCKS_PER_SEC;
printf("gratulacje wygrales\nzajelo ci to: %.2f s\nnajlepszyczas to: %.2f s", win_time*0.001, best_time(win_time*0.001));
}else{
printf("niestety przegrales :(\n");
}
return 0;
}
void bomb_generator()
{
srand(time(NULL));
for(int i = 0; i < BOMBS;){
int x = rand()%BOARD_WIDTH;
int y = rand()%BOARD_HEIGHT;
if(!board[x][y].has_bomb){
board[x][y].has_bomb = true;
i++;
}
}
}
void value_generator()
{
for(int y = 0; y < BOARD_HEIGHT; y++){
for(int x = 0; x < BOARD_WIDTH; x++){
if(board[x][y].has_bomb == true){
if(x+1 < BOARD_WIDTH && y+1 < BOARD_HEIGHT && board[x+1][y+1].has_bomb == false)
board[x+1][y+1].value++;
if(x+1 < BOARD_WIDTH && board[x+1][y].has_bomb == false)
board[x+1][y].value++;
if(x+1 < BOARD_WIDTH && y-1 >= 0 && board[x+1][y-1].has_bomb == false)
board[x+1][y-1].value++;
if(x-1 >= 0 && y+1 < BOARD_HEIGHT && board[x-1][y+1].has_bomb == false)
board[x-1][y+1].value++;
if(x-1 >= 0 && board[x-1][y].has_bomb == false)
board[x-1][y].value++;
if(x-1 >= 0 && y-1 >= 0 && board[x-1][y-1].has_bomb == false)
board[x-1][y-1].value++;
if(y+1 < BOARD_HEIGHT && board[x][y+1].has_bomb == false)
board[x][y+1].value++;
if(y-1 >= 0 && board[x][y-1].has_bomb == false)
board[x][y-1].value++;
}
}
}
}
/*char getch()
{
system("/bin/stty raw");
char ret = getchar();
system("/bin/stty cooked");
return ret;
}*/
void graphics()
{
printf("\t\t\t\t\tliczba zaznaczonych min: %d\n", bomb_count);
for(int y = 0; y < BOARD_HEIGHT; y++){
for(int x = 0; x < BOARD_WIDTH; x++){
if(x == X_dir && y == Y_dir){
printf(" * ");
}else if(board[x][y].isFlagged){
printf("[!]");
}else if(board[x][y].isVisible){
if(board[x][y].value == 0)
printf(" ");
else
printf(" %d ", board[x][y].value);
}else{
printf("[ ]");
}
}
printf("\n");
}
printf("\nsterowanie: WSAD \t odkryj pole: E \t oznacz bombe: M\nwyjscie: X");
}
void movement()
{
switch(getch()){
case 'w':
if(Y_dir-1 >= 0){
Y_dir--;
}break;
case 's':
if(Y_dir+1 < BOARD_HEIGHT){
Y_dir++;
}break;
case 'a':
if(X_dir-1 >= 0){
X_dir--;
}break;
case 'd':
if(X_dir+1 < BOARD_WIDTH){
X_dir++;
}break;
case 'e':
if(board[X_dir][Y_dir].has_bomb){
game_over = true;
results();
}else{
visibility(X_dir, Y_dir);
}
break;
case 'm':
if(board[X_dir][Y_dir].isFlagged && board[X_dir][Y_dir].isVisible == false){
board[X_dir][Y_dir].isFlagged = false;
if(bomb_count > 0)
bomb_count--;
}else if(board[X_dir][Y_dir].isFlagged == false && board[X_dir][Y_dir].isVisible == false){
if(bomb_count < BOMBS){
board[X_dir][Y_dir].isFlagged = true;
bomb_count++;
results();
}
}
break;
case 'x':
game_over = true;
results();
break;
}
system("cls");
graphics();
}
void visibility(int x, int y)
{
if(x<0 || x>9) return;
if(y<0 || y>9) return;
if(board[x][y].isVisible == true) return;
if(board[x][y].has_bomb == false && board[x][y].value != 9 && board[x][y].isVisible == false)
board[x][y].isVisible = true;
if (board[x][y].value != 0) return;
visibility(x-1,y-1);
visibility(x-1,y);
visibility(x-1,y+1);
visibility(x+1,y-1);
visibility(x+1,y);
visibility(x+1,y+1);
visibility(x,y-1);
visibility(x,y);
visibility(x,y+1);
}
void results()
{
if(bomb_count == BOMBS){
for(int y = 0; y < BOARD_HEIGHT; y++){
for(int x = 0; x < BOARD_WIDTH; x++){
if(board[x][y].isFlagged != board[x][y].has_bomb){
game_over = true;
}else{
win = true;
}
}
}
}
if(win || game_over){
the_end = true;
}
}
float best_time(float current_time)
{
FILE *f;
float file_time;
f = fopen("high_score.txt", "r+");
fscanf(f, "%f", &file_time);
printf("%.2f\n", file_time);
if(current_time < file_time){
fseek(f, 0, SEEK_SET);
fprintf(f, "%f", current_time);
file_time = current_time;
}
fclose(f);
return file_time;
}