-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
194 lines (189 loc) · 5.29 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <locale.h>
#include <unistd.h>
#include <ncurses.h>
#include <string.h>
#include "tetris.h"
#include "game.h"
#include "room.h"
struct shape SHAPES[SIZE_SHAPE] = {
{
.name='O',
.color=COLOR_YELLOW,
.structure=
{{'\0','\0','\0','\0'},
{'\0','O','O','\0'},
{'\0','O','O','\0'},
{'\0','\0','\0','\0'}},
.init_position=1
},
{
.name='L',
.color=COLOR_ORANGE,
.structure=
{{'\0','L','\0','\0'},
{'\0','L','\0','\0'},
{'\0','L','L','\0'},
{'\0','\0','\0','\0'}},
.init_position=1
},
{
.name='J',
.color=COLOR_BLUE,
.structure=
{{'\0','J','\0','\0'},
{'\0','J','\0','\0'},
{'J','J','\0','\0'},
{'\0','\0','\0','\0'}},
.init_position=0
},
{
.name='I',
.color=COLOR_CYAN,
.structure=
{{'\0','\0','I','\0'},
{'\0','\0','I','\0'},
{'\0','\0','I','\0'},
{'\0','\0','I','\0'}},
.init_position=2
},
{
.name='S',
.color=COLOR_GREEN,
.structure=
{{'\0','\0','\0','\0'},
{'\0','S','S','\0'},
{'S','S','\0','\0'},
{'\0','\0','\0','\0'}},
.init_position=0
},
{
.name='Z',
.color=COLOR_RED,
.structure=
{{'\0','\0','\0','\0'},
{'Z','Z','\0','\0'},
{'\0','Z','Z', '\0'},
{'\0','\0','\0','\0'}},
.init_position=0
},
{
.name='T',
.color=COLOR_MAGENTA,
.structure=
{{'\0','\0','\0','\0'},
{'T','T','T','\0'},
{'\0','T','\0','\0'},
{'\0','\0','\0','\0'}},
.init_position=0
}
};
int ORIGIN_RULES[SIZE_SHAPE][4][2] = {
{{-1, -1}, {-1, -1}, {-1, -1}, {-1, -1}}, // O_SHAPE
{{0, -1}, {-1, -1}, {-1, -1}, {-1, -2}}, // L_SHAPE
{{0, -1}, {0, -1}, {-1, -2}, {-2, 0}}, // J_SHAPE
{{0, -2}, {-2, 0}, {0, -1}, {-1, 0}}, // I_SHAPE
{{-1, -1}, {0, -1}, {-1, -2}, {-1, -1}}, // S_SHAPE
{{-1, 0}, {0, -2}, {-1, -1}, {-1, -2}}, // Z_SHAPE
{{-1, 0}, {0, -2}, {-1, -2}, {-1, -1}}, // T_SHAPE
};
struct shape next_shape;
char board[ROW_GRID][COL_GRID] = {};
struct c_shape current_cshape;
int losed = FALSE;
int score = 0;
int level = 1;
int color = -1; // randomly
int rgb = FALSE;
WINDOW* game_win, *preview_shape_win, *score_win, *hint_win, *prompt_win;
int socket_fd;
int client_fd;
int turn = SINGLE;
int player = SERVER_SIDE;
char HELP_TEXT[] = "Usage: ./tetris [options...]\n"
" -h, --help show this help text.\n"
" -l, --level set game level, between [1-20], default 1.\n"
" -b, --background set background color of game, default \"randomly\".\n"
" -R, --rgb apply rgb effect to game.\n"
" -c, --create-room <port> creating room for two-player mode.\n"
" -j, --join-room <ip> <port> Join to room.\n\n"
"list of avaiable colors:\n"
" - yellow\n - orange\n - blue\n - cyan\n - green\n - red\n - magenta\n\n";
int main(int argc, char** argv) {
for (argc--,argv++; argc > 0; argc--,argv++) {
if (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-h") == 0) {
printf("%s", HELP_TEXT);
exit(1);
} else if (strcmp(*argv, "--rgb") == 0 || strcmp(*argv, "-R") == 0) {
rgb = TRUE;
} else if (strcmp(*argv, "--level") == 0 || strcmp(*argv, "-l") == 0) {
if (*(argv+1) != NULL && atoi(*(argv+1)) > 0 && atoi(*(argv+1)) < 21) {
level = atoi(*(argv+1));
argv++; argc--;
} else {
printf("[-] ERROR: level between [1-20] must be provide!\n");
exit(1);
}
} else if (strcmp(*argv, "-b") == 0 || strcmp(*argv, "--background") == 0) {
if (*(argv+1) != NULL && (strcasecmp(*(argv+1), "YELLOW") == 0))
color = 1;
else if (*(argv+1) != NULL && (strcasecmp(*(argv+1), "ORANGE") == 0))
color = 2;
else if (*(argv+1) != NULL && (strcasecmp(*(argv+1), "BLUE") == 0))
color = 3;
else if (*(argv+1) != NULL && (strcasecmp(*(argv+1), "CYAN") == 0))
color = 4;
else if (*(argv+1) != NULL && (strcasecmp(*(argv+1), "GREEN") == 0))
color = 5;
else if (*(argv+1) != NULL && (strcasecmp(*(argv+1), "RED") == 0))
color = 6;
else if (*(argv+1) != NULL && (strcasecmp(*(argv+1), "MAGENTA") == 0))
color = 7;
else {
printf("[-] ERROR: invalid '%s' color given!, -h, --help: more info\n", *(argv+1));
exit(1);
}
argv++; argc--;
} else if (strcmp(*argv, "-c") == 0 || strcmp(*argv, "--create-room") == 0) {
if (*(argv+1) != NULL && atoi(*(argv+1)) > 0 && atoi(*(argv+1)) <= 65535) {
create_room(atoi(*(argv+1)));
argv++;argc--;
turn = SERVER_SIDE;
player = SERVER_SIDE;
send_move(level);
} else {
if (*(argv+1) == NULL)
printf("[-] ERROR: specify port for creating room\n");
else
printf("[-] ERROR: invalid port given '%s' please enter between [0-65535]\n", *(argv+1));
exit(1);
}
}
else if (strcmp(*argv, "-j") == 0 || strcmp(*argv, "--join-room") == 0) {
if (*(argv+1) != NULL && *(argv+2) != NULL &&
atoi(*(argv+2)) > 0 && atoi(*(argv+2)) <= 65535) {
join_room(*(argv+1), atoi(*(argv+2)));
argv += 2;
argc -= 2;
turn = CLIENT_SIDE;
player = CLIENT_SIDE;
level = read_move();
} else {
if (*(argv+1) == NULL || *(argv+2) == NULL)
printf("[-] ERROR: ip and port required\n");
else
printf("[-] ERROR: invalid port given '%s' please enter between [0-65535]\n", *(argv+1));
exit(1);
}
} else {
printf("[-] ERROR: invalid options '%s'\n", *argv);
exit(1);
}
}
srand(time(NULL));
setlocale(LC_ALL, "");
init_windows();
loop();
}