-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
188 lines (172 loc) · 3.86 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
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <winuser.h>
#define M 30
#define N 40
#define PLAYER 8
#define KEY_LEFT 75
#define KEY_RIGHT 77
int player_left, player_right;
int Game = 0;
int random()
{
return rand() % (N-PLAYER-2);
}
void game_over()
{
system("Cls");
printf("\n\n\nGame over!");
Sleep(1500);
Game = 1;
}
void bar(int field[M][N])
{
int left = random(), right = random();
while(N - 2 - (left + right) < 10)
{
right = random();
}
for (int i = 0; i < N; i++)
{
if (i < left || i > N-right)
field[1][i] = 2;
}
}
void check(int field[M][N], int c)
{
for (int i = 1; i < N-1; i++)
{
if(c+2 == M-2)
{
if(field[c+2][i] == 1 && field[c+1][i] == 2)
{
game_over();
return;
}
if(field[c+2][i] == 0 && field[c+1][i] == 2)
{
field[c+2][i] = field[c+1][i];
field[c+1][i] = 0;
}
}
else if(c+2 < M-2)
{
field[c+2][i] = field[c+1][i];
field[c+1][i] = 0;
}
else if(field[c][i] == 2 )
field[c][i] = 0;
else if(field[c+1][i] == 2 )
field[c+1][i] = 0;
}
for (int i = 1; i < N-1; i++)
{
field[M-1][i] = 2;
}
}
void play_round(int field[M][N])
{
static int c = 0, c2 = 15;
int tab[N];
if(!c || !c2)
bar(field);
check(field, c);
check(field, c2);
c++;
c2++;
c = c%(M-2);
c2 = c2%(M-2);
}
void move(int field[M][N])
{
int c = 0;
if(_kbhit()){
switch((c = _getch()))
{
case KEY_LEFT:
if(player_left > 1)
{
field[M-2][player_right] = 0;
player_left--;
player_right--;
field[M-2][player_left] = 1;
}
break;
case KEY_RIGHT:
if(player_right < N-2)
{
field[M-2][player_left] = 0;
player_left++;
player_right++;
field[M-2][player_right] = 1;
}
break;
}
}
}
void initialize(int field[M][N])
{
player_left = (N - PLAYER-1) / 2;
player_right = (N + PLAYER) / 2;
for(int i = 0; i < M; i++)
{
for(int j = 0; j < N; j++)
{
if( i == M-2 && j > player_left && j < (N + PLAYER) / 2)
field[i][j] = 1;
else if (i == 0 || j == 0 || i == M-1 || j == N-1)
field[i][j] = 2;
else
field[i][j] = 0;
}
}
}
void restart_screen()
{
HANDLE h_output = GetStdHandle(STD_OUTPUT_HANDLE);
COORD position;
position.X = 0;
position.Y = 0;
SetConsoleCursorPosition(h_output,position);
}
void print(int field[M][N])
{
for (int i = 0; i < M; i++)
{
for(int j = 0; j < N; j++)
{
if (field[i][j] == 2)
printf("X");
else if (field[i][j] == 0)
printf(" ");
else if (field[i][j] == 1)
printf("-");
}
printf("\n");
}
}
int main()
{
int field[M][N];
srand(time(0));
initialize(field);
Sleep(20);
int counter = 100;
while(Game == 0)
{
if(!counter)
{
play_round(field);
print(field);
restart_screen();
counter = 150;
Sleep(40);
}
move(field);
counter--;
}
return 0;
}