Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <unistd.h>
- //#include <ostream>
- using namespace std;
- class Snake {
- public:
- int x, y;
- Snake *next;
- };
- class Game {
- private:
- int board[10][10][1];
- int szerokosc, dlugosc;
- int points;
- int direction;
- Snake head;
- public:
- Game(int szerokosc, int dlugosc) {
- this->szerokosc = szerokosc;
- this->dlugosc = dlugosc;
- this->points = 0;
- Snake *head = new Snake();
- head->x = 5;
- head->y = 5;
- }
- void pressUp() {
- this->direction = 0;
- }
- void pressDown() {
- this->direction = 1;
- }
- void pressLeft() {
- this->direction = 2;
- }
- void pressRight() {
- this->direction = 4;
- }
- void swap(int tx, int ty, Snake &tmp) {
- int x, y;
- x = tmp.x;
- y = tmp.y;
- tmp.x = tx;
- tmp.y = ty;
- }
- void chodzenie() {
- Snake tmp = head;
- switch (this->direction) {
- case 0:
- while (tmp.next != NULL) {
- int tx, ty;
- tx = tmp.x;
- ty = tmp.y;
- tmp.y++;
- tmp = *tmp.next;
- tmp.swap(tx, ty, tmp);
- }
- tmp.y++;
- break;
- case 1:
- while (tmp.next != NULL) {
- int tx, ty;
- tx = tmp.x;
- ty = tmp.y;
- tmp.y--;
- tmp = *tmp.next;
- tmp.swap(tx, ty, tmp);
- }
- tmp.y--;
- break;
- case 2:
- while (tmp.next != NULL) {
- int tx, ty;
- tx = tmp.x;
- ty = tmp.y;
- tmp.x--;
- tmp = *tmp.next;
- tmp.swap(tx, ty, tmp);
- }
- tmp.x--;
- break;
- case 4:
- while (tmp.next != NULL) {
- int tx, ty;
- tx = tmp.x;
- ty = tmp.y;
- tmp.x++;
- tmp = *tmp.next;
- tmp.swap(tx, ty, tmp);
- }
- tmp.x++;
- break;
- }
- }
- void step() {
- for (int i = 0; i < 10; i++)
- for (int j = 0; j < 10; j++)
- board[i][j][0] = 0;
- Snake tmp = head;
- while (head.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() {
- }
- void setDirection(int direction) {
- switch (direction) {
- case 0:
- pressUp();
- break;
- case 1:
- pressDown();
- break;
- case 2:
- pressLeft();
- break;
- case 4:
- pressRight();
- break;
- }
- }
- void testSnake() {
- int steps = 50;
- Game *g = new Game(10, 10);
- g->show();
- int a[] = {5, 2, 5, 5, 5, 1, 5, 5, 5, 3, 5, 5, 5, 5, 5, 5, 0, 5, 5, 5, 5, 5, 2, 5, 5, 5, 5, 5, 1, 5, 5, 5, 3, 5,
- 5, 5, 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;
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement