-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmatching.c
341 lines (204 loc) · 9.29 KB
/
matching.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// Week 2 Project: Matching Game
// Team OG Trio
// Louie Kotler, Simone Stern & Chris Narducci (Columbia University)
// Instructor: Sonny Li
// gcc -lform -lncurses -Wall matching.c -o matching
// ./matching
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <time.h>
#include <ncurses.h>
#include <unistd.h>
#include <ctype.h>
void reload();
int terminate();
// =========================================================================================================
int main () {
// ~~~~~~ Initialization: Part I ~~~~~~
srand(time(NULL));
initscr(); // creates a 'window' that can be edited. printw(), scanw(), refresh(), etc. are editing this 'window'
mvprintw(0, 0, "Matching Game! To play, press [Enter]\n");
noecho(); // the terminal doesn't echo (type) what you enter. kind of like when you input a password
char enter;
enter = getch(); // get character
while (enter != '\n') // if the key typed is not [enter], do nothing
{
refresh(); // refreshes the window. needed for ncurses function. prints won't appear unless the screen is refreshed
enter = getch();
}
move(0, 0); // move cursor to top left of window (starting point). coordinates are (y, x) instead of (x, y)
clrtoeol(); // clear the line (Matching Game! blah blah blah)
refresh();
// ~~~~~~ Initialization: Part 2 ~~~~~~
// mvprintw(0, 0,"=============================\n");
// mvprintw(1, 0," Matching Game \n");
// mvprintw(2, 0,"=============================\n\n");
//
// refresh();
// ---------------------------------------------------------------------------------------------------------
// covered displayed
char array1[4][4] = {{'*', '*','*','*'}, {'*', '*','*','*'}, {'*', '*','*','*'}, {'*', '*','*','*'}};
int x = 0;
// make arr2[16] a randomized answer
char arr[16] = {'!', '@', '#', '$','#', '$', '%', '^','@', '^', '!', '&','=', '%', '=', '&'};
char arr2[16] = {'0', '0', '0', '0','0', '0', '0', '0','0', '0', '0', '0', '0', '0', '0', '0'};
while (x < 16) {
int r = rand() % 16;
if (arr[r] != '0') {
arr2[x] = arr[r];
arr[r] = '0';
x++;
}
}
// plug arr2[ 16] -> array2[4][4]
char array2[4][4] = {{'0', '0', '0', '0'}, {'0', '0', '0', '0'}, {'0', '0', '0', '0'}, {'0', '0', '0', '0'}};
int count = 0;
for (int y = 0; y < 4; y++) {
for (int z = 0; z < 4; z++) {
array2[y][z] = arr2[count];
count++;
}
}
// ---------------------------------------------------------------------------------------------------------
int running = 1;
int counterfull = 0;
while (running) {
int ent1 = 0, ent2 = 0, ent3 = 0, ent4 = 0;
initscr();
// reload();
mvprintw(0, 0, "=============================\n");
mvprintw(1, 0, " Matching Game \n");
mvprintw(2, 0, "=============================\n\n");
refresh();
mvprintw(5, 0, " \\ 1 2 3 4 \n\n");
refresh();
mvprintw(8, 0, " 1 %c %c %c %c", array1[0][0], array1[0][1], array1[0][2], array1[0][3]);
mvprintw(11, 0, " 2 %c %c %c %c", array1[1][0], array1[1][1], array1[1][2], array1[1][3]);
mvprintw(14, 0, " 3 %c %c %c %c", array1[2][0], array1[2][1], array1[2][2], array1[2][3]);
mvprintw(17, 0, " 4 %c %c %c %c", array1[3][0], array1[3][1], array1[3][2], array1[3][3]);
refresh();
// sleep(3);
mvprintw(20, 0,"Enter Coordinate 1 (x y): ");
echo();
refresh();
scanw("%d %d", &ent1, &ent2);
while (ent1 < 1 || ent1 > 4 || ent2 < 1 || ent2 > 4) {
mvprintw(20, 0,"Enter Coordinate 1 (x y): ");
echo();
refresh();
scanw("%d %d", &ent1, &ent2);
}
refresh();
echo();
refresh();
mvprintw(21, 0,"Enter Coordinate 2 (x y): ");
echo();
refresh();
scanw("%d %d", &ent3, &ent4);
while (ent3 < 1 || ent3 > 4 || ent4 < 1 || ent4 > 4) {
mvprintw(21, 0, "Enter Coordinate 2 (x y): ");
echo();
refresh();
scanw("%d %d", &ent3, &ent4);
}
refresh();
noecho();
refresh();
ent1 -= 1;
ent2 -= 1;
ent3 -= 1;
ent4 -= 1;
memcpy(&array1[ent1][ent2], &array2[ent1][ent2], 1);
memcpy(&array1[ent3][ent4], &array2[ent3][ent4], 1);
// sleep(3);
mvprintw(5, 0, " \\ 1 2 3 4 \n\n");
mvprintw(8, 0, " 1 %c %c %c %c", array1[0][0], array1[0][1], array1[0][2], array1[0][3]);
mvprintw(11, 0, " 2 %c %c %c %c", array1[1][0], array1[1][1], array1[1][2], array1[1][3]);
mvprintw(14, 0, " 3 %c %c %c %c", array1[2][0], array1[2][1], array1[2][2], array1[2][3]);
mvprintw(17, 0, " 4 %c %c %c %c", array1[3][0], array1[3][1], array1[3][2], array1[3][3]);
echo();
refresh();
// WIN
if (array1[ent1][ent2] == array1[ent3][ent4]) {
if (ent1 != ent3 || ent2 != ent4) {
running = 0;
mvprintw(0, 0, "=============================\n");
mvprintw(1, 0, " Matching Game \n");
mvprintw(2, 0, "=============================\n\n");
refresh();
mvprintw(5, 0, " \\ 1 2 3 4 \n\n");
refresh();
mvprintw(8, 0, " 1 %c %c %c %c", array2[0][0], array2[0][1], array2[0][2], array2[0][3]);
mvprintw(11, 0, " 2 %c %c %c %c", array2[1][0], array2[1][1], array2[1][2], array2[1][3]);
mvprintw(14, 0, " 3 %c %c %c %c", array2[2][0], array2[2][1], array2[2][2], array2[2][3]);
mvprintw(17, 0, " 4 %c %c %c %c", array2[3][0], array2[3][1], array2[3][2], array2[3][3]);
refresh();
mvprintw(21, 0, "You got it! Nice!\n");
// terminate();
// endwin();
system("clear");
}
}
// here is refresh
array1[ent1][ent2]= '*';
array1[ent3][ent4]= '*';
counterfull++;
mvprintw(23, 0, "Guesses Left: %d", 5-counterfull);
if (counterfull >= 5) {
running = 0;
mvprintw(0, 0, "=============================\n");
mvprintw(1, 0, " Matching Game \n");
mvprintw(2, 0, "=============================\n\n");
refresh();
mvprintw(5, 0, " \\ 1 2 3 4 \n\n");
refresh();
mvprintw(8, 0, " 1 %c %c %c %c", array2[0][0], array2[0][1], array2[0][2], array2[0][3]);
mvprintw(11, 0, " 2 %c %c %c %c", array2[1][0], array2[1][1], array2[1][2], array2[1][3]);
mvprintw(14, 0, " 3 %c %c %c %c", array2[2][0], array2[2][1], array2[2][2], array2[2][3]);
mvprintw(17, 0, " 4 %c %c %c %c", array2[3][0], array2[3][1], array2[3][2], array2[3][3]);
refresh();
mvprintw(19, 0, "\nYou ran out of guesses, bruh ur stupid.\n");
system("clear");
}
refresh();
reload();
}
terminate();
return 0;
}
// ====================================================================================================
void reload() {
char enter;
noecho();
enter = getch();
while (enter != '\n') {
refresh();
enter = getch();
}
move(0, 0);
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
printw(" ");
}
printw("\n");
}
move(0, 0);
clrtoeol();
refresh();
}
// ====================================================================================================
int terminate () {
char leave;
mvprintw(22, 0, "To quit, press [q]\n");
leave = getch();
leave = tolower(leave);
refresh();
while (leave != 'q') {
leave = getch();
leave = tolower(leave);
endwin();
}
endwin();
return 0;
}