Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <Windows.h>
- #include <ctype.h>
- #include <random>
- #include <chrono>
- #include <thread>
- using namespace std;
- #define rowsCount 5
- #define colsCount 7
- string boardSquareCurrentState[rowsCount][colsCount] =
- {
- { "1", "2", "3", "4", "5", "6", "7" },
- { "8", "9", "10", "11", "12", "13", "14" },
- { "15", "16", "17", "18", "19", "20", "21" },
- { "22", "23", "24", "25", "26", "27", "28" },
- { "29", "30", "31", "32", "33", "34", "35" }
- };
- string boardSquareInitialState[rowsCount][colsCount] =
- {
- { "1", "2", "3", "4", "5", "6", "7" },
- { "8", "9", "10", "11", "12", "13", "14" },
- { "15", "16", "17", "18", "19", "20", "21" },
- { "22", "23", "24", "25", "26", "27", "28" },
- { "29", "30", "31", "32", "33", "34", "35" }
- };
- string wallsArray[] =
- {
- "1", "2", "3", "4", "5", "6", "7",
- "8", "9", "10", "11", "12", "13", "14",
- "15", "16", "17", "18", "19", "20", "21",
- "22", "23", "24", "25", "26", "27", "28",
- "29", "30", "31", "32", "33", "34", "35"
- };
- //string a[10][rowsCount][colsCount] =
- //{
- // {
- // { "1", "2", "3", "4", "5", "6", "7" },
- // { "8", "9", "10", "11", "12", "13", "14" },
- // { "15", "16", "17", "18", "19", "20", "21" },
- // { "22", "23", "24", "25", "26", "27", "28" },
- // { "29", "30", "31", "32", "33", "34", "35" }
- // },
- //
- // {
- // { "1", "2", "3", "4", "5", "6", "7" },
- // { "8", "9", "10", "11", "12", "13", "14" },
- // { "15", "16", "17", "18", "19", "20", "21" },
- // { "22", "23", "24", "25", "26", "27", "28" },
- // { "29", "30", "31", "32", "33", "34", "35" }
- // },
- //
- // {
- // { "1", "2", "3", "4", "5", "6", "7" },
- // { "8", "9", "10", "11", "12", "13", "14" },
- // { "15", "16", "17", "18", "19", "20", "21" },
- // { "22", "23", "24", "25", "26", "27", "28" },
- // { "29", "30", "31", "32", "33", "34", "35" }
- // }
- //};
- int numberOfWalls, x, y, lastx, lasty;
- char movingObject;
- int mainFunction();
- int difficulty();
- void easyDifficulty();
- void normalDifficulty();
- void hardDifficulty();
- void gameStart();
- void customGame();
- void gameControls();
- void gameCredits();
- void exitProgram();
- void backToMenu();
- void board(string input[rowsCount][colsCount], string output[rowsCount][colsCount]);
- void drawColums();
- void startPoint();
- string startingPoint;
- void endPoint();
- string endingPoint;
- void typeOfWalls();
- int typeNumberOfWalls();
- void generateWalls();
- void generateWallsWithRandomNumberOfWalls();
- void generateWallsWithRandomSetWalls();
- void wallReset();
- int randomShuffle();
- int isInputValid(string currentInput, string lastInput);
- int isDigitValid();
- void ShowConsoleCursor(bool showFlag);
- void gotoxy(int, int);
- void setValueAtPosition(string input, string symbols);
- typedef void(*menuOption)();
- menuOption menuOptions[] = { gameStart, customGame, gameControls, gameCredits, exitProgram };
- int main()
- {
- system("color 04");
- ShowWindow(GetConsoleWindow(), SW_MAXIMIZE);
- SendMessage(GetConsoleWindow(), WM_SYSKEYDOWN, VK_RETURN, 0x2000000);
- ShowConsoleCursor(false);
- string menu[5] = { "Start Game", "Create a Custom Game", "Controls", "Credits", "Exit" };
- int pointer = 0;
- bool lastUpKeyState = false;
- bool lastDownKeyState = false;
- bool lastReturnKeyState = false;
- 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)
- {
- cout << "-> ";
- 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)
- {
- menuOptions[pointer]();
- }
- lastReturnKeyState = returnKeyState;
- this_thread::sleep_for(chrono::milliseconds(200));
- }
- return 0;
- }
- int mainFunction()
- {
- int isValid = -1;
- int i = 0;
- string choice = "Start";
- string lastInput = "";
- cout << endl << endl;
- do
- {
- board(boardSquareInitialState, boardSquareCurrentState);
- cout << "\n" << "\n";
- cout << "The used blocks will be marked with XX" << endl << endl;
- cout << "Enter an integer number between 1 and 35, excluding the numbers, that were chosen for walls!" << "\n" << "\n";
- cout << "REPEATING IS FORBIDDEN!" << "\n" << "\n";
- cout << "Restart buttons:" << "\n";
- cout << "Restart / restart / R / r" << "\n" << "\n";
- cout << "Quit buttons:" << "\n";
- cout << "Quit / quit / Q / q" << "\n" << "\n";
- cout << "Your starting point is " << startingPoint << ". You need to enter the labyrinth from there." << "\n";
- cout << "Your ending point is " << endingPoint << ". You need to exit the labyrinth from there." << "\n" << "\n";
- cout << "Chosen number by You: ";
- cin >> choice;
- for (int i = 0; i < choice.length(); i++)
- {
- choice[i] = tolower(choice[i]);
- }
- if (choice == "restart" || choice == "r")
- {
- wallReset();
- lastInput = "";
- continue;
- }
- if (GetAsyncKeyState('R'))
- {
- wallReset();
- lastInput = "";
- continue;
- }
- if (choice == "quit" || choice == "q")
- {
- return 1; //NOTE TO BE FIXED!: back to setting start and the rest
- }
- if (GetAsyncKeyState('Q'))
- {
- return 1;
- }
- /*
- if (stoi(choice) < 10)
- {
- choice = "0" + choice;
- }
- */
- isValid = isInputValid(choice, lastInput);
- if (isValid == -1)
- {
- i = -1;
- }
- else if (isValid == 0)
- {
- i = 0;
- }
- else
- {
- setValueAtPosition(choice, "XX");
- if (choice == endingPoint)
- {
- i = 1;
- }
- else
- {
- i = -1;
- lastInput = choice;
- }
- }
- } while (i == -1);
- if (i != 0)
- {
- board(boardSquareInitialState, boardSquareCurrentState);
- }
- if (i == 1)
- {
- cout << endl << endl;
- cout << "Good job! You found the way out!" << endl;
- cin.ignore();
- cin.get();
- gameCredits();
- }
- }
- using menuOption = void(*)();
- menuOption menuDifficulties[] = { easyDifficulty, normalDifficulty, hardDifficulty };
- int difficulty()
- {
- string difficulty[3] = { "Easy", "Normal", "Hard" };
- int pointer = 0;
- bool lastUpKeyState = false;
- bool lastDownKeyState = false;
- bool lastReturnKeyState = false;
- 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)
- {
- cout << "-> ";
- 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)
- {
- menuDifficulties[pointer]();
- }
- backToMenu();
- lastReturnKeyState = returnKeyState;
- this_thread::sleep_for(chrono::milliseconds(200));
- }
- }
- void easyDifficulty()
- {
- string levelOneEasy[rowsCount][colsCount] =
- {
- { "1", "2", "3", "##", "5", "6", "7" },
- { "##", "##", "10", "##", "12", "##", "14" },
- { "15", "16", "17", "18", "19", "20", "21" },
- { "22", "##", "##", "##", "##", "##", "28" },
- { "29", "30", "31", "32", "33", "34", "35" }
- };
- this_thread::sleep_for(chrono::milliseconds(1));
- }
- void normalDifficulty()
- {
- this_thread::sleep_for(chrono::milliseconds(1));
- }
- void hardDifficulty()
- {
- this_thread::sleep_for(chrono::milliseconds(1));
- }
- void gameStart()
- {
- difficulty();
- this_thread::sleep_for(chrono::seconds(1));
- }
- void customGame()
- {
- system("cls");
- startPoint();
- endPoint();
- typeOfWalls();
- this_thread::sleep_for(chrono::seconds(1));
- }
- 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 --> Enter |" << "\n";
- cout << " | Escape --> Back |" << "\n";
- cout << " | |" << "\n";
- cout << " | |" << "\n";
- cout << " -------------------------------------" << "\n";
- while (true)
- {
- backToMenu();
- }
- this_thread::sleep_for(chrono::seconds(1));
- }
- 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";
- while (true)
- {
- backToMenu();
- }
- this_thread::sleep_for(chrono::seconds(1));
- }
- void exitProgram()
- {
- exit(0);
- }
- void backToMenu()
- {
- if (GetAsyncKeyState(VK_ESCAPE))
- {
- main();
- }
- }
- void board(string input[rowsCount][colsCount], string output[rowsCount][colsCount])
- {
- string singleLine = "";
- system("cls"); //making it only 1 board on the console, which updates itself after every input
- cout << endl;
- drawColums();
- for (int i = 0; i < rowsCount; i++)
- {
- singleLine = "";
- for (int j = 0; j < colsCount; j++)
- {
- if (j == 0)
- {
- singleLine += " ";
- }
- else if (j == 1)
- {
- singleLine += " | ";
- }
- else if (j < colsCount)
- {
- singleLine += " | ";
- }
- if
- (
- output[i][j] != "XX" && output[i][j] != "##"
- && output[i][j] != "SP" && output[i][j] != "EP"
- && stoi(output[i][j]) < 10
- )
- {
- singleLine += " " + output[i][j];
- }
- else
- {
- singleLine += output[i][j];
- }
- }
- cout << singleLine << endl;
- if (i < (rowsCount - 1))
- {
- cout << "__________|__________|__________|__________|__________|__________|__________" << endl;
- cout << " | | | | | | " << endl;
- }
- drawColums();
- }
- }
- void drawColums()
- {
- // cout << " | | | | | | " << endl;
- string singleLine;
- singleLine = "";
- for (int j = 0; j < colsCount; j++)
- {
- if (j == colsCount - 1)
- {
- singleLine += " ";
- }
- else
- {
- singleLine += " |";
- }
- }
- cout << singleLine << endl;
- // cout << " | | | | | | " << endl;
- }
- void startPoint()
- {
- cout << endl << endl;
- cout << "Where do You want the start of the Labyrinth to be?" << "\n";
- cout << "Number of the start: ";
- cin >> startingPoint;
- /*
- if (stoi(startingPoint) < 10)
- {
- startingPoint = "0" + startingPoint;
- }
- */
- setValueAtPosition(startingPoint, "SP");
- cout << "Your starting point is " << startingPoint << ". You need to enter the labyrinth from there.";
- }
- void endPoint()
- {
- cout << endl << endl;
- cout << "Where do You want the end of the Labyrinth to be?" << "\n";
- cout << "Number of the end: ";
- cin >> endingPoint;
- /*
- if (stoi(endingPoint) < 10)
- {
- endingPoint = "0" + endingPoint;
- }
- */
- setValueAtPosition(endingPoint, "EP");
- cout << "Your ending point is " << endingPoint << ". You need to exit the labyrinth from there." << "\n";
- }
- void typeOfWalls()
- {
- cout << endl << endl;
- cout << "Do You want random generated walls or You want to placed them yourself?" << "\n";
- cout << "If You want random generated walls type:" << "\n";
- cout << "Random / random" << "\n";
- cout << endl;
- cout << "If You want to place them yourself type:" << "\n";
- cout << "Myself / myself" << "\n" << "\n";
- cout << "Your answer is: ";
- string answer;
- cin >> answer;
- for (int i = 0; i < answer.length(); i++)
- {
- answer[i] = tolower(answer[i]);
- }
- if (answer == "random")
- {
- cout << endl;
- cout << "Do You want random number of walls? If not you can place them yourself." << "\n";
- cout << "If You want random number of walls type:" << "\n";
- cout << "Random / random" << "\n";
- cout << endl;
- cout << "If You want to place them yourself type:" << "\n";
- cout << "Myself / myself" << "\n" << "\n";
- cout << "Your answer is: ";
- string answer2;
- cin >> answer2;
- for (int i = 0; i < answer2.length(); i++)
- {
- answer2[i] = tolower(answer2[i]);
- }
- if (answer2 == "random")
- {
- generateWallsWithRandomNumberOfWalls();
- }
- else if (answer2 == "myself")
- {
- generateWallsWithRandomSetWalls();
- }
- }
- else if (answer == "myself")
- {
- generateWalls();
- }
- }
- int typeNumberOfWalls()
- {
- cout << endl << endl;
- cout << "How many walls do You want the Labyrinth to have?" << "\n";
- cout << "Number of walls: ";
- cin >> numberOfWalls;
- return numberOfWalls;
- }
- void generateWalls()
- {
- typeNumberOfWalls();
- cout << "\n";
- for (int i = 0; i < numberOfWalls; i++)
- {
- cout << "Which number do You want it to be a wall?" << "\n";
- cout << "Number: ";
- int wall = isDigitValid();
- cout << "\n";
- /*
- if (stoi(wall) < 10)
- {
- wall = "0" + wall;
- }
- */
- if (wall != stoi(startingPoint) && wall != stoi(endingPoint))
- {
- setValueAtPosition(to_string(wall), "##");
- }
- else
- {
- cout << "You can't place a wall here!" << "\n";
- i--;
- system("pause > 0");
- }
- }
- }
- void generateWallsWithRandomNumberOfWalls()
- {
- srand(time(NULL));
- int min = 8;
- int max = 12;
- int range = max - min + 1;
- int numberOfWalls = rand() % range + min;
- for (int i = 0; i < numberOfWalls; i++)
- {
- int randomIndex = randomShuffle();
- if (randomIndex != (stoi(startingPoint) - 1) && randomIndex != (stoi(endingPoint) - 1))
- {
- to_string(randomIndex);
- setValueAtPosition(wallsArray[randomIndex], "##");
- }
- else
- {
- i--;
- }
- }
- }
- void generateWallsWithRandomSetWalls()
- {
- typeNumberOfWalls();
- for (int i = 0; i < numberOfWalls; i++)
- {
- int randomIndex = randomShuffle();
- if (randomIndex != (stoi(startingPoint) - 1) && randomIndex != (stoi(endingPoint) - 1))
- {
- to_string(randomIndex);
- setValueAtPosition(wallsArray[randomIndex], "##");
- }
- else
- {
- i--;
- }
- }
- }
- void wallReset()
- {
- for (int i = 0; i < rowsCount; i++)
- {
- for (int j = 0; j < colsCount; j++)
- {
- if (boardSquareCurrentState[i][j] == "XX")
- {
- boardSquareCurrentState[i][j] = boardSquareInitialState[i][j];
- }
- }
- }
- }
- int randomShuffle()
- {
- int min = 2;
- int max = 34;
- int range = max - min + 1;
- return rand() % range + min;
- }
- int isInputValid(string currentInput, string lastInputLocal)
- {
- int foundRowIndex = 0;
- int foundColIndex = 0;
- if (lastInputLocal == "" && currentInput != startingPoint)
- {
- cout << endl << endl;
- cout << "You need to start from position " << startingPoint << "!" << endl;
- system("pause > 0");
- return -1;
- }
- else if (lastInputLocal == "" && currentInput == startingPoint)
- {
- return 1;
- }
- for (int i = 0; i < rowsCount; i++)
- {
- for (int j = 0; j < colsCount; j++)
- {
- if (boardSquareInitialState[i][j] == currentInput)
- {
- foundRowIndex = i;
- foundColIndex = j;
- }
- }
- }
- if (boardSquareInitialState[foundRowIndex][foundColIndex] == "##")
- {
- cout << endl << endl;
- cout << "Invalid move! That's a wall!" << "\n";
- system("pause > 0");
- return -1;
- }
- if (boardSquareCurrentState[foundRowIndex][foundColIndex] == "XX")
- {
- cout << endl << endl;
- cout << "Invalid move! You have been here! Don't repeat!" << "\n";
- system("pause > 0");
- return 1;
- }
- if ((foundRowIndex - 1) >= 0)
- {
- //NOTE: I have a neighbor above me - last row wasn't outside
- if (boardSquareInitialState[foundRowIndex - 1][foundColIndex] == lastInputLocal)
- {
- return 1;
- }
- }
- if ((foundRowIndex + 1) <= rowsCount)
- {
- //NOTE: I have a neighbor under me - next one isn't outside of the board
- if (boardSquareInitialState[foundRowIndex + 1][foundColIndex] == lastInputLocal)
- {
- return 1;
- }
- }
- if ((foundColIndex - 1) >= 0)
- {
- //NOTE: I have a neighbor on my left - last column wasn't outside
- if (boardSquareInitialState[foundRowIndex][foundColIndex - 1] == lastInputLocal)
- {
- return 1;
- }
- }
- if ((foundColIndex + 1) <= colsCount)
- {
- //NOTE: I have a neighbor on my left - next column isn't outside
- if (boardSquareInitialState[foundRowIndex][foundColIndex + 1] == lastInputLocal)
- {
- return 1;
- }
- }
- cout << "Invalid move! You can choose only numbers, that are near each other!" << endl;
- system("pause > 0");
- return -1;
- }
- int isDigitValid()
- {
- string wall;
- cin >> wall;
- if (isdigit(wall[0]))
- {
- return stoi(wall);
- }
- else
- {
- return isDigitValid();
- }
- }
- 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);
- }
- void gotoxy(int eex, int eey)
- {
- COORD coord;
- coord.X = eex;
- coord.Y = eey;
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
- }
- void setValueAtPosition(string input, string symbols)
- {
- for (int z = 0; z < rowsCount; z++)
- {
- for (int j = 0; j < colsCount; j++)
- {
- if (boardSquareInitialState[z][j] == input)
- {
- boardSquareCurrentState[z][j] = symbols;
- }
- }
- }
- }
- /*
- system("cls");
- cout << endl << endl;
- cout << " Labyrinth with 3 possible way outs!" << endl << endl;
- cout << endl;
- cout << " Start" << "\n";
- cout << " | | | | | | " << endl;
- cout << " " << square[1] << " | " << square[2] << " | " << square[3] << " | " << "##" << " | " << square[5] << " | " << square[6] << " | " << square[7] << endl;
- cout << "__________|__________|__________|__________|__________|__________|__________" << endl;
- cout << " | | | | | | " << endl;
- cout << " " << " ##" << " | " << " ##" << " | " << square[10] << " | " << "##" << " | " << square[12] << " | " << "##" << " | " << square[14] << endl;
- cout << "__________|__________|__________|__________|__________|__________|__________" << endl;
- cout << " | | | | | | " << endl;
- cout << " " << square[15] << " | " << square[16] << " | " << square[17] << " | " << square[18] << " | " << square[19] << " | " << square[20] << " | " << square[21] << endl;
- cout << "__________|__________|__________|__________|__________|__________|__________" << endl;
- cout << " | | | | | | " << endl;
- cout << " " << square[22] << " | " << " ##" << " | " << "##" << " | " << "##" << " | " << "##" << " | " << "##" << " | " << square[28] << endl;
- cout << "__________|__________|__________|__________|__________|__________|__________" << endl;
- cout << " | | | | | | " << endl;
- cout << " " << square[29] << " | " << square[30] << " | " << square[31] << " | " << square[32] << " | " << square[33] << " | " << square[34] << " | " << square[35] << " End" << endl;
- cout << " | | | | | | " << endl;
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement