-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnek.h
177 lines (156 loc) · 4.31 KB
/
snek.h
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
#pragma once
#include <vector>
#include <random>
#include <conio.h>
#include <deque>
#include "frut.h"
namespace snekGame
{
enum eDirection
{
STOP = 0,
L,
R,
U,
D
};
const unsigned dimension = 20;
std::random_device rd; //Will be used to obtain a seed for the random number engine
std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
std::uniform_int_distribution<> distrib(0, dimension - 1);
class Snek
{
private:
eDirection dir;
// used on movement, with the commented line change instructions
void updTail()
{
if (dir != STOP)
{
for (unsigned i = 1; i > posTail.size(); i--)
{
posTail.at(i) = posTail.at(i - 1);
}
}
}
bool collision()
{
if (posTail.front().second < 0 || posTail.front().second > dimension - 1 ||
posTail.front().first < 0 || posTail.front().first > dimension - 1)
{
std::cout << "Game over, man. Game over"
<< "\n";
return true;
}
for (int k = 1; k < posTail.size(); k++)
{
if (posTail.front() == posTail.at(k))
{
std::cout << "Game over, man. Game over"
<< "\n";
return true;
}
}
return false;
}
void Move(bool &stats)
{
int x = posTail.front().first;
int y = posTail.front().second;
switch (dir)
{
case STOP:
break;
case U:
if(dir != D)
y--;
break;
case L:
if(dir != R)
x--;
break;
case R:
if(dir != L)
x++;
break;
case D:
if(dir != U)
y++;
break;
}
if (dir != STOP)
{
posTail.push_front(std::make_pair(x, y));
posTail.pop_back();
}
if (collision())
stats = !stats;
}
public:
std::deque<std::pair<int, int>> posTail;
Snek()
{
posTail.push_back(std::make_pair(dimension / 2, dimension / 2));
posTail.push_back(std::make_pair(posTail.front().first, posTail.front().second + 1));
posTail.push_back(std::make_pair(posTail.front().first, posTail.front().second + 2));
dir = STOP;
}
void Input(bool &stats)
{
if (_kbhit())
{
switch (_getch())
{
case 'w':
if (dir != D)
dir = U;
break;
case 'a':
if (dir != R)
dir = L;
break;
case 'd':
if (dir != L)
dir = R;
break;
case 's':
if (dir != U)
dir = D;
break;
}
}
Move(stats);
}
};
// class Frut
// {
// public:
// std::pair<int, int> pos;
// unsigned pts;
// unsigned score;
// void refresh(std::deque<std::pair<int, int>> body)
// {
// pos.first = distrib(gen);
// pos.second = distrib(gen);
// for (std::pair<int, int> Spos: body)
// {
// if (pos == Spos)
// refresh(body);
// }
// }
// void ate(snekGame::Snek &S)
// {
// if (S.posTail.front() == pos)
// {
// score += pts;
// pts++;
// S.posTail.push_front(S.posTail.front());
// refresh(S.posTail);
// }
// }
// void showScore()
// {
// std::cout << "score: " << score << "\n";
// }
// };
} // namespace snekGame