-
Notifications
You must be signed in to change notification settings - Fork 0
/
action_game.c
105 lines (96 loc) · 2.3 KB
/
action_game.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
/*
** action_game.c for tetris in /home/rectoria/delivery/Projets/PSU_2016_tetris
**
** Made by Bastien
** Login <[email protected]>
**
** Started on Wed Mar 1 11:06:23 2017 Bastien
** Last update Fri Mar 17 13:35:22 2017 Thibaut Cornolti
*/
#include <unistd.h>
#include "tetris.h"
int can_dash(t_pos *pos, char **board, int d)
{
int i;
int j;
i = -1;
while (pos->map[++i])
{
j = -1;
while (pos->map[i][++j])
if (board[pos->y + i][pos->x + j + d] > 0 && pos->map[i][j] > 0)
return (0);
}
return (1);
}
static void set_can_rotate(t_shapes *shapes, t_pos *pos)
{
pos->map = (pos->orient == 0) ? shapes->map_right : pos->map;
pos->map = (pos->orient == 1) ? shapes->map_down : pos->map;
pos->map = (pos->orient == 2) ? shapes->map_left : pos->map;
pos->map = (pos->orient == 3) ? shapes->map : pos->map;
}
void can_rotate(t_shapes *shapes, t_pos *pos, char **board)
{
char **tab;
int i;
int j;
tab = pos->map;
set_can_rotate(shapes, pos);
i = -1;
if ((my_strlen(pos->map[0]) + pos->x > my_strlen(board[0]))
|| (my_tablen(pos->map) + pos->y > my_tablen(board)))
{
pos->map = tab;
return ;
}
while (pos->map[++i])
{
j = -1;
while (pos->map[i][++j])
if (board[pos->y + i][pos->x + j] > 0 && pos->map[i][j] > 0)
{
pos->map = tab;
return ;
}
}
pos->orient = ((pos->orient + 1) % 4 == 0) ? 0 : pos->orient + 1;
}
int apply_action(int action, char **board,
t_pos *pos, t_shapes *shapes)
{
if (action == 1 && pos->x > 0 && can_dash(pos, board, -1))
pos->x -= 1;
else if (action == 2 &&
pos->x + my_strlen(pos->map[0]) < my_strlen(board[0])
&& can_dash(pos, board, 1))
pos->x += 1;
else if (action == 3)
can_rotate(&shapes[pos->index], pos, board);
else if (action == 4)
falling_shapes(board, pos);
else if (action == 5)
return (1);
return (0);
}
int get_action(t_pars *p)
{
char buff[201];
int n;
if ((n = read(0, buff, 199)) <= 0)
return (0);
buff[n] = 0;
if (my_strstr(buff, p->kl))
return (1);
if (my_strstr(buff, p->kr))
return (2);
if (my_strstr(buff, p->kt))
return (3);
if (my_strstr(buff, p->kd))
return (4);
if (my_strstr(buff, p->kq))
return (5);
if (my_strstr(buff, p->kp))
return (6);
return (0);
}