Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ed715c9

Browse files
author
Sonny Li
committed
Louie Kotler, Simone Stern & Chris Narducci
1 parent 62b10ad commit ed715c9

File tree

1 file changed

+341
-0
lines changed

1 file changed

+341
-0
lines changed

‎Projects/matching.c

Lines changed: 341 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,341 @@
1+
// Week 2 Project: Matching Game
2+
// Team OG Trio
3+
// Louie Kotler, Simone Stern & Chris Narducci (Columbia University)
4+
5+
// Instructor: Sonny Li
6+
7+
// gcc -lform -lncurses -Wall matching.c -o matching
8+
// ./matching
9+
10+
#include <stdio.h>
11+
#include <stdlib.h>
12+
#include <strings.h>
13+
#include <time.h>
14+
#include <ncurses.h>
15+
#include <unistd.h>
16+
#include <ctype.h>
17+
18+
void reload();
19+
int terminate();
20+
21+
// =========================================================================================================
22+
23+
int main () {
24+
25+
// ~~~~~~ Initialization: Part I ~~~~~~
26+
27+
srand(time(NULL));
28+
29+
initscr(); // creates a 'window' that can be edited. printw(), scanw(), refresh(), etc. are editing this 'window'
30+
31+
mvprintw(0, 0, "Matching Game! To play, press [Enter]\n");
32+
33+
noecho(); // the terminal doesn't echo (type) what you enter. kind of like when you input a password
34+
35+
char enter;
36+
37+
enter = getch(); // get character
38+
39+
while (enter != '\n') // if the key typed is not [enter], do nothing
40+
{
41+
refresh(); // refreshes the window. needed for ncurses function. prints won't appear unless the screen is refreshed
42+
enter = getch();
43+
}
44+
45+
move(0, 0); // move cursor to top left of window (starting point). coordinates are (y, x) instead of (x, y)
46+
clrtoeol(); // clear the line (Matching Game! blah blah blah)
47+
48+
refresh();
49+
50+
// ~~~~~~ Initialization: Part 2 ~~~~~~
51+
52+
// mvprintw(0, 0,"=============================\n");
53+
// mvprintw(1, 0," Matching Game \n");
54+
// mvprintw(2, 0,"=============================\n\n");
55+
//
56+
// refresh();
57+
58+
// ---------------------------------------------------------------------------------------------------------
59+
60+
// covered displayed
61+
62+
char array1[4][4] = {{'*', '*','*','*'}, {'*', '*','*','*'}, {'*', '*','*','*'}, {'*', '*','*','*'}};
63+
64+
int x = 0;
65+
66+
// make arr2[16] a randomized answer
67+
68+
char arr[16] = {'!', '@', '#', '$','#', '$', '%', '^','@', '^', '!', '&','=', '%', '=', '&'};
69+
70+
char arr2[16] = {'0', '0', '0', '0','0', '0', '0', '0','0', '0', '0', '0', '0', '0', '0', '0'};
71+
72+
while (x < 16) {
73+
74+
int r = rand() % 16;
75+
76+
if (arr[r] != '0') {
77+
78+
arr2[x] = arr[r];
79+
arr[r] = '0';
80+
x++;
81+
82+
}
83+
84+
}
85+
86+
// plug arr2[ 16] -> array2[4][4]
87+
88+
char array2[4][4] = {{'0', '0', '0', '0'}, {'0', '0', '0', '0'}, {'0', '0', '0', '0'}, {'0', '0', '0', '0'}};
89+
90+
int count = 0;
91+
92+
for (int y = 0; y < 4; y++) {
93+
94+
for (int z = 0; z < 4; z++) {
95+
96+
array2[y][z] = arr2[count];
97+
count++;
98+
99+
}
100+
101+
}
102+
103+
// ---------------------------------------------------------------------------------------------------------
104+
105+
int running = 1;
106+
int counterfull = 0;
107+
108+
while (running) {
109+
110+
int ent1 = 0, ent2 = 0, ent3 = 0, ent4 = 0;
111+
112+
initscr();
113+
114+
// reload();
115+
116+
mvprintw(0, 0, "=============================\n");
117+
mvprintw(1, 0, " Matching Game \n");
118+
mvprintw(2, 0, "=============================\n\n");
119+
refresh();
120+
121+
mvprintw(5, 0, " \\ 1 2 3 4 \n\n");
122+
refresh();
123+
124+
mvprintw(8, 0, " 1 %c %c %c %c", array1[0][0], array1[0][1], array1[0][2], array1[0][3]);
125+
mvprintw(11, 0, " 2 %c %c %c %c", array1[1][0], array1[1][1], array1[1][2], array1[1][3]);
126+
mvprintw(14, 0, " 3 %c %c %c %c", array1[2][0], array1[2][1], array1[2][2], array1[2][3]);
127+
mvprintw(17, 0, " 4 %c %c %c %c", array1[3][0], array1[3][1], array1[3][2], array1[3][3]);
128+
refresh();
129+
130+
// sleep(3);
131+
132+
mvprintw(20, 0,"Enter Coordinate 1 (x y): ");
133+
134+
echo();
135+
refresh();
136+
137+
scanw("%d %d", &ent1, &ent2);
138+
139+
while (ent1 < 1 || ent1 > 4 || ent2 < 1 || ent2 > 4) {
140+
141+
mvprintw(20, 0,"Enter Coordinate 1 (x y): ");
142+
143+
echo();
144+
refresh();
145+
146+
scanw("%d %d", &ent1, &ent2);
147+
148+
}
149+
150+
151+
refresh();
152+
echo();
153+
154+
refresh();
155+
156+
mvprintw(21, 0,"Enter Coordinate 2 (x y): ");
157+
158+
echo();
159+
refresh();
160+
161+
scanw("%d %d", &ent3, &ent4);
162+
163+
while (ent3 < 1 || ent3 > 4 || ent4 < 1 || ent4 > 4) {
164+
165+
mvprintw(21, 0, "Enter Coordinate 2 (x y): ");
166+
167+
echo();
168+
refresh();
169+
170+
scanw("%d %d", &ent3, &ent4);
171+
172+
}
173+
174+
175+
refresh();
176+
177+
noecho();
178+
refresh();
179+
180+
ent1 -= 1;
181+
ent2 -= 1;
182+
ent3 -= 1;
183+
ent4 -= 1;
184+
185+
memcpy(&array1[ent1][ent2], &array2[ent1][ent2], 1);
186+
memcpy(&array1[ent3][ent4], &array2[ent3][ent4], 1);
187+
188+
// sleep(3);
189+
190+
mvprintw(5, 0, " \\ 1 2 3 4 \n\n");
191+
192+
mvprintw(8, 0, " 1 %c %c %c %c", array1[0][0], array1[0][1], array1[0][2], array1[0][3]);
193+
mvprintw(11, 0, " 2 %c %c %c %c", array1[1][0], array1[1][1], array1[1][2], array1[1][3]);
194+
mvprintw(14, 0, " 3 %c %c %c %c", array1[2][0], array1[2][1], array1[2][2], array1[2][3]);
195+
mvprintw(17, 0, " 4 %c %c %c %c", array1[3][0], array1[3][1], array1[3][2], array1[3][3]);
196+
197+
echo();
198+
refresh();
199+
200+
// WIN
201+
202+
if (array1[ent1][ent2] == array1[ent3][ent4]) {
203+
204+
if (ent1 != ent3 || ent2 != ent4) {
205+
206+
running = 0;
207+
208+
mvprintw(0, 0, "=============================\n");
209+
mvprintw(1, 0, " Matching Game \n");
210+
mvprintw(2, 0, "=============================\n\n");
211+
refresh();
212+
213+
mvprintw(5, 0, " \\ 1 2 3 4 \n\n");
214+
refresh();
215+
216+
mvprintw(8, 0, " 1 %c %c %c %c", array2[0][0], array2[0][1], array2[0][2], array2[0][3]);
217+
mvprintw(11, 0, " 2 %c %c %c %c", array2[1][0], array2[1][1], array2[1][2], array2[1][3]);
218+
mvprintw(14, 0, " 3 %c %c %c %c", array2[2][0], array2[2][1], array2[2][2], array2[2][3]);
219+
mvprintw(17, 0, " 4 %c %c %c %c", array2[3][0], array2[3][1], array2[3][2], array2[3][3]);
220+
refresh();
221+
222+
mvprintw(21, 0, "You got it! Nice!\n");
223+
224+
// terminate();
225+
// endwin();
226+
227+
system("clear");
228+
229+
}
230+
231+
}
232+
233+
// here is refresh
234+
235+
array1[ent1][ent2]= '*';
236+
array1[ent3][ent4]= '*';
237+
238+
counterfull++;
239+
240+
mvprintw(23, 0, "Guesses Left: %d", 5-counterfull);
241+
242+
if (counterfull >= 5) {
243+
244+
running = 0;
245+
246+
mvprintw(0, 0, "=============================\n");
247+
mvprintw(1, 0, " Matching Game \n");
248+
mvprintw(2, 0, "=============================\n\n");
249+
refresh();
250+
251+
mvprintw(5, 0, " \\ 1 2 3 4 \n\n");
252+
refresh();
253+
254+
mvprintw(8, 0, " 1 %c %c %c %c", array2[0][0], array2[0][1], array2[0][2], array2[0][3]);
255+
mvprintw(11, 0, " 2 %c %c %c %c", array2[1][0], array2[1][1], array2[1][2], array2[1][3]);
256+
mvprintw(14, 0, " 3 %c %c %c %c", array2[2][0], array2[2][1], array2[2][2], array2[2][3]);
257+
mvprintw(17, 0, " 4 %c %c %c %c", array2[3][0], array2[3][1], array2[3][2], array2[3][3]);
258+
refresh();
259+
260+
mvprintw(19, 0, "\nYou ran out of guesses, bruh ur stupid.\n");
261+
262+
system("clear");
263+
264+
}
265+
266+
refresh();
267+
268+
reload();
269+
270+
}
271+
272+
terminate();
273+
274+
return 0;
275+
276+
}
277+
278+
// ====================================================================================================
279+
280+
void reload() {
281+
282+
char enter;
283+
284+
noecho();
285+
286+
enter = getch();
287+
288+
while (enter != '\n') {
289+
290+
refresh();
291+
enter = getch();
292+
293+
}
294+
295+
move(0, 0);
296+
297+
for (int i = 0; i < 100; i++) {
298+
299+
for (int j = 0; j < 100; j++) {
300+
301+
printw(" ");
302+
303+
}
304+
305+
printw("\n");
306+
307+
}
308+
309+
move(0, 0);
310+
311+
clrtoeol();
312+
refresh();
313+
314+
}
315+
316+
// ====================================================================================================
317+
318+
int terminate () {
319+
320+
char leave;
321+
322+
mvprintw(22, 0, "To quit, press [q]\n");
323+
324+
leave = getch();
325+
leave = tolower(leave);
326+
327+
refresh();
328+
329+
while (leave != 'q') {
330+
331+
leave = getch();
332+
leave = tolower(leave);
333+
endwin();
334+
335+
}
336+
337+
endwin();
338+
339+
return 0;
340+
341+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /