-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.cpp
77 lines (60 loc) · 2.22 KB
/
board.cpp
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
#include "board.h"
board::board(int width, int height) {
this->boardWidth = width;
this->boardHeight = height;
_snake.initializePos(width / 2, height / 2);
_feed.width = width;
_feed.height = height;
_feed.newFeed(_snake);
}
//reset the class for a new game
void board::reset() {
_snake.initializePos(boardWidth / 2, boardHeight / 2);
_feed.width = boardWidth;
_feed.height = boardHeight;
_feed.newFeed(_snake);
}
void board::play() {
playing = true;
this->reset();
while (playing) {
//if keyborad is pressed call the turn function of _snake to change direction
if (_kbhit()) _snake.turn(_getch());
system("CLS");
_snake.move();
//check if _snake crash to somewhere
if (_snake.x >= this->boardWidth || _snake.x < 0 || _snake.y >= this->boardHeight || _snake.y < 0) {
playing = false;
}
//if crashed to its tail
auto foundX = findInArr(_snake.tailX, _snake.length, _snake.x);
auto foundY = findInArr(_snake.tailY, _snake.length, _snake.y);
if(foundX != -1 && foundX == foundY){
playing = false;
}
//if _snake gets the _feed increase its length and generate a new _feed
if (_snake.x == _feed.x && _snake.y == _feed.y) {
_snake.length += 1;
_feed.newFeed(_snake);
}
//draw the game
this->draw();
//if _snake crushed somewhere end the game
if (!playing) std::cout << "Game Over \nYour Score is:" << _snake.length << "\n";
else std::cout << "Score:" << _snake.length << "\n";
sleep(500);
}
}
//draw the game to console
void board::draw() {
for (int j = -1; j < this->boardHeight + 1; j++) {
for (int i = -1; i < this->boardWidth + 1; i++) {
if (j == -1 || j == boardHeight || i == -1 || i == boardWidth) std::cout << "#";
else if (i == _snake.x && j == _snake.y) std::cout << "O";
else if (i == _feed.x && j == _feed.y) std::cout << "*";
else if (findInArr(_snake.tailX, _snake.length, i) && findInArr(_snake.tailY, _snake.length, j)) std::cout << "o";
else std::cout << " ";
}
std::cout << "\n";
}
}