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 656d400

Browse files
author
Sonny Li
committed
asdf
1 parent a92fd05 commit 656d400

File tree

1 file changed

+354
-0
lines changed

1 file changed

+354
-0
lines changed

‎Projects/matching.c‎

Lines changed: 354 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,354 @@
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(4, 0, "\n");
122+
mvprintw(5, 0, " 1 2 3 4\n\n");
123+
refresh();
124+
125+
/* for(i = 0; i < 4; i++) {
126+
127+
mvprintw("%d", i+1);
128+
129+
for(j=0; j<4; j++) {
130+
131+
mvprintw(" %c", array1[i][j]);
132+
133+
}
134+
135+
mvprintw("\n\n");
136+
}
137+
*/
138+
139+
mvprintw(8, 0, "1 %c %c %c %c", array1[0][0], array1[0][1], array1[0][2], array1[0][3]);
140+
mvprintw(11, 0, "2 %c %c %c %c", array1[1][0], array1[1][1], array1[1][2], array1[1][3]);
141+
mvprintw(14, 0, "3 %c %c %c %c", array1[2][0], array1[2][1], array1[2][2], array1[2][3]);
142+
mvprintw(17, 0, "4 %c %c %c %c", array1[3][0], array1[3][1], array1[3][2], array1[3][3]);
143+
refresh();
144+
145+
// sleep(3);
146+
147+
mvprintw(20, 0,"Enter Coordinate 1 (x y): ");
148+
149+
echo();
150+
refresh();
151+
152+
scanw("%d %d", &ent1, &ent2);
153+
154+
while (ent1 < 1 || ent1 > 4 || ent2 < 1 || ent2 > 4) {
155+
156+
mvprintw(20, 0,"Enter Coordinate 1 (x y): ");
157+
158+
echo();
159+
refresh();
160+
161+
scanw("%d %d", &ent1, &ent2);
162+
163+
}
164+
165+
166+
refresh();
167+
echo();
168+
169+
refresh();
170+
171+
mvprintw(21, 0,"Enter Coordinate 2 (x y): ");
172+
173+
echo();
174+
refresh();
175+
176+
scanw("%d %d", &ent3, &ent4);
177+
178+
while (ent3 < 1 || ent3 > 4 || ent4 < 1 || ent4 > 4) {
179+
180+
mvprintw(21, 0, "Enter Coordinate 2 (x y): ");
181+
182+
echo();
183+
refresh();
184+
185+
scanw("%d %d", &ent3, &ent4);
186+
187+
}
188+
189+
190+
refresh();
191+
192+
noecho();
193+
refresh();
194+
195+
ent1 -= 1;
196+
ent2 -= 1;
197+
ent3 -= 1;
198+
ent4 -= 1;
199+
200+
memcpy(&array1[ent1][ent2], &array2[ent1][ent2], 1);
201+
memcpy(&array1[ent3][ent4], &array2[ent3][ent4], 1);
202+
203+
// sleep(3);
204+
205+
mvprintw(5, 0, " 1 2 3 4\n\n");
206+
207+
/*
208+
209+
for (i = 0; i < 4; i++) {
210+
211+
mvprintw("%d", i+1);
212+
213+
for (j = 0; j < 4; j++) {
214+
215+
mvprintw(" %c", array1[i][j]);
216+
217+
}
218+
219+
mvprintw("\n\n");
220+
221+
}
222+
223+
*/
224+
225+
mvprintw(8, 0, "1 %c %c %c %c", array1[0][0], array1[0][1], array1[0][2], array1[0][3]);
226+
mvprintw(11, 0, "2 %c %c %c %c", array1[1][0], array1[1][1], array1[1][2], array1[1][3]);
227+
mvprintw(14, 0, "3 %c %c %c %c", array1[2][0], array1[2][1], array1[2][2], array1[2][3]);
228+
mvprintw(17, 0, "4 %c %c %c %c", array1[3][0], array1[3][1], array1[3][2], array1[3][3]);
229+
230+
echo();
231+
refresh();
232+
233+
// WIN
234+
235+
if (array1[ent1][ent2] == array1[ent3][ent4]) {
236+
237+
if (ent1 != ent3 || ent2 != ent4) {
238+
239+
running = 0;
240+
241+
mvprintw(0, 0,"===========================\n");
242+
mvprintw(1, 0," Matching Game \n");
243+
mvprintw(2, 0,"===========================\n\n");
244+
refresh();
245+
246+
mvprintw(4, 0, "\n");
247+
mvprintw(5, 0, " 1 2 3 4\n\n");
248+
249+
mvprintw(8, 0, "1 %c %c %c %c", array1[0][0], array1[0][1], array1[0][2], array1[0][3]);
250+
mvprintw(11, 0, "2 %c %c %c %c", array1[1][0], array1[1][1], array1[1][2], array1[1][3]);
251+
mvprintw(14, 0, "3 %c %c %c %c", array1[2][0], array1[2][1], array1[2][2], array1[2][3]);
252+
mvprintw(17, 0, "4 %c %c %c %c", array1[3][0], array1[3][1], array1[3][2], array1[3][3]);
253+
254+
mvprintw(4, 0, "You got it! Nice!\n");
255+
256+
// terminate();
257+
// endwin();
258+
259+
system("clear");
260+
261+
}
262+
263+
}
264+
265+
// here is refresh
266+
267+
array1[ent1][ent2]= '*';
268+
array1[ent3][ent4]= '*';
269+
270+
counterfull++;
271+
mvprintw(3, 0, "Guesses Left: %d", 5-counterfull);
272+
273+
if (counterfull >= 5) {
274+
275+
running = 0;
276+
mvprintw(4,0,"\nYou ran out of guesses, bruh ur stuppiddddd\n");
277+
278+
system("clear");
279+
280+
}
281+
282+
refresh();
283+
284+
}
285+
286+
terminate();
287+
288+
return 0;
289+
290+
}
291+
292+
// ====================================================================================================
293+
294+
void reload() {
295+
296+
char enter;
297+
298+
noecho();
299+
300+
enter = getch();
301+
302+
while (enter != '\n')
303+
{
304+
refresh();
305+
enter = getch();
306+
}
307+
308+
move(0, 0);
309+
310+
for (int i = 0; i < 100; i++) {
311+
312+
for (int j = 0; j < 100; j++) {
313+
314+
printw(" ");
315+
316+
}
317+
318+
printw("\n");
319+
320+
}
321+
322+
move(0, 0);
323+
324+
clrtoeol();
325+
refresh();
326+
327+
}
328+
329+
// ====================================================================================================
330+
331+
int terminate () {
332+
333+
char leave;
334+
335+
mvprintw(22, 0, "Press [Q] to Quit\n");
336+
337+
leave = getch();
338+
leave = tolower(leave);
339+
340+
refresh();
341+
342+
while (leave != 'q') {
343+
344+
leave = getch();
345+
leave = tolower(leave);
346+
endwin();
347+
348+
}
349+
350+
endwin();
351+
352+
return 0;
353+
354+
}

0 commit comments

Comments
(0)

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