-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.c
169 lines (159 loc) · 3.71 KB
/
engine.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
#include "engine.h"
#include "list_funcs.h"
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
void show(char grid[][W + 3]) {
int i, j;
for (i = 0; i < H + 2; i++) {
for (j = 0; j < W + 2; j++) {
// printf("%c", grid[i][j]);
switch (grid[i][j]) {
case ' ':
printf(" ");
break;
case '#':
printf(GREEN "#" RESET);
break;
case '?':
printf(CYAN "?" RESET);
break;
default:
printf(RED "%c" RESET, grid[i][j]);
}
}
printf("\n");
}
}
void create_grid(char grid[][W + 1 + 2]) {
int i, j;
printf(CLEAR);
// Initialize grid
for (i = 0; i < H + 2; i++) {
for (j = 0; j < W + 2; j++) {
grid[i][j] = ' ';
}
}
for (i = 0; i < W + 2; i++) {
grid[0][i] = '#';
grid[H + 1][i] = '#';
}
for (j = 0; j < H + 2; j++) {
grid[j][0] = '#';
grid[j][W + 1] = '#';
grid[j][W + 2] = '\0';
}
}
void initialize_player(char grid[][W + 1 + 2], int *x, int *y, vec *vtmp,
Ptr_node *player, int *orientation, vec *point,char* input) {
srand(time(NULL));
*x = 10 + (rand() % (W - 20));
*y = 5 + (rand() % (H - 10));
vtmp->x = *x;
vtmp->y = *y;
*player = inserisciInTesta(*player, *vtmp);
*orientation = rand() % 4;
if (*orientation == UP) {
grid[*y][*x] = '@';
grid[*y + 1][*x] = '*';
vtmp->y = *y + 1;
*input = 'w';
} else if (*orientation == RIGHT) {
grid[*y][*x] = '@';
grid[*y][*x - 1] = '*';
vtmp->x = *x - 1;
*input = 'd';
} else if (*orientation == DOWN) {
grid[*y][*x] = '@';
grid[*y - 1][*x] = '*';
vtmp->y = *y - 1;
*input = 's';
} else {
grid[*y][*x] = '@';
grid[*y][*x + 1] = '*';
vtmp->x = *x + 1;
*input = 'a';
}
*player = inserisciInTesta(*player, *vtmp);
// Initialize point
point->x = 1 + rand() % W;
point->y = 1 + rand() % H;
grid[point->y][point->x] = '?';
}
void show_options(char grid[][W + 1 + 2], int *len) {
show(grid);
printf("Quit: q\t: w\t: d\t: s\t: a\n\n");
printf("Your score is: %d\n\n", *len - 2);
}
int move_player(Ptr_node *player, char *input, int *orientation, vec *point,
int *len, vec *vtmp) {
// Move player
// First the body
Ptr_node tmp = *player;
while (tmp->next) {
tmp->pos.x = tmp->next->pos.x;
tmp->pos.y = tmp->next->pos.y;
tmp = tmp->next;
}
// Then the head
// TODO the player can go inside itself, left then right
switch (*input) {
case 'q':
printf("\nYou quit the game, See you next time ( ͡ᵔ ͜ʖ ͡ᵔ ) \n");
return 0;
case 'w':
*orientation = UP;
break;
case 'd':
*orientation = RIGHT;
break;
case 's':
*orientation = DOWN;
break;
case 'a':
*orientation = LEFT;
break;
}
switch (*orientation) {
case UP:
tmp->pos.y -= 1;
break;
case RIGHT:
tmp->pos.x += 1;
break;
case DOWN:
tmp->pos.y += 1;
break;
case LEFT:
tmp->pos.x -= 1;
break;
}
if (tmp->pos.x == point->x && tmp->pos.y == point->y) {
point->x = 1 + rand() % W; // Spawn new point
point->y = 1 + rand() % H;
*len += 1; // Points + 1
vtmp->x = (*player)->pos.x;
vtmp->y = (*player)->pos.y;
*player = inserisciInTesta(*player, *vtmp);
}
return 1;
}
void* get_input(void * args){
use_args *actual_args = args;
*(actual_args->my_char) = (char)getchar();
printf("\nGot char\n");
*(actual_args->got_char) = 1;
free(actual_args);
}
void set_input(int* got_input,char* input,pthread_t *t1){
if(*got_input){
*got_input = 0;
use_args *args = malloc(sizeof *args);
args->my_char = input;
args->got_char = got_input;
if(pthread_create(t1,NULL,&get_input,args) != 0){
printf("Err creazione thread\n");
exit(1);
}
}
}