Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <conio.h>
- #include <iostream>
- #include <locale.h>
- #include <time.h>
- #include <chrono>
- #include <thread>
- #include <vector>
- #include <iomanip>
- #include <array>
- using std::cout; using std::cin; using std::endl; using std::string; using std::vector;
- const int MAX_SIZE = 20, MAX_DIF = 3;
- int tabuleiro[MAX_SIZE][MAX_SIZE];
- int x, y, moveDirection, snakeLength, newx, newy, dificulty, score, timeri, deltat;
- int chooseDimension(), chooseDificulty();
- void chooseHorizontal(), chooseVertical(), startGame(), testHit(), theGame(), placeFood(), displayBoard(), moveSnake(), createNewPiece(), correctPlace(), displayHorizontalBorder(), addScore(), setDificulty(), displayScore(), resetArray();
- bool error = false, food = false, lose = false, ate;
- vector<int> xsnake, ysnake;
- int main()
- {
- char op;
- setlocale(LC_ALL, "");
- srand(time(NULL));
- do
- {
- system("CLS");
- cout << "Bem-vindo ao Snake!" << endl;
- cout << "Escolha as dimensões do tabuleiro:" << endl;
- chooseHorizontal();
- chooseVertical();
- setDificulty();
- startGame();
- theGame();
- deltat = time(NULL) - timeri;
- setlocale(LC_ALL, "");
- cout << "\nPerdeste! Aguentaste-te durante " << deltat << " segundos!\n";
- cout << "Queres começar de novo? Pressiona Y para jogar novamente. Tecla N para fechar.\n";
- std::this_thread::sleep_for(std::chrono::milliseconds(500));
- do
- {
- op = _getch();
- } while (!(op == 'Y' || op == 'y' || op == 'N' || op == 'n'));
- } while (op == 'Y' || op == 'y');
- return 0;
- }
- void setDificulty()
- {
- while (true)
- {
- cout << "Dificuldade? (Maximo: " << MAX_DIF << "): ";
- int v;
- v = chooseDificulty();
- if (!error)
- {
- dificulty = v;
- break;
- }
- }
- return;
- }
- int chooseDificulty()
- {
- int v = 0;
- cin >> v;
- cin.ignore(100, '\n');
- if (v <= 0)
- {
- cout << "Tem de escolher uma dificuldade positiva\n";
- error = true;
- }
- else if (v > MAX_DIF)
- {
- cout << "Usou um valor demasiado elevado.\n";
- error = true;
- }
- else error = false;
- return v;
- }
- int chooseDimension()
- {
- int v = 0;
- cin >> v;
- cin.ignore(100, '\n');
- if (v <= 0)
- {
- cout << "Tem de escolher uma dimensão positiva\n";
- error = true;
- }
- else if (v > MAX_SIZE)
- {
- cout << "Usou um valor demasiado elevado.\n";
- error = true;
- }
- else error = false;
- return v;
- }
- void chooseHorizontal()
- {
- while (true)
- {
- cout << "Dimensão horizontal (Maximo: " << MAX_SIZE << "): ";
- int v;
- v = chooseDimension();
- if (!error)
- {
- x = v - 1;
- break;
- }
- }
- return;
- }
- void chooseVertical()
- {
- while (true)
- {
- cout << "Dimensão vertical (Maximo: " << MAX_SIZE << "): ";
- int v;
- v = chooseDimension();
- if (!error)
- {
- y = v - 1;
- break;
- }
- }
- return;
- }
- void startGame()
- {
- resetArray();
- xsnake = {};
- ysnake = {};
- score = 0;
- snakeLength = 1;
- error = false;
- food = false;
- lose = false;
- int xpos, ypos;
- xpos = rand() % x;
- xsnake.push_back(xpos);
- ypos = rand() % y;
- ysnake.push_back(ypos);
- moveDirection = rand() % 4 + 1;
- tabuleiro[ypos][xpos] = 1;
- timeri = time(NULL);
- return;
- }
- void resetArray()
- {
- for (int i = 0; i <= y; i++)
- for (int c = 0; c <= x; c++)
- tabuleiro[i][c] = 0;
- }
- void theGame()
- {
- setlocale(LC_ALL, "C");
- int dif = 12 - dificulty * 3;
- while (true)
- {
- if (!food) placeFood();
- displayBoard();
- for (int i = 0; i <= dif; i++)
- {
- testHit();
- std::this_thread::sleep_for(std::chrono::milliseconds(10));
- }
- moveSnake();
- if (lose) return;
- }
- }
- void moveSnake()
- {
- switch (moveDirection)
- {
- case 1:
- newx = xsnake[0];
- newy = ysnake[0] - 1;
- break;
- case 2:
- newx = xsnake[0] + 1;
- newy = ysnake[0];
- break;
- case 3:
- newx = xsnake[0];
- newy = ysnake[0] + 1;
- break;
- case 4:
- newx = xsnake[0] - 1;
- newy = ysnake[0];
- break;
- }
- correctPlace();
- int elem = tabuleiro[newy][newx];
- if (elem == 2)
- {
- food = false;
- ate = true;
- addScore();
- createNewPiece();
- }
- if (elem == 1)
- {
- lose = true;
- return;
- }
- else ate = false;
- if (!ate)
- {
- tabuleiro[ysnake[snakeLength - 1]][xsnake[snakeLength - 1]] = 0;
- }
- for (int i = snakeLength - 1; i >= 1; i--)
- {
- xsnake[i] = xsnake[i - 1];
- ysnake[i] = ysnake[i - 1];
- }
- xsnake[0] = newx;
- ysnake[0] = newy;
- tabuleiro[newy][newx] = 1;
- return;
- }
- void createNewPiece()
- {
- snakeLength++;
- xsnake.push_back(xsnake[snakeLength - 2]);
- ysnake.push_back(ysnake[snakeLength - 2]);
- return;
- }
- void correctPlace()
- {
- if (newx > x) newx = 0;
- else if (newx < 0) newx = x;
- else if (newy > y) newy = 0;
- else if (newy < 0) newy = y;
- return;
- }
- void placeFood()
- {
- int xpos, ypos;
- do
- {
- xpos = rand() % x;
- ypos = rand() % y;
- if (!(1 == tabuleiro[ypos][xpos]))
- {
- tabuleiro[ypos][xpos] = 2;
- food = true;
- error = false;
- }
- else error = true;
- } while (error);
- return;
- }
- void addScore()
- {
- score = score + 10 * dificulty;
- return;
- }
- void displayBoard()
- {
- system("CLS");
- displayHorizontalBorder();
- cout << endl;
- displayScore();
- displayHorizontalBorder();
- cout << endl;
- for (int i = 0; i <= y; i++)
- {
- cout << "|";
- for (int c = 0; c <= x; c++)
- {
- int elem = tabuleiro[i][c];
- if (elem == 0) cout << " ";
- else if (1 == elem) cout << "o";
- else cout << "*";
- }
- cout << "|" << endl;
- }
- displayHorizontalBorder();
- return;
- }
- void displayScore()
- {
- cout << "|Score: " << std::setw(x - 6) << score << "|";
- cout << endl;
- }
- void displayHorizontalBorder()
- {
- for (int i = -2; i <= x; i++)
- {
- if (-2 == i || x == i) cout << "+";
- else cout << "-";
- }
- }
- void inputControl(char control)
- {
- switch (control)
- {
- case 'H':
- if (3 != moveDirection) moveDirection = 1;
- break;
- case 'M':
- if (4 != moveDirection) moveDirection = 2;
- break;
- case 'P':
- if (1 != moveDirection) moveDirection = 3;
- break;
- case 'K':
- if (2 != moveDirection) moveDirection = 4;
- break;
- }
- return;
- }
- void testHit()
- {
- if (_kbhit())
- {
- char control;
- control = (_getch(), _getch());
- inputControl(control);
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement