Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <Windows.h>
- #include <ctype.h>
- #include <random>
- #include <chrono>
- #include <thread>
- #include <conio.h>
- using namespace std;
- string player = "O";
- int playerX = 1;
- int playerY = 0;
- int originalPlayerX = playerX;
- int originalPlayerY = playerY;
- int mainMenu();
- void gameStart();
- void customGame();
- void gameControls();
- void gameCredits();
- void exitProgram();
- int difficultyMenu();
- void easyDifficulty();
- void normalDifficulty();
- void hardDifficulty();
- void backToMenu();
- void characterMove(string**, int&, int&, int, int);
- void playerMove(string**);
- void display(string**, int, int, int, string);
- void loadMap(string, int, string);
- int filesInFolderCount(string);
- void levels(string);
- void ShowConsoleCursor(bool showFlag);
- int main()
- {
- system("color 04");
- ShowWindow(GetConsoleWindow(), SW_MAXIMIZE);
- SendMessage(GetConsoleWindow(), WM_SYSKEYDOWN, VK_RETURN, 0x2000000);
- ShowConsoleCursor(false);
- mainMenu();
- return 0;
- }
- typedef void(*menuOptions)();
- menuOptions mainMenuOptions[] = { gameStart, customGame, gameControls, gameCredits, exitProgram };
- int mainMenu()
- {
- string menu[5] = { "Start Game", "Create a Custom Game", "Controls", "Credits", "Exit" };
- int pointer = 0;
- bool lastUpKeyState = false;
- bool lastDownKeyState = false;
- bool lastReturnKeyState = false;
- bool arrowVisible = true;
- while (true)
- {
- system("cls");
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
- cout << "The Labyrinth" << "\n" << "\n";
- cout << "Main Menu" << "\n" << "\n";
- for (int i = 0; i < 5; i++)
- {
- if (i == pointer)
- {
- if (arrowVisible)
- {
- cout << "-> ";
- arrowVisible = false;
- }
- else
- {
- cout << " "; // Prints 4 spaces to cover the previous "-> "
- arrowVisible = true;
- }
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN);
- cout << menu[i] << endl;
- }
- else
- {
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
- cout << menu[i] << endl;
- }
- }
- bool upKeyState = GetAsyncKeyState(VK_UP);
- if ( (upKeyState) && !(lastUpKeyState) )
- {
- pointer -= 1;
- if (pointer == -1)
- {
- pointer = 4;
- }
- }
- lastUpKeyState = upKeyState;
- bool downKeyState = GetAsyncKeyState(VK_DOWN);
- if ( (downKeyState) && !(lastDownKeyState) )
- {
- pointer += 1;
- if (pointer == 5)
- {
- pointer = 0;
- }
- }
- lastDownKeyState = downKeyState;
- bool returnKeyState = GetAsyncKeyState(VK_RETURN);
- if ( (returnKeyState) && !(lastReturnKeyState) )
- {
- mainMenuOptions[pointer]();
- }
- lastReturnKeyState = returnKeyState;
- this_thread::sleep_for(chrono::milliseconds(200));
- }
- }
- void gameStart()
- {
- difficultyMenu();
- }
- void customGame()
- {
- system("cls");
- }
- void gameControls()
- {
- system("cls");
- cout << "\n";
- cout << " --------------CONTROLS---------------" << "\n";
- cout << " | |" << "\n";
- cout << " | |" << "\n";
- cout << " | ^ --> Up Arrow to move up |" << "\n";
- cout << " | < --> Left Arrow to move left |" << "\n";
- cout << " | > --> Right Arrow to move right |" << "\n";
- cout << " | v --> Down Arrow to move down |" << "\n";
- cout << " | |" << "\n";
- cout << " | Enter / Return --> Enter |" << "\n";
- cout << " | Escape --> Back |" << "\n";
- cout << " | |" << "\n";
- cout << " | |" << "\n";
- cout << " -------------------------------------" << "\n";
- while (true)
- {
- backToMenu();
- }
- }
- void gameCredits()
- {
- system("cls");
- cout << "Thanks for playing!" << "\n";
- cout << "\n";
- cout << "Creator: Petar Bogdanov" << "\n";
- cout << "The people who helped me with this project: eng. Stoyan Filipov and eng. Monika Gencheva" << "\n" << "\n";
- cout << "Press \"ESCAPE\" to go back to the Main Menu" << "\n";
- while (true)
- {
- backToMenu();
- }
- }
- void exitProgram()
- {
- exit(0);
- }
- menuOptions difficultyMenuOptions[] = { easyDifficulty, normalDifficulty, hardDifficulty };
- int difficultyMenu()
- {
- string difficulty[3] = { "Easy", "Normal", "Hard" };
- int pointer = 0;
- bool lastUpKeyState = false;
- bool lastDownKeyState = false;
- bool lastReturnKeyState = false;
- bool arrowVisible = true;
- while (true)
- {
- system("cls");
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
- cout << "Choose difficulty" << "\n" << "\n";
- for (int i = 0; i < 3; i++)
- {
- if (i == pointer)
- {
- if (arrowVisible)
- {
- cout << "-> ";
- arrowVisible = false;
- }
- else
- {
- cout << " "; // Prints 4 spaces to cover the previous "-> "
- arrowVisible = true;
- }
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN);
- cout << difficulty[i] << endl;
- }
- else
- {
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
- cout << difficulty[i] << endl;
- }
- }
- bool upKeyState = GetAsyncKeyState(VK_UP);
- if ( (upKeyState) && !(lastUpKeyState) )
- {
- pointer -= 1;
- if (pointer == -1)
- {
- pointer = 2;
- }
- }
- lastUpKeyState = upKeyState;
- bool downKeyState = GetAsyncKeyState(VK_DOWN);
- if ( (downKeyState) && !(lastDownKeyState) )
- {
- pointer += 1;
- if (pointer == 3)
- {
- pointer = 0;
- }
- }
- lastDownKeyState = downKeyState;
- bool returnKeyState = GetAsyncKeyState(VK_RETURN);
- if ( (returnKeyState) && !(lastReturnKeyState) )
- {
- difficultyMenuOptions[pointer]();
- }
- backToMenu();
- lastReturnKeyState = returnKeyState;
- this_thread::sleep_for(chrono::milliseconds(200));
- }
- }
- void easyDifficulty()
- {
- levels("easy");
- }
- void normalDifficulty()
- {
- levels("normal");
- }
- void hardDifficulty()
- {
- levels("hard");
- }
- void backToMenu()
- {
- if (GetAsyncKeyState(VK_ESCAPE))
- {
- main();
- }
- }
- void characterMove(string** labyrinth, int& x, int& y, int dx, int dy)
- {
- if (labyrinth[x + dx][y + dy] == "S" || labyrinth[x + dx][y + dy] == " " || labyrinth[x + dx][y + dy] == "E")
- {
- x += dx;
- y += dy;
- }
- }
- void playerMove(string** labyrinth)
- {
- char key = _getch();
- if (key == 72) // The ASCII CODE FOR THE UP ARROW KEY
- {
- characterMove(labyrinth, playerX, playerY, -1, 0);
- }
- else if (key == 75) // The ASCII CODE FOR THE LEFT ARROW KEY
- {
- characterMove(labyrinth, playerX, playerY, 0, -1);
- }
- else if (key == 80) // The ASCII CODE FOR THE DOWN ARROW KEY
- {
- characterMove(labyrinth, playerX, playerY, 1, 0);
- }
- else if (key == 77) // The ASCII CODE FOR THE RIGHT ARROW KEY
- {
- characterMove(labyrinth, playerX, playerY, 0, 1);
- }
- }
- void display(string** labyrinth, int rowsCount, int columnsCount, int level, string levelType)
- {
- bool breakAfterRender = false;
- while (true)
- {
- system("cls");
- cout << "Difficulty: " << levelType << "\n";
- cout << "Level: " << level << " / " << filesInFolderCount(levelType) << "\n" << "\n";
- int colour = rand() % 15 + 1;
- for (int i = 0; i < rowsCount; i++)
- {
- for (int j = 0; j < columnsCount; j++)
- {
- if ((i == playerX) && (j == playerY))
- {
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colour);
- cout << player;
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
- }
- else
- {
- cout << labyrinth[i][j];
- }
- }
- cout << "\n";
- }
- if (breakAfterRender)
- {
- cout << "\n";
- cout << "Congratulations! You have reached the end of the level!" << "\n";
- playerX = originalPlayerX;
- playerY = originalPlayerY;
- cout << "\n" << "\n";
- cout << "Press \"ENTER\" to continue" << "\n";
- while (true)
- {
- if (_getch() == 13) // The ASCII CODE FOR THE ENTER / RETURN KEY
- {
- break;
- }
- }
- break;
- }
- labyrinth[playerX][playerY] = " ";
- playerMove(labyrinth);
- if (labyrinth[playerX][playerY] == "E")
- {
- breakAfterRender = true;
- }
- labyrinth[playerX][playerY] = player;
- this_thread::sleep_for(chrono::milliseconds(24));
- }
- }
- void loadMap(string fileName, int level, string levelType)
- {
- int rowsCount = 1;
- int columnsCount = 0;
- int startAt = 0;
- int endAt = 0;
- ifstream mapFile;
- mapFile.open(fileName);
- if (mapFile.is_open())
- {
- char mychar;
- while (mapFile)
- {
- mychar = mapFile.get();
- if (mychar == '\n')
- {
- rowsCount++;
- }
- else if ( (mychar != '\n') && (rowsCount == 1) )
- {
- columnsCount++;
- }
- else if (mychar == 'S')
- {
- startAt = rowsCount - 1;
- }
- else if (mychar == 'E')
- {
- endAt = rowsCount - 1;
- }
- }
- }
- else
- {
- cout << "Error: Unable to open file " << fileName << "\n" << "\n";
- cout << "Press \"ESCAPE\" to go back to the Main Menu" << "\n";
- while (true)
- {
- backToMenu();
- }
- }
- mapFile.close();
- // Dynamically allocating row space in the heap
- string** labyrinth = new string * [rowsCount];
- // Dynamically allocating column space in the heap
- for (int i = 0; i < rowsCount; i++)
- {
- labyrinth[i] = new string[columnsCount];
- }
- int currentRow = 0;
- int currentColumn = 0;
- mapFile.open(fileName);
- if (mapFile.is_open())
- {
- char currentSymbol;
- while (mapFile.get(currentSymbol))
- {
- if ( (currentRow < rowsCount) && (currentColumn < columnsCount) )
- {
- labyrinth[currentRow][currentColumn] = currentSymbol;
- }
- if (currentSymbol == '\n')
- {
- currentRow++;
- currentColumn = 0;
- }
- else
- {
- currentColumn++;
- }
- }
- }
- mapFile.close();
- // Displaying the labyrinth
- display(labyrinth, rowsCount, columnsCount, level, levelType);
- // Free up the space after the use of the array
- for (int i = 0; i < rowsCount; i++)
- {
- delete[] labyrinth[i];
- }
- delete[] labyrinth;
- }
- int filesInFolderCount(string levelType)
- {
- int fileCount = 0;
- string folderPath = "maps/" + levelType + "/";
- // Get the first file in the folder
- WIN32_FIND_DATAA findFileData;
- HANDLE hFind = FindFirstFileA((folderPath + "*").c_str(), &findFileData);
- // Iterate through the files in the folder
- if (hFind != INVALID_HANDLE_VALUE)
- {
- while (true)
- {
- // Check if the current file is a regular file
- if ( (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0 )
- {
- fileCount++;
- }
- if ( !(FindNextFileA(hFind, &findFileData)) )
- {
- break;
- }
- }
- // Clean up
- FindClose(hFind);
- }
- return fileCount;
- }
- void levels(string levelType)
- {
- int level = 1;
- if ( (levelType != "easy") && (levelType != "normal") && (levelType != "hard") )
- {
- system("cls");
- cout << "Invalid level!" << "\n";
- return;
- }
- while (level <= filesInFolderCount(levelType))
- {
- system("cls");
- string mapFile = "maps/" + levelType + "/map_" + to_string(level) + ".txt";
- // Opening the text file
- loadMap(mapFile, level, levelType);
- level++;
- }
- gameCredits();
- }
- void ShowConsoleCursor(bool showFlag)
- {
- HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
- CONSOLE_CURSOR_INFO cursorInfo;
- GetConsoleCursorInfo(out, &cursorInfo);
- cursorInfo.bVisible = showFlag; // SET THE CURSOR VISIBILITY
- SetConsoleCursorInfo(out, &cursorInfo);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement