-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathsnake.cpp
More file actions
153 lines (142 loc) · 3.49 KB
/
Copy pathsnake.cpp
File metadata and controls
153 lines (142 loc) · 3.49 KB
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
#include "snake.h"
#include <pthread.h>
#include <iostream>
#include <vector>
#include <utility>
#include "snake_map.h"
#include "macros.h"
using namespace std;
void *input_thread_work(void *arg)
{
struct Snake *snake = (struct Snake *)arg;
while (true)
{
enum Direction direction = get_input();
snake->update_next_direction(direction);
}
}
Snake::Snake(void)
{
direction = East;
next_direction = direction;
food_eaten = false;
is_dead = false;
length = INITIAL_SNAKE_LENGTH;
clear_snake_world();
initialize_snake();
sem_init(&snake_sema, 0, 1);
pthread_create(&input_thread, NULL, input_thread_work, this);
}
void Snake::update_direction(enum Direction direction)
{
sem_wait(&this->snake_sema);
switch (direction)
{
case West:
if (this->direction != East)
{
this->direction = direction;
}
break;
case North:
if (this->direction != South)
{
this->direction = direction;
}
break;
case East:
if (this->direction != West)
{
this->direction = direction;
}
break;
case South:
if (this->direction != North)
{
this->direction = direction;
}
break;
}
sem_post(&this->snake_sema);
}
void Snake::update_next_direction(enum Direction direction)
{
this->next_direction = direction;
}
enum Direction Snake::get_direction(void)
{
enum Direction result = East;
sem_wait(&this->snake_sema);
result = this->direction;
sem_post(&this->snake_sema);
return result;
}
void Snake::validate_direction(void)
{
if (next_direction != Error)
{
update_direction(next_direction);
}
}
void Snake::update_movement(void)
{
pair<int, int> movement_part;
enum Direction direction = get_direction();
switch (direction)
{
case West:
movement_part = make_pair(snake_head.first, snake_head.second - 1);
break;
case North:
movement_part = make_pair(snake_head.first - 1, snake_head.second);
break;
case East:
movement_part = make_pair(snake_head.first, snake_head.second + 1);
break;
case South:
movement_part = make_pair(snake_head.first + 1, snake_head.second);
break;
}
snake_head = movement_part;
snake_parts.push_back(movement_part);
food_eaten = snake_head.first == snake_food.first && snake_head.second == snake_food.second;
if (food_eaten)
{
length++;
}
else
{
pair<int, int> tail = snake_parts.front();
snake_world_array[tail.first][tail.second]--;
snake_parts.erase(snake_parts.begin());
}
int head_value = ++snake_world_array[snake_head.first][snake_head.second];
if (head_value > 1)
{
is_dead = true;
}
}
void Snake::set_snake_food(pair<int, int> snake_food)
{
this->snake_food = snake_food;
}
void Snake::clear_snake_world(void)
{
for (int i = 0; i < MAP_HEIGHT; i++)
{
for (int j = 0; j < MAP_WIDTH; j++)
{
snake_world_array[i][j] = 0;
}
}
}
void Snake::initialize_snake(void)
{
for (int i = 0; i < INITIAL_SNAKE_LENGTH; i++)
{
pair<int, int> snake_part = make_pair(MAP_HEIGHT / 2, MAP_WIDTH / 2 - (INITIAL_SNAKE_LENGTH / 2) + i);
snake_parts.push_back(snake_part);
snake_world_array[snake_part.first][snake_part.second] = 1;
}
snake_head = snake_parts[snake_parts.size() - 1];
}