Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <unistd.h>
- using namespace std;
- class Snake {
- public:
- int x, y;
- Snake *next;
- Snake(int x, int y) {
- this->x = x;
- this->y = y;
- this->next = NULL;
- }
- static void swap(int tx, int ty, Snake *tmp) {
- int x, y;
- x = tmp->x;
- y = tmp->y;
- tmp->x = tx;
- tmp->y = ty;
- }
- };
- class Game {
- private:
- int board[10][10][1];
- int szerokosc, dlugosc;
- int points;
- int direction;
- Snake *head;
- int papuX, papuY;
- public:
- Game(int szerokosc, int dlugosc) {
- this->szerokosc = szerokosc;
- this->dlugosc = dlugosc;
- this->points = 0;
- head = new Snake(5, 5);
- head->x = 5;
- head->y = 5;
- this->direction = 1;
- newPapu();
- }
- void pressUp() {
- this->direction = 0;
- }
- void pressDown() {
- this->direction = 1;
- }
- void pressLeft() {
- this->direction = 2;
- }
- void pressRight() {
- this->direction = 4;
- }
- bool sprawdz() {
- if (head->x == szerokosc-1 && direction == 4)return false;
- if (head->x == 0 && direction == 2)return false;
- if (head->y == dlugosc-1 && direction == 0)return false;
- if (head->y == 0 && direction == 1)return false;
- return true;
- }
- void chodzenie() {
- Snake tmp = *head;
- switch (this->direction) {
- case 0:
- while (tmp.next != NULL) {
- int tx, ty;
- tx = tmp.x;
- ty = tmp.y;
- if (sprawdz()) {
- tmp.y++;
- tmp = *tmp.next;
- Snake::swap(tx, ty, &tmp);
- }
- }
- if (sprawdz())tmp.y++;
- *head = tmp;
- break;
- case 1:
- while (tmp.next != NULL) {
- int tx, ty;
- tx = tmp.x;
- ty = tmp.y;
- if (sprawdz()) {
- tmp.y--;
- tmp = *tmp.next;
- Snake::swap(tx, ty, &tmp);
- }
- }
- if (sprawdz())tmp.y--;
- *head = tmp;
- break;
- case 2:
- while (tmp.next != NULL) {
- int tx, ty;
- tx = tmp.x;
- ty = tmp.y;
- if (sprawdz()) {
- tmp.x--;
- tmp = *tmp.next;
- Snake::swap(tx, ty, &tmp);
- }
- }
- if (sprawdz())tmp.x--;
- *head = tmp;
- break;
- case 4:
- while (tmp.next != NULL) {
- int tx, ty;
- tx = tmp.x;
- ty = tmp.y;
- if (sprawdz()) {
- tmp.x++;
- tmp = *tmp.next;
- Snake::swap(tx, ty, &tmp);
- }
- }
- if (sprawdz())tmp.x++;
- *head = tmp;
- break;
- }
- if (this->head->x == papuX && this->head->y == papuY) {
- this->points++;
- Snake *nowy = new Snake(papuX, papuY);
- switch (direction) {
- case 0:
- nowy->y--;
- break;
- case 1:
- nowy->y++;
- break;
- case 2:
- nowy->x++;
- break;
- case 4:
- nowy->x--;
- break;
- }
- head->next = nowy;
- newPapu();
- }
- }
- void newPapu() {
- int tx, ty;
- tx = papuX;
- ty = papuY;
- papuX = (tx + 4) % szerokosc;
- papuY = (ty + 4) % dlugosc;
- }
- void step() {
- for (int i = 0; i < szerokosc; i++)
- for (int j = 0; j < dlugosc; j++)
- board[i][j][0] = 0;
- chodzenie();
- Snake tmp = *head;
- while (tmp.next != NULL) {
- board[tmp.x][tmp.y][0] = 1;
- tmp = *tmp.next;
- }
- board[tmp.x][tmp.y][0] = 1;
- }
- int getPoints() {
- return this->points;
- }
- void show() {
- for (int i = 0; i < szerokosc; i++) {
- for (int j = 0; j < dlugosc; j++) {
- if (i == papuX && j == papuY)
- cout << "*";
- else if (board[i][j][0] == 0)
- cout << "-";
- else cout << "X";
- }
- cout << endl;
- }
- }
- void setDirection(int direction) {
- if (direction == 0 || direction == 1 || direction == 2 || direction == 4)
- switch (direction) {
- case 0:
- pressUp();
- break;
- case 1:
- pressDown();
- break;
- case 2:
- pressLeft();
- break;
- case 4:
- pressRight();
- break;
- }
- }
- };
- void testSnake() {
- int steps = 500;
- Game *g = new Game(10, 10);
- g->show();
- int a[] = {4, 2, 5, 5, 5, 1, 5, 0, 5, 3, 4, 5, 0, 5, 5, 5, 0, 0, 4, 5, 5, 5, 2, 5, 0, 5, 5, 5, 1, 5, 5, 5, 3, 4,
- 5, 4, 0};
- for (int i = 0; i < steps; i++) {
- usleep(500000);
- int r = a[i % (sizeof(a) / sizeof(*a))];
- system("clear");
- cout << "Punkty:" << g->getPoints() << endl;
- if (r < 4) {
- g->setDirection(r);
- }
- g->step();
- g->show();
- cout << flush;
- }
- }
- int main() {
- testSnake();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement