Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <Windows.h>
- #include <conio.h>
- #include <ctype.h>
- #include <random>
- #include <chrono>
- #include <thread>
- #include <mmsystem.h>
- #pragma comment(lib, "winmm.lib")
- using namespace std;
- size_t playerX = 0;
- size_t playerY = 0;
- int mainMenu();
- void gameStart();
- void customGame();
- void gameControls();
- void gameCredits();
- void customMapInformation();
- void exitProgram();
- int customGameMenu();
- int difficultyMenu();
- void easyDifficulty();
- void normalDifficulty();
- void hardDifficulty();
- void backToMenu();
- void playMusic(wstring&);
- void characterMove(string**, size_t&, size_t&, size_t, size_t);
- void playerMove(string**);
- void display(string**, size_t, size_t, size_t, string);
- void loadMapFromAFolder(string, size_t, string);
- void loadMap(string**, size_t, size_t);
- void loadCustomMapFromTheFolder();
- void createACustomMap();
- void rowsAndColumnsChoice(size_t&, size_t&);
- void rowsAndColumnsSetByThePlayer(size_t&, size_t&);
- void startAndEndChoice(int&, int&);
- void startAndEndSetByThePlayer(string**&, size_t&, size_t&, size_t&, size_t&, string&, string&);
- int setWallsCountChoice(int&);
- void wallsSetByThePlayerWithoutASetWallsCount(string**&, size_t&, size_t&);
- void chooseAnAction(string**&, size_t&, size_t&, string&);
- void saveTheMapConfiguration(string**&, size_t&, size_t&, string&);
- void displayTheConfiguration(string**, size_t, size_t);
- void displayTheConfigurationWithCoordinates(string**, size_t, size_t);
- void displayTheConfigurationWithoutSpaces(string**, size_t, size_t);
- size_t filesInFolderCount(string);
- void levels(string);
- void CoutCentered(string);
- void CoutCenteredWithoutNewLine(string);
- size_t GetConsoleWidth();
- void SetVolume(int);
- void ShowConsoleCursor(bool showFlag);
- int main()
- {
- system("color 04");
- HWND hWnd = GetConsoleWindow(); // Get the console window handle
- ShowWindow(hWnd, SW_MAXIMIZE); // Maximize the window
- HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
- CONSOLE_FONT_INFOEX fontInfo{};
- fontInfo.cbSize = sizeof(fontInfo);
- GetCurrentConsoleFontEx(consoleHandle, FALSE, &fontInfo);
- fontInfo.dwFontSize.X += 2; // Increase the font width
- fontInfo.dwFontSize.Y += 2; // Increase the font height
- SetCurrentConsoleFontEx(consoleHandle, FALSE, &fontInfo);
- ShowConsoleCursor(false);
- PlaySound(TEXT("running_home_to_you.wav"), NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);
- // Set audio volume to 25%
- SetVolume(0xFFFF / 4);
- mainMenu();
- return 0;
- }
- typedef void(*menuOptions)();
- menuOptions mainMenuOptions[] = { gameStart, customGame, gameControls, gameCredits, customMapInformation, exitProgram };
- int mainMenu()
- {
- string menu[] = { "Start Game", "Create a Custom Game", "Controls", "Credits", "How to create a custom map?", "Exit"};
- size_t 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 << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n";
- CoutCentered("The Labyrinth");
- cout << "\n" << "\n";
- CoutCentered("Main Menu");
- cout << "\n";
- for (size_t i = 0; i < size(menu); i++)
- {
- if (i == pointer)
- {
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN);
- if (arrowVisible)
- {
- CoutCentered("-> " + menu[i]);
- arrowVisible = false;
- }
- else
- {
- CoutCentered(" ");
- arrowVisible = true;
- }
- }
- else
- {
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
- CoutCentered(menu[i]);
- }
- }
- bool upKeyState = GetAsyncKeyState(VK_UP);
- if ( (upKeyState) && !(lastUpKeyState) )
- {
- pointer -= 1;
- if (pointer == -1)
- {
- pointer = size(menu) - 1; // FROM pointer = 4
- }
- }
- lastUpKeyState = upKeyState;
- bool downKeyState = GetAsyncKeyState(VK_DOWN);
- if ( (downKeyState) && !(lastDownKeyState) )
- {
- pointer += 1;
- if (pointer == size(menu)) // FROM 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()
- {
- customGameMenu();
- }
- void gameControls()
- {
- system("cls");
- cout << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n";
- CoutCentered(" --------------CONTROLS---------------");
- CoutCentered(" | |");
- CoutCentered(" | |");
- CoutCentered(" | ^ --> Up Arrow to move up |");
- CoutCentered(" | < --> Left Arrow to move left |");
- CoutCentered(" | > --> Right Arrow to move right |");
- CoutCentered(" | v --> Down Arrow to move down |");
- CoutCentered(" | |");
- CoutCentered(" | Enter / Return --> Enter |");
- CoutCentered(" | Escape --> Back |");
- CoutCentered(" | |");
- CoutCentered(" | |");
- CoutCentered(" -------------------------------------");
- while (true)
- {
- backToMenu();
- }
- }
- void gameCredits()
- {
- system("cls");
- cout << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n";
- CoutCentered("Thanks for playing!");
- cout << "\n";
- CoutCentered("Creator: Petar Bogdanov");
- CoutCentered("The people who helped me with this project: eng. Stoyan Filipov and eng. Monika Gencheva");
- cout << "\n";
- CoutCentered("Press \"ESCAPE\" to go back to the Main Menu");
- while (true)
- {
- backToMenu();
- }
- }
- void customMapInformation()
- {
- system("cls");
- cout << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n";
- CoutCentered("To create a custom map you need to:");
- cout << "\n";
- CoutCentered("1. Create a file with the .txt extension. ");
- CoutCentered("2. Visualize your map inside the .txt file.");
- CoutCentered("3. Put the file in the \"custom\" subfolder.");
- cout << "\n";
- CoutCentered("NOTE: DO NOT DO PART 3 IF YOU HAVE CREATED THE FILE ALREADY THERE!");
- cout << "\n" << "\n";
- CoutCentered("Press \"ESCAPE\" to go back to the Main Menu");
- while (true)
- {
- backToMenu();
- }
- }
- void exitProgram()
- {
- exit(0);
- }
- menuOptions customMenuOptions[] = { loadCustomMapFromTheFolder, createACustomMap };
- int customGameMenu()
- {
- string menu[] = { "Play on already created custom map placed in the subfolder", "Create a Custom Map" };
- size_t 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 << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n";
- CoutCentered("Type of a custom map");
- cout << "\n" << "\n";
- for (size_t i = 0; i < size(menu); i++)
- {
- if (i == pointer)
- {
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN);
- if (arrowVisible)
- {
- CoutCentered("-> " + menu[i]);
- arrowVisible = false;
- }
- else
- {
- CoutCentered(" ");
- arrowVisible = true;
- }
- }
- else
- {
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
- CoutCentered(menu[i]);
- }
- }
- bool upKeyState = GetAsyncKeyState(VK_UP);
- if ( (upKeyState) && !(lastUpKeyState) )
- {
- pointer -= 1;
- if (pointer == -1)
- {
- pointer = size(menu) - 1;
- }
- }
- lastUpKeyState = upKeyState;
- bool downKeyState = GetAsyncKeyState(VK_DOWN);
- if ( (downKeyState) && !(lastDownKeyState) )
- {
- pointer += 1;
- if (pointer == size(menu))
- {
- pointer = 0;
- }
- }
- lastDownKeyState = downKeyState;
- bool returnKeyState = GetAsyncKeyState(VK_RETURN);
- if ( (returnKeyState) && !(lastReturnKeyState) )
- {
- customMenuOptions[pointer]();
- }
- lastReturnKeyState = returnKeyState;
- backToMenu();
- this_thread::sleep_for(chrono::milliseconds(200));
- }
- }
- menuOptions difficultyMenuOptions[] = { easyDifficulty, normalDifficulty, hardDifficulty };
- int difficultyMenu()
- {
- string difficulty[] = { "Easy", "Normal", "Hard" };
- size_t pointer = 0;
- bool lastUpKeyState = false;
- bool lastDownKeyState = false;
- bool lastReturnKeyState = false;
- bool arrowVisible = true;
- while (true)
- {
- system("cls");
- cout << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n";
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
- CoutCentered("Choose difficulty");
- cout << "\n" << "\n";
- for (size_t i = 0; i < size(difficulty); i++)
- {
- if (i == pointer)
- {
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN);
- if (arrowVisible)
- {
- CoutCentered("-> " + difficulty[i]);
- arrowVisible = false;
- }
- else
- {
- CoutCentered(" ");
- arrowVisible = true;
- }
- }
- else
- {
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
- CoutCentered(difficulty[i]);
- }
- }
- bool upKeyState = GetAsyncKeyState(VK_UP);
- if ( (upKeyState) && !(lastUpKeyState) )
- {
- pointer -= 1;
- if (pointer == -1)
- {
- pointer = size(difficulty) - 1;
- }
- }
- lastUpKeyState = upKeyState;
- bool downKeyState = GetAsyncKeyState(VK_DOWN);
- if ( (downKeyState) && !(lastDownKeyState) )
- {
- pointer += 1;
- if (pointer == size(difficulty))
- {
- pointer = 0;
- }
- }
- lastDownKeyState = downKeyState;
- bool returnKeyState = GetAsyncKeyState(VK_RETURN);
- if ( (returnKeyState) && !(lastReturnKeyState) )
- {
- difficultyMenuOptions[pointer]();
- }
- lastReturnKeyState = returnKeyState;
- backToMenu();
- 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))
- {
- mainMenu();
- }
- }
- void characterMove(string** labyrinth, size_t& x, size_t& y, size_t dx, size_t 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);
- //}
- // Using the GetAsyncKeyState function from the Windows.h header performs better than using the ASCII codes for the arrow buttons from the conio.h header
- bool upKeyState = GetAsyncKeyState(VK_UP);
- bool leftKeyState = GetAsyncKeyState(VK_LEFT);
- bool downKeyState = GetAsyncKeyState(VK_DOWN);
- bool rightKeyState = GetAsyncKeyState(VK_RIGHT);
- if (upKeyState)
- {
- characterMove(labyrinth, playerX, playerY, -1, 0);
- }
- else if (leftKeyState)
- {
- characterMove(labyrinth, playerX, playerY, 0, -1);
- }
- else if (downKeyState)
- {
- characterMove(labyrinth, playerX, playerY, 1, 0);
- }
- else if (rightKeyState)
- {
- characterMove(labyrinth, playerX, playerY, 0, 1);
- }
- }
- void display(string** labyrinth, size_t rowsCount, size_t columnsCount, size_t level, string levelType)
- {
- string player = "O";
- bool breakAfterRender = false;
- while (true)
- {
- system("cls");
- cout << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n";
- // Center the game screen
- size_t consoleWidth = GetConsoleWidth();
- size_t screenLeftMargin = (consoleWidth - columnsCount) / 2;
- CoutCentered("Difficulty: " + levelType);
- if ( (levelType == "easy") || (levelType == "normal") || (levelType == "hard") )
- {
- CoutCentered("Level: " + to_string(level) + " / " + to_string(filesInFolderCount(levelType)));
- cout << "\n" << "\n";
- }
- else
- {
- cout << "\n";
- }
- int colour = rand() % 15 + 1;
- for (size_t i = 0; i < rowsCount; i++)
- {
- cout << setw(screenLeftMargin); // Center each row
- for (size_t 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" << "\n";
- CoutCentered("Congratulations! You have reached the end of the level!");
- cout << "\n" << "\n";
- CoutCentered("Press \"ENTER\" to continue");
- cout << "\n";
- while (true)
- {
- if (GetAsyncKeyState(VK_RETURN))
- {
- break;
- }
- }
- break;
- }
- labyrinth[playerX][playerY] = " ";
- playerMove(labyrinth);
- if (labyrinth[playerX][playerY] == "E")
- {
- breakAfterRender = true;
- }
- labyrinth[playerX][playerY] = player;
- // Pause sound
- if (GetAsyncKeyState(VK_ESCAPE))
- {
- system("cls");
- cout << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n";
- CoutCentered("Paused");
- while (true)
- {
- if (GetAsyncKeyState(VK_ESCAPE))
- {
- break;
- }
- }
- }
- this_thread::sleep_for(chrono::milliseconds(64));
- }
- }
- void loadMapFromAFolder(string fileName, size_t level, string levelType)
- {
- size_t rowsCount = 1;
- size_t columnsCount = 0;
- size_t startAt = 0;
- size_t endAt = 0;
- ifstream mapFile;
- mapFile.open(fileName);
- if (mapFile.is_open())
- {
- char currentSymbol;
- while (mapFile)
- {
- currentSymbol = mapFile.get();
- if (currentSymbol == '\n')
- {
- rowsCount++;
- }
- else if ( (currentSymbol != '\n') && (rowsCount == 1) )
- {
- columnsCount++;
- }
- else if (currentSymbol == 'S')
- {
- startAt = rowsCount - 1;
- }
- else if (currentSymbol == 'E')
- {
- endAt = rowsCount - 1;
- }
- }
- }
- else
- {
- CoutCentered("Error: Unable to open file " + fileName + ".");
- cout << "\n";
- CoutCentered("Press \"ESCAPE\" to go back to the Main Menu.");
- 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 (size_t i = 0; i < rowsCount; i++)
- {
- labyrinth[i] = new string[columnsCount];
- }
- size_t currentRow = 0;
- size_t 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();
- for (size_t i = 0; i < rowsCount; i++)
- {
- for (size_t j = 0; j < columnsCount; j++)
- {
- if (labyrinth[i][j] == "S")
- {
- playerX = i;
- playerY = j;
- }
- }
- }
- // Displaying the labyrinth
- display(labyrinth, rowsCount, columnsCount, level, levelType);
- // Free up the space after the use of the array
- for (size_t i = 0; i < rowsCount; i++)
- {
- delete[] labyrinth[i];
- }
- delete[] labyrinth;
- }
- void loadMap(string** labyrinth, size_t rowsCount, size_t columnsCount)
- {
- for (size_t i = 0; i < rowsCount; i++)
- {
- for (size_t j = 0; j < columnsCount; j++)
- {
- if (labyrinth[i][j] == "S")
- {
- playerX = i;
- playerY = j;
- }
- }
- }
- // Displaying the labyrinth
- display(labyrinth, rowsCount, columnsCount, size_t(0), "custom");
- // Free up the space after the use of the array
- for (size_t i = 0; i < rowsCount; i++)
- {
- delete[] labyrinth[i];
- }
- delete[] labyrinth;
- }
- void loadCustomMapFromTheFolder()
- {
- size_t fileCount = filesInFolderCount("custom");
- string* menu = new string[fileCount];
- WIN32_FIND_DATA findData;
- HANDLE hFind;
- string folderPath = "maps/custom/";
- wstring wideFolderPath(folderPath.begin(), folderPath.end());
- wideFolderPath += L"*";
- hFind = FindFirstFile(wideFolderPath.c_str(), &findData);
- if (hFind != INVALID_HANDLE_VALUE)
- {
- char narrowString[MAX_PATH];
- string* newData;
- size_t counter = 0;
- while (FindNextFile(hFind, &findData) != 0)
- {
- WideCharToMultiByte(CP_ACP, 0, findData.cFileName, -1, narrowString, MAX_PATH, NULL, NULL);
- if ( (strcmp(narrowString, ".") != 0) && (strcmp(narrowString, "..") != 0) )
- {
- if (counter == fileCount)
- {
- newData = new string[fileCount + 2];
- for (size_t i = 0; i < counter; i++)
- {
- newData[i] = menu[i];
- }
- delete[] menu;
- menu = newData;
- }
- menu[counter] = narrowString;
- counter++;
- }
- }
- FindClose(hFind);
- }
- size_t pointer = 0;
- bool lastUpKeyState = false;
- bool lastDownKeyState = false;
- bool lastReturnKeyState = false;
- bool arrowVisible = true;
- while (true)
- {
- system("cls");
- cout << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n";
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
- CoutCentered("There are " + to_string(fileCount) + " files in the subfolder called \"custom\".");
- cout << "\n";
- CoutCentered("Select a Custom Map");
- cout << "\n";
- for (size_t i = 0; i < fileCount; i++)
- {
- if (i == pointer)
- {
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN);
- if (arrowVisible)
- {
- CoutCentered("-> " + menu[i]);
- arrowVisible = false;
- }
- else
- {
- CoutCentered(" ");
- arrowVisible = true;
- }
- }
- else
- {
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
- CoutCentered(menu[i]);
- }
- }
- bool upKeyState = GetAsyncKeyState(VK_UP);
- if ( (upKeyState) && !(lastUpKeyState) )
- {
- pointer -= 1;
- if (pointer == -1)
- {
- pointer = fileCount - 1;
- }
- }
- lastUpKeyState = upKeyState;
- bool downKeyState = GetAsyncKeyState(VK_DOWN);
- if ( (downKeyState) && !(lastDownKeyState) )
- {
- pointer += 1;
- if (pointer == fileCount)
- {
- pointer = 0;
- }
- }
- lastDownKeyState = downKeyState;
- bool returnKeyState = GetAsyncKeyState(VK_RETURN);
- if ( (returnKeyState) && !(lastReturnKeyState) )
- {
- // Play sound
- PlaySound(TEXT("river.wav"), NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);
- // Set audio volume to 25%
- SetVolume(0xFFFF / 4);
- string mapFile = "maps/custom/" + menu[pointer];
- // Opening the text file
- loadMapFromAFolder(mapFile, 0, "custom");
- PlaySound(TEXT("running_home_to_you.wav"), NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);
- // Set audio volume to 25%
- SetVolume(0xFFFF / 4);
- mainMenu();
- }
- lastReturnKeyState = returnKeyState;
- if (GetAsyncKeyState(VK_ESCAPE))
- {
- customGameMenu();
- }
- this_thread::sleep_for(chrono::milliseconds(200));
- }
- delete[] menu;
- }
- void createACustomMap()
- {
- system("cls");
- system("color 04");
- cin.ignore();
- cin.get();
- size_t rowsCount = 0;
- size_t columnsCount = 0;
- //rowsAndColumnsChoice(rowsCount, columnsCount);
- rowsAndColumnsSetByThePlayer(rowsCount, columnsCount);
- size_t farRightAngle = rowsCount * 2 + columnsCount * 2 - 4;
- size_t farLeftAngle = rowsCount * 2 + columnsCount - 3;
- system("cls");
- // Dynamically allocating row space in the heap
- string** labyrinth = new string * [rowsCount];
- // Dynamically allocating column space in the heap
- for (size_t i = 0; i < rowsCount; i++)
- {
- labyrinth[i] = new string[columnsCount];
- }
- size_t count = 1;
- for (size_t i = 0; i < rowsCount; i++)
- {
- for (size_t j = 0; j < columnsCount; j++)
- {
- if ( !( (i > 0) && (i < rowsCount - 1) && (j > 0) && (j < columnsCount - 1) ) )
- {
- labyrinth[i][j] = to_string(count);
- count++;
- }
- }
- }
- system("cls");
- cout << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n";
- displayTheConfiguration(labyrinth, rowsCount, columnsCount);
- string start;
- string end;
- //startAndEndChoice(start, end);
- startAndEndSetByThePlayer(labyrinth, rowsCount, columnsCount, farRightAngle, farLeftAngle, start, end);
- /*
- int wallsCount = 0;
- setWallsCountChoice(wallsCount);
- */
- wallsSetByThePlayerWithoutASetWallsCount(labyrinth, rowsCount, columnsCount);
- string name;
- chooseAnAction(labyrinth, rowsCount, columnsCount, name);
- }
- void rowsAndColumnsChoice(size_t& rowsCount, size_t& columnsCount)
- {
- system("cls");
- cout << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n";
- CoutCentered("Do you want to set your own rows and columns or do you want the game to set them for you?");
- CoutCentered("The possible answers are \"myself\" for you to set them yourself or \"random\" for the game to set them for you.");
- CoutCenteredWithoutNewLine("Your answer is: ");
- string choice;
- getline(cin >> ws, choice);
- for (size_t i = 0; i < choice.length(); i++)
- {
- choice[i] = tolower(choice[i]);
- }
- if (choice == "myself")
- {
- rowsAndColumnsSetByThePlayer(rowsCount, columnsCount);
- }
- else if (choice == "random")
- {
- // Possible future update
- }
- else
- {
- system("cls");
- cout << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n";
- CoutCentered("Invalid choice!");
- this_thread::sleep_for(chrono::milliseconds(800));
- return rowsAndColumnsChoice(rowsCount, columnsCount);
- }
- }
- void rowsAndColumnsSetByThePlayer(size_t& rowsCount, size_t& columnsCount)
- {
- system("cls");
- cout << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n";
- // MIN 5
- // MAX 23
- CoutCentered("The allowed values are between 5 and 23.");
- cout << "\n" << "\n";
- CoutCenteredWithoutNewLine("Enter rows: ");
- cin >> rowsCount;
- while ( (rowsCount < 5) || (rowsCount > 23) )
- {
- system("cls");
- cout << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n";
- CoutCentered("Invalid choice!");
- CoutCentered("The allowed values are between 5 and 23.");
- CoutCenteredWithoutNewLine("Enter rows: ");
- cin >> rowsCount;
- }
- system("cls");
- cout << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n";
- CoutCentered("The allowed values are between 5 and 23.");
- cout << "\n" << "\n";
- CoutCenteredWithoutNewLine("Enter columns: ");
- cin >> columnsCount;
- while ( (columnsCount < 5) || (columnsCount > 23) )
- {
- system("cls");
- cout << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n";
- CoutCentered("Invalid choice!");
- CoutCentered("The allowed values are between 5 and 23.");
- CoutCenteredWithoutNewLine("Ener columns: ");
- cin >> columnsCount;
- }
- }
- void startAndEndChoice(int& start, int& end)
- {
- cout << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n";
- CoutCentered("Do you want to set your own start and end or you do want the game to set them for you?");
- CoutCentered("The possible answers are \"myself\" for you to set them yourself or \"random\" for the game to set them for you.");
- CoutCenteredWithoutNewLine("Your answer is: ");
- string choice;
- getline(cin >> ws, choice);
- for (size_t i = 0; i < choice.length(); i++)
- {
- choice[i] = tolower(choice[i]);
- }
- if (choice == "myself")
- {
- }
- else if (choice == "random")
- {
- }
- else
- {
- system("cls");
- cout << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n";
- CoutCentered("Invalid choice!");
- this_thread::sleep_for(chrono::milliseconds(800));
- return startAndEndChoice(start, end);
- }
- }
- void startAndEndSetByThePlayer(string**& labyrinth, size_t& rowsCount, size_t& columnsCount, size_t& farRightAngle, size_t& farLeftAngle, string& start, string& end)
- {
- system("cls");
- cout << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n";
- CoutCentered("The starting point can't be on 1, " + to_string(rowsCount) + ", " + to_string(farLeftAngle) + " and " + to_string(farRightAngle) + ".");
- cout << "\n" << "\n";
- displayTheConfiguration(labyrinth, rowsCount, columnsCount);
- cout << "\n";
- string startAt;
- CoutCenteredWithoutNewLine("Choose a starting point: ");
- cin >> startAt;
- while ( (stoi(startAt) == 1) || (stoi(startAt) == farRightAngle) || (stoi(startAt) == farLeftAngle) || (stoi(startAt) == columnsCount) )
- {
- system("cls");
- CoutCentered("The starting point can't be on 1, " + to_string(rowsCount) + ", " + to_string(farLeftAngle) + " and " + to_string(farRightAngle) + ".");
- cout << "\n" << "\n";
- displayTheConfiguration(labyrinth, rowsCount, columnsCount);
- cout << "\n";
- CoutCenteredWithoutNewLine("Choose a starting point: ");
- cin >> startAt;
- }
- string currentElement;
- for (size_t i = 0; i < rowsCount; i++)
- {
- for (size_t j = 0; j < columnsCount; j++)
- {
- currentElement = labyrinth[i][j];
- if (startAt == labyrinth[i][j])
- {
- labyrinth[i][j] = "S";
- }
- }
- }
- system("cls");
- cout << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n";
- CoutCentered("The ending point can't be on 1, " + to_string(rowsCount) + ", " + to_string(farLeftAngle) + " and " + to_string(farRightAngle) + ".");
- cout << "\n" << "\n";
- displayTheConfiguration(labyrinth, rowsCount, columnsCount);
- cout << "\n";
- string endAt;
- CoutCenteredWithoutNewLine("Choose an ending point: ");
- cin >> endAt;
- while ( (stoi(endAt) == 1) || (endAt == startAt) || (stoi(endAt) == farRightAngle) || (stoi(endAt) == farLeftAngle) || (stoi(endAt) == columnsCount) )
- {
- system("cls");
- cout << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n";
- CoutCentered("The ending point can't be on 1, " + to_string(rowsCount) + ", " + to_string(farLeftAngle) + " and " + to_string(farRightAngle) + ".");
- cout << "\n" << "\n";
- displayTheConfiguration(labyrinth, rowsCount, columnsCount);
- cout << "\n";
- CoutCenteredWithoutNewLine("Choose an ending point: ");
- cin >> endAt;
- }
- for (size_t i = 0; i < rowsCount; i++)
- {
- for (size_t j = 0; j < columnsCount; j++)
- {
- if (endAt == labyrinth[i][j])
- {
- labyrinth[i][j] = "E";
- }
- }
- }
- }
- int setWallsCountChoice(int& wallsCount)
- {
- cout << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n";
- CoutCentered("Do you want to set your number of walls or do you want the game to set them for you?");
- CoutCentered("The possible answers are \"myself\" for you to set them yourself or \"random\" for the game to set them for you.");
- CoutCenteredWithoutNewLine("Your answer is: ");
- string choice;
- getline(cin >> ws, choice);
- for (size_t i = 0; i < choice.length(); i++)
- {
- choice[i] = tolower(choice[i]);
- }
- if (choice == "myself")
- {
- }
- else if (choice == "random")
- {
- }
- else
- {
- system("cls");
- cout << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n";
- CoutCentered("Invalid choice!");
- this_thread::sleep_for(chrono::milliseconds(800));
- return setWallsCountChoice(wallsCount);
- }
- return wallsCount;
- }
- void wallsSetByThePlayerWithoutASetWallsCount(string**& labyrinth, size_t& rowsCount, size_t& columnsCount)
- {
- system("cls");
- int count = 1;
- for (size_t i = 0; i < rowsCount; i++)
- {
- for (size_t j = 0; j < columnsCount; j++)
- {
- if ( (labyrinth[i][j] == "S") || (labyrinth[i][j] == "E") )
- {
- continue;
- }
- else if ( (i > 0) && (i < rowsCount - 1) && (j > 0) && (j < columnsCount - 1) )
- {
- count++;
- }
- else
- {
- labyrinth[i][j] = "#";
- }
- }
- }
- system("cls");
- cout << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n";
- CoutCentered("You can enter as many walls as you want. When you want to stop entering walls, type \"end\".");
- cout << "\n" << "\n";
- displayTheConfigurationWithCoordinates(labyrinth, rowsCount, columnsCount);
- cout << "\n";
- string currentWall;
- CoutCenteredWithoutNewLine("Enter a wall (ROWxCOLUMN): ");
- cin >> currentWall;
- for (size_t i = 0; i < currentWall.length(); i++)
- {
- currentWall[i] = tolower(currentWall[i]);
- }
- while (currentWall != "end")
- {
- for (size_t i = 0; i < rowsCount; i++)
- {
- for (size_t j = 0; j < columnsCount; j++)
- {
- if (currentWall == (to_string(i) + 'x' + to_string(j)))
- {
- labyrinth[i][j] = "#";
- }
- }
- }
- system("cls");
- cout << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n";
- CoutCentered("You can enter as many walls as you want. When you want to stop entering walls, type \"end\".");
- cout << "\n" << "\n";
- displayTheConfigurationWithCoordinates(labyrinth, rowsCount, columnsCount);
- cout << "\n";
- CoutCenteredWithoutNewLine("Enter a wall (ROWxCOLUMN): ");
- cin >> currentWall;
- for (size_t i = 0; i < currentWall.length(); i++)
- {
- currentWall[i] = tolower(currentWall[i]);
- }
- }
- }
- void chooseAnAction(string**& labyrinth, size_t& rowsCount, size_t& columnsCount, string& customMapName)
- {
- system("cls");
- cout << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n";
- displayTheConfigurationWithoutSpaces(labyrinth, rowsCount, columnsCount);
- cout << "\n" << "\n";
- CoutCentered("What do you want to do now?");
- cout << "\n";
- CoutCentered("If you want to save the map and play it, type \"both\".");
- cout << "\n";
- CoutCentered("If you want to save the map without playing it, type \"save\".");
- cout << "\n";
- CoutCentered("If you want to play the map without saving it, type \"play\".");
- cout << "\n";
- CoutCenteredWithoutNewLine("Your answer is: ");
- string choice;
- getline(cin >> ws, choice);
- for (size_t i = 0; i < choice.length(); i++)
- {
- choice[i] = tolower(choice[i]);
- }
- if (choice == "both")
- {
- saveTheMapConfiguration(labyrinth, rowsCount, columnsCount, customMapName);
- // Play sound
- PlaySound(TEXT("river.wav"), NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);
- // Set audio volume to 25%
- SetVolume(0xFFFF / 4);
- loadMapFromAFolder(customMapName, size_t(0), "custom");
- PlaySound(TEXT("running_home_to_you.wav"), NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);
- // Set audio volume to 25%
- SetVolume(0xFFFF / 4);
- mainMenu();
- }
- else if (choice == "save")
- {
- saveTheMapConfiguration(labyrinth, rowsCount, columnsCount, customMapName);
- }
- else if (choice == "play")
- {
- // Play sound
- PlaySound(TEXT("river.wav"), NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);
- // Set audio volume to 25%
- SetVolume(0xFFFF / 4);
- loadMap(labyrinth, rowsCount, columnsCount);
- PlaySound(TEXT("running_home_to_you.wav"), NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);
- // Set audio volume to 25%
- SetVolume(0xFFFF / 4);
- mainMenu();
- }
- else
- {
- system("cls");
- cout << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n";
- CoutCentered("Invalid choice!");
- this_thread::sleep_for(chrono::milliseconds(800));
- return chooseAnAction(labyrinth, rowsCount, columnsCount, customMapName);
- }
- }
- void saveTheMapConfiguration(string**& labyrinth, size_t& rowsCount, size_t& columnsCount, string& nameWithTheFolderPath)
- {
- system("cls");
- cout << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n";
- CoutCentered("Type a name for the text file. The name should contain only lowercase letters.");
- CoutCenteredWithoutNewLine("File's name is: ");
- string name;
- getline(cin >> ws, name);
- for (size_t i = 0; i < name.length(); i++)
- {
- name[i] = tolower(name[i]);
- }
- time_t current_time;
- current_time = time(NULL);
- name += "_" + to_string(current_time) + ".txt";
- nameWithTheFolderPath = "maps/custom/" + name;
- ofstream customMap(nameWithTheFolderPath);
- for (size_t i = 0; i < rowsCount; i++)
- {
- for (size_t j = 0; j < columnsCount; j++)
- {
- customMap << labyrinth[i][j];
- }
- customMap << "\n";
- }
- customMap.close();
- }
- void displayTheConfiguration(string** labyrinth, size_t rowsCount, size_t columnsCount)
- {
- size_t consoleWidth = GetConsoleWidth();
- size_t screenLeftMargin = (consoleWidth - columnsCount) / 2;
- string currentElement;
- size_t currentLength = 0;
- for (size_t i = 0; i < rowsCount; i++)
- {
- cout << setw(screenLeftMargin); // Center each row
- for (size_t j = 0; j < columnsCount; j++)
- {
- currentElement = labyrinth[i][j];
- currentLength = currentElement.length();
- if (currentElement.length() < 2)
- {
- currentElement = " " + currentElement;
- }
- if (currentElement.length() < 3)
- {
- cout << currentElement + string(3 - currentElement.length(), ' ');
- }
- else
- {
- cout << currentElement;
- }
- }
- cout << "\n";
- }
- }
- void displayTheConfigurationWithCoordinates(string** labyrinth, size_t rowsCount, size_t columnsCount)
- {
- size_t consoleWidth = GetConsoleWidth();
- size_t screenLeftMargin = (consoleWidth - columnsCount) / 2;
- string currentElement;
- size_t currentLength = 0;
- for (size_t i = 0; i < rowsCount; i++)
- {
- cout << setw(screenLeftMargin); // Center each row
- if (i == 0)
- {
- for (size_t j = 0; j < columnsCount; j++)
- {
- currentElement = to_string(j);
- currentLength = currentElement.length();
- if (currentElement.length() < 2)
- {
- if (j == 0)
- {
- currentElement = " " + currentElement + " ";
- }
- else
- {
- currentElement = " " + currentElement;
- }
- }
- if (currentElement.length() < 3)
- {
- cout << currentElement + string(3 - currentElement.length(), ' ');
- }
- else
- {
- cout << currentElement;
- }
- }
- cout << "\n";
- }
- cout << setw(screenLeftMargin); // Center each row
- for (size_t j = 0; j < columnsCount; j++)
- {
- currentElement = labyrinth[i][j];
- currentLength = currentElement.length();
- if (currentElement.length() < 2)
- {
- if (j == 0)
- {
- currentElement = " " + to_string(i) + " " + currentElement + " ";
- }
- else
- {
- currentElement = " " + currentElement;
- }
- }
- if (currentElement.length() < 3)
- {
- cout << currentElement + string(3 - currentElement.length(), ' ');
- }
- else
- {
- cout << currentElement;
- }
- }
- cout << "\n";
- }
- }
- void displayTheConfigurationWithoutSpaces(string** labyrinth, size_t rowsCount, size_t columnsCount)
- {
- for (size_t i = 0; i < rowsCount; i++)
- {
- for (size_t j = 0; j < columnsCount; j++)
- {
- if ( (labyrinth[i][j] != "S") && (labyrinth[i][j] != "#") && (labyrinth[i][j] != "E") )
- {
- labyrinth[i][j] = " ";
- }
- }
- }
- size_t consoleWidth = GetConsoleWidth();
- size_t screenLeftMargin = (consoleWidth - columnsCount) / 2;
- for (size_t i = 0; i < rowsCount; i++)
- {
- cout << setw(screenLeftMargin); // Center each row
- for (size_t j = 0; j < columnsCount; j++)
- {
- cout << labyrinth[i][j];
- }
- cout << "\n";
- }
- }
- size_t filesInFolderCount(string levelType)
- {
- size_t 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)
- {
- // Play sound
- PlaySound(TEXT("river.wav"), NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);
- // Set audio volume to 25%
- SetVolume(0xFFFF / 4);
- size_t level = 1;
- if ( (levelType != "easy") && (levelType != "normal") && (levelType != "hard") )
- {
- system("cls");
- CoutCentered("Invalid level type!");
- return;
- }
- string mapFile;
- while (level <= filesInFolderCount(levelType))
- {
- system("cls");
- mapFile = "maps/" + levelType + "/map_" + to_string(level) + ".txt";
- // Opening the text file
- loadMapFromAFolder(mapFile, level, levelType);
- level++;
- }
- system("cls");
- cout << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n";
- CoutCentered("Congratulations! You have finished the game!");
- cout << "\n";
- CoutCentered("Press \"ENTER\" to continue");
- while (true)
- {
- if (GetAsyncKeyState(VK_RETURN))
- {
- break;
- }
- }
- PlaySound(TEXT("running_home_to_you.wav"), NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);
- // Set audio volume to 25%
- SetVolume(0xFFFF / 4);
- gameCredits();
- }
- // This function centers the text within the console window.
- void CoutCentered(string text)
- {
- HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); // Get the console handle.
- CONSOLE_SCREEN_BUFFER_INFO screenInfo;
- GetConsoleScreenBufferInfo(hConsole, &screenInfo); // Get the console screen buffer info.
- size_t consoleWidth = screenInfo.dwSize.X;
- size_t textWidth = static_cast<size_t>(text.length());
- size_t leftMargin = (consoleWidth - textWidth) / 2;
- for (size_t i = 0; i < leftMargin; i++)
- {
- cout << " ";
- }
- cout << text << "\n";
- }
- // This function centers the text within the console window without new line.
- void CoutCenteredWithoutNewLine(string text)
- {
- HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); // Get the console handle.
- CONSOLE_SCREEN_BUFFER_INFO screenInfo;
- GetConsoleScreenBufferInfo(hConsole, &screenInfo); // Get the console screen buffer info.
- size_t consoleWidth = screenInfo.dwSize.X;
- size_t textWidth = static_cast<size_t>(text.length());
- size_t leftMargin = (consoleWidth - textWidth) / 2;
- for (size_t i = 0; i < leftMargin; i++)
- {
- cout << " ";
- }
- cout << text;
- }
- size_t GetConsoleWidth()
- {
- CONSOLE_SCREEN_BUFFER_INFO csbi;
- GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
- size_t columns = csbi.srWindow.Right - csbi.srWindow.Left + 2;
- return columns;
- }
- // Sets the audio volume
- void SetVolume(int volume)
- {
- DWORD newVolume = ((DWORD)volume << 16) | (DWORD)volume;
- waveOutSetVolume(NULL, newVolume);
- }
- 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