-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
172 lines (134 loc) · 2.94 KB
/
main.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
#include "main.h"
void childHandler(int signum);
void mainAlarmHandler(int signum);
void playGame();
pid_t child[3];
int isGameEnd;
unsigned int score;
int main(void)
{
atexit(exiting);
while(1)
{
printf("\033[H\033[J");
printf("\033[%dd\033[%dG%s",1 ,1, "1. Start Game");
printf("\033[%dd\033[%dG%s",2 ,1, "2. Exit Game\n");
int input = _getch();
switch(input)
{
case '1':
playGame();
break;
case '2':
return 0;
}
}
}
void childHandler(int signum)
{
unsigned int i;
for (i=0;i<3;i++)
kill(child[i],SIGKILL);
isGameEnd = 1;
}
void mainAlarmHandler(int signum)
{
printf("\033[2d\033[60G\033[36m%d\033[0m초",score);
score+=1;
}
void playGame()
{
int fd[3][2];
int i, j;
struct sigaction sa, alarmsa;
printf("\033[H\033[J");
//pipe 생성
if (pipe(fd[0]) || pipe(fd[1]) || pipe(fd[2]))
{
perror("pipe Error");
exit(-1);
}
for (i=0; i<3; i++)
if ((child[i] = fork()) <= 0)
break;
//printf("Hi I'm %d and My child is %d (i = %d)\n", getpid(), child, i);//확인 구문
//자식은 i = 0, 1, 2 부모는 i = 3
//각 함수로 pipe 인자를 보냄
switch (i)
{
case 0:
letterGame(fd[0]);
exit(0);
case 1:
snakeGame(fd[1]);
exit(0);
case 2:
hurdleGame(fd[2]);
exit(0);
case 3:
{
struct itimerval tim;
score = 1;
showCursor(0);
sa.sa_handler = &childHandler;
sa.sa_flags = 0;
if((sigemptyset(&sa.sa_mask) == -1) || (sigaction(SIGCHLD, &sa, NULL) == -1))
perror("Failed to install SIGALRM signal handler");
alarmsa.sa_handler = &mainAlarmHandler;
alarmsa.sa_flags = 0;
if((sigemptyset(&alarmsa.sa_mask) == -1) || (sigaction(SIGALRM, &alarmsa, NULL) == -1))
perror("Failed to install SIGALRM signal handler");
tim.it_value.tv_sec = 1;
tim.it_value.tv_usec = 0;
tim.it_interval.tv_sec = 1;
tim.it_interval.tv_usec = 0;
if (setitimer(ITIMER_REAL, &tim, NULL) == -1)
{
perror("main Setitimer Error");
}
isGameEnd = 0;
// 종료 시까지 대기
while (!isGameEnd)
{
int ch = _getch();
switch (ch)
{
case 32: // Space Bar
write(fd[2][1], &ch, 1);
break;
case 27: // 상하좌우
if (_getch() == 91)
{
ch = _getch();
if ((ch >= 65) && (ch <= 68))
write(fd[1][1], &ch, 1);
}
break;
default:
// 알파벳
if (((ch >= 'A') && (ch <= 'Z')) || ((ch >= 'a') && (ch <= 'z')))
{
write(fd[0][1], &ch, 1);
}
break;
}
}
for (i=0;i<3;i++)
for (j=0;j<2;j++)
close(fd[i][j]);
tim.it_value.tv_sec = 0;
tim.it_value.tv_usec = 0;
tim.it_interval.tv_sec = 1;
tim.it_interval.tv_usec = 0;
if (setitimer(ITIMER_REAL, &tim, NULL) == -1)
{
perror("main Setitimer Error2");
}
printf("\033[H\033[J");
printf("Game Over\n\nYour Score is \033[36m%d\033[0m!!\n\nPlease Press \'Q\'\n(uppercase, not a lowercase \'q\')\n\n->", score);
while (_getch() != 'Q');
}
break;
}
return;
}