-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnake.cpp
More file actions
115 lines (97 loc) · 2.61 KB
/
Snake.cpp
File metadata and controls
115 lines (97 loc) · 2.61 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
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <iomanip>
int rows = 5;
int columns = 7;
std::vector<char> tab(rows * columns, '.');
std::vector<std::pair<int, int>> snake;
std::pair<int, int> apple;
void init_snake() {
snake.push_back({2, 3});
snake.push_back({2, 4});
snake.push_back({2, 5});
}
void reset_board() {
std::fill(tab.begin(), tab.end(), '.');
}
void add_snake_to_board() {
tab[snake[0].first * columns + snake[0].second] = 'O';
for (size_t i = 1; i < snake.size(); i++) {
tab[snake[i].first * columns + snake[i].second] = 'o';
}
}
void generate_apple() {
apple = {3, 4};
}
void add_apple_to_board() {
tab[apple.first * columns + apple.second] = '@';
}
void draw_board() {
for (int r = 0; r < rows; r++) {
for (int c = 0; c < columns; c++) {
std::cout << tab[r * columns + c];
}
std::cout << std::endl;
}
}
void print_message(const std::string &msg) {
std::cout << msg;
}
void move_snake(int dr, int dc) {
int new_r = snake[0].first + dr;
int new_c = snake[0].second + dc;
if (new_r < 0 || new_r >= rows || new_c < 0 || new_c >= columns) {
print_message("Game Over!\n");
exit(2);
}
for (const auto &segment : snake) {
if (segment.first == new_r && segment.second == new_c) {
print_message("Game Over!\n");
exit(2);
}
}
snake.insert(snake.begin(), {new_r, new_c});
if (new_r == apple.first && new_c == apple.second) {
generate_apple();
} else {
snake.pop_back();
}
}
void play_game() {
char key;
while (true) {
reset_board();
add_snake_to_board();
add_apple_to_board();
draw_board();
print_message("what do you want to do ('q' to quit):");
std::cin >> key;
if (key == 'q') {
print_message("bye bye !\n");
exit(0);
} else if (key == 'i') {
move_snake(-1, 0);
} else if (key == 'k') {
move_snake(1, 0);
} else if (key == 'j') {
move_snake(0, -1);
} else if (key == 'l') {
move_snake(0, 1);
}
}
}
int main() {
init_snake();
generate_apple();
play_game();
std::vector<std::string> letters;
letters.push_back("abc");
std::string s{"def"};
letters.push_back(std::move(s));
std::cout << "std::vector letters holds: ";
for (auto&& e : letters)
std::cout << std::quoted(e) << ' ';
std::cout << "\nMoved-from string s holds: " << std::quoted(s) << '\n';
}