Fork Copy #include #include #include #include #include using namespace std; enum Direction { STOP = 0, LEFT, RIGHT, UP, DOWN }; class Point { public: int x, y; Point(int x = 0, int y = 0): x(x), y(y) {} }; class Apple { private: Point point; char display; public: Apple() { display = '@'; setPoint(Point(rand() % 18 + 1, rand() % 18 + 1)); } bool checkEat(Point head) { return head.x == point.x && head.y == point.y; } void setPoint(Point p) { point = p; } Point getPoint() { return point; } char getDisplay() { return display; } }; class Snake { private: vector body; Direction dir; public: Snake() { body.push_back(Point(10, 10)); // đầu o body.push_back(Point(9, 10)); // thân # body.push_back(Point(8, 10)); // đuôi # dir = STOP; } void setDirection(Direction d) { dir = d; } Direction getDirection() { return dir; } void move() { if (dir == STOP) return; Point next = body[0]; switch(dir) { case LEFT: next.x--; break; case RIGHT: next.x++; break; case UP: next.y--; break; case DOWN: next.y++; break; default: break; } // Dịch chuyển thân từ sau -> trước for (int i = body.size() - 1; i > 0; i--) { body[i] = body[i - 1]; } body[0] = next; } void upgrade() { body.push_back(body.back()); // tăng độ dài rắn thêm 1 } Point getHead() { return body[0]; } vector getBody() { return body; } bool checkDie(int n) { Point head = body[0]; if (head.x <= 0 || head.x >= n - 1 || head.y <= 0 || head.y >= n - 1) return true; for (int i = 1; i < body.size(); i++) { if (head.x == body[i].x && head.y == body[i].y) return true; } return false; } }; const int n = 20; int score = 0; void gotoxy(int x, int y) { static HANDLE h = NULL; if (!h) h = GetStdHandle(STD_OUTPUT_HANDLE); COORD c = { SHORT(x), SHORT(y) }; SetConsoleCursorPosition(h, c); } void Draw(Snake& snake, Apple& apple) { system("CLS"); for (int i = 0; i < n; i++) cout << "-"; cout << endl; for (int y = 1; y < n - 1; y++) { for (int x = 0; x < n; x++) { if (x == 0 || x == n - 1) cout << "|"; else { Point p(x, y); bool printed = false; // Vẽ Apple if (p.x == apple.getPoint().x && p.y == apple.getPoint().y) { cout << apple.getDisplay(); printed = true; } else { const vector& body = snake.getBody(); for (int i = 0; i < body.size(); i++) { if (p.x == body[i].x && p.y == body[i].y) { if (i == 0) cout << 'o'; // đầu else if (i == body.size() - 1) cout << 'O'; // đuôi else cout << '#'; // thân printed = true; break; } } } if (!printed) cout << " "; } } cout << endl; } for (int i = 0; i < n; i++) cout << "-"; cout << endl; cout << "Score: " << score << endl; } void WaitKey(Snake& snake) { if (_kbhit()) { switch (_getch()) { case 'a': snake.setDirection(LEFT); break; case 'd': snake.setDirection(RIGHT); break; case 'w': snake.setDirection(UP); break; case 's': snake.setDirection(DOWN); break; } } } int main() { srand(time(0)); Snake snake; Apple apple; while (true) { WaitKey(snake); snake.move(); if (apple.checkEat(snake.getHead())) { score++; snake.upgrade(); // Sinh lại apple tại vị trí mới không trùng thân Point p; do { p = Point(rand() % (n - 2) + 1, rand() % (n - 2) + 1); bool conflict = false; for (auto s : snake.getBody()) if (s.x == p.x && s.y == p.y) { conflict = true; break; } if (!conflict) break; } while (true); apple.setPoint(p); } if (snake.checkDie(n)) { system("CLS"); cout << "Game Over!" << endl; cout << "Final Score: " << score << endl; break; } Draw(snake, apple); Sleep(150); } return 0; }