Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // THIS IS DUNGEON CRAWLER
- #include <iostream>
- #include <string>
- #include <chrono>
- #include <thread>
- #include <stdio.h>
- #include <termios.h>
- #include <unistd.h>
- #include <fcntl.h>
- struct enemy
- {
- int position;
- enemy();
- void move();
- };
- void drawBoard(int board[64]);
- void movement(int board[], int &userPosition, enemy enemies[]);
- void hasWon(int board[], int &userPosition);
- void onEnemy(int board[], int &userPosition, enemy enemies[]);
- int kbhit();
- // global variable
- bool lost = false;
- bool won = false;
- enemy::enemy()
- {
- // 0 is player start position
- position = rand() % 63 + 1;
- }
- void enemy::move()
- {
- int r = rand() % 3;
- int newPosition = position;
- switch (r)
- {
- case 0:
- newPosition -= 8;
- break;
- case 1:
- newPosition += 8;
- break;
- case 2:
- newPosition -= 1;
- break;
- case 3:
- newPosition += 1;
- break;
- default:
- return;
- }
- if (newPosition < 0 || newPosition > 62)
- return; // 62 because 63 is the players objective
- position = newPosition;
- return;
- }
- ///////////////////////////////////
- //////////////////////////////////
- int main()
- {
- int board[64];
- int userPosition = 0;
- #pragma region Intializing board
- for (int i = 0; i < 64; i++)
- {
- board[i] = 0;
- }
- board[userPosition] = 5;
- // spawning enemies
- enemy enemies[4];
- for (int i = 0; i < 4; i++)
- {
- board[enemies[i].position] = 7;
- }
- board[63] = 1;
- #pragma endregion
- drawBoard(board);
- while (won != true && lost != true)
- {
- std::cout << won << std::endl;
- movement(board, userPosition, enemies);
- drawBoard(board);
- hasWon(board, userPosition);
- onEnemy(board, userPosition, enemies);
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
- std::system("clear");
- }
- std::cout << "Congratulation, you've finished the game\n\n";
- if (won == true)
- std::cout << "You've WOOOOON !!! \n\n";
- else
- std::cout << "You're noob...\n\n";
- }
- ///////////////////////////////////////////////
- /////////////////////////////////////////////
- void movement(int board[], int &userPosition, enemy enemies[])
- {
- if (kbhit())
- {
- char input = getchar();
- int userPositionNew = userPosition;
- switch (input)
- {
- case 'w':
- userPositionNew -= 8;
- break;
- case 's':
- userPositionNew += 8;
- break;
- case 'a':
- userPositionNew -= 1;
- break;
- case 'd':
- userPositionNew += 1;
- break;
- default:
- return;
- }
- if (userPositionNew < 0 || userPositionNew > 63)
- return;
- board[userPosition] = 0;
- userPosition = userPositionNew;
- board[userPosition] = 5;
- // I wasnt sure how to do this, but I wanted to make enemy move every time player moves...
- for (int i = 0; i < 4; i++)
- {
- board[enemies[i].position] = 0;
- }
- for (int i = 0; i < 4; i++)
- {
- enemies[i].move();
- }
- for (int i = 0; i < 4; i++)
- {
- board[enemies[i].position] = 7;
- }
- }
- return;
- }
- void onEnemy(int board[], int &userPosition, enemy enemies[])
- {
- for (int i = 0; i < 4; i++)
- {
- if (userPosition == enemies[i].position)
- lost = true;
- }
- return;
- }
- void hasWon(int board[], int &userPosition)
- {
- if (userPosition == 63)
- {
- won = true;
- std::cout << "You've won" << std::endl;
- }
- return;
- }
- void drawBoard(int board[64])
- {
- // int i = 1 and no 0 because of the division, division of 0 by 8 is 0
- // and so it writes newline that we do not want
- for (int i = 1; i <= 64; i++)
- {
- std::cout << board[i - 1] << " ";
- if (i % 8 == 0)
- std::cout << '\n';
- }
- }
- int kbhit()
- {
- struct termios oldt, newt;
- int ch;
- int oldf;
- tcgetattr(STDIN_FILENO, &oldt);
- newt = oldt;
- newt.c_lflag &= ~(ICANON | ECHO);
- tcsetattr(STDIN_FILENO, TCSANOW, &newt);
- oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
- fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
- ch = getchar();
- tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
- fcntl(STDIN_FILENO, F_SETFL, oldf);
- if (ch != EOF)
- {
- ungetc(ch, stdin);
- return 1;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement