PIBogdanov

The Labyrinth

Jan 25th, 2023
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 27.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <Windows.h>
  4. #include <ctype.h>
  5. #include <random>
  6. #include <chrono>
  7. #include <thread>
  8. #include <conio.h>
  9.  
  10. using namespace std;
  11.  
  12. //#define rowsCount 5
  13. //#define colsCount 7
  14.  
  15. const int rowsCount = 10;
  16. const int colsCount = 10;
  17.  
  18. string boardSquareCurrentState[rowsCount][colsCount] =
  19. {
  20. { "1", "2", "3", "4", "5", "6", "7" },
  21. { "8", "9", "10", "11", "12", "13", "14" },
  22. { "15", "16", "17", "18", "19", "20", "21" },
  23. { "22", "23", "24", "25", "26", "27", "28" },
  24. { "29", "30", "31", "32", "33", "34", "35" }
  25. };
  26.  
  27. string boardSquareInitialState[rowsCount][colsCount] =
  28. {
  29. { "1", "2", "3", "4", "5", "6", "7" },
  30. { "8", "9", "10", "11", "12", "13", "14" },
  31. { "15", "16", "17", "18", "19", "20", "21" },
  32. { "22", "23", "24", "25", "26", "27", "28" },
  33. { "29", "30", "31", "32", "33", "34", "35" }
  34. };
  35.  
  36. string wallsArray[] =
  37. {
  38. "1", "2", "3", "4", "5", "6", "7",
  39. "8", "9", "10", "11", "12", "13", "14",
  40. "15", "16", "17", "18", "19", "20", "21",
  41. "22", "23", "24", "25", "26", "27", "28",
  42. "29", "30", "31", "32", "33", "34", "35"
  43. };
  44.  
  45. string labyrinth[rowsCount][colsCount] =
  46. {
  47. { "#", "#", "#", "#", "#", "#", "#", "#", "#", "#" },
  48. { "S", " ", " ", " ", " ", " ", " ", " ", " ", "#" },
  49. { "#", " ", "#", "#", "#", "#", " ", "#", " ", "#" },
  50. { "#", " ", " ", " ", " ", " ", " ", "#", " ", "#" },
  51. { "#", " ", "#", " ", "#", " ", " ", "#", " ", "#" },
  52. { "#", " ", " ", " ", "#", " ", " ", "#", " ", "#" },
  53. { "#", " ", "#", "#", "#", " ", "#", "#", " ", "#" },
  54. { "#", " ", " ", " ", " ", " ", "#", " ", " ", "#" },
  55. { "#", " ", " ", " ", " ", " ", " ", " ", " ", "E" },
  56. { "#", "#", "#", "#", "#", "#", "#", "#", "#", "#" }
  57. };
  58.  
  59. //string a[10][rowsCount][colsCount] =
  60. //{
  61. // {
  62. // { "1", "2", "3", "4", "5", "6", "7" },
  63. // { "8", "9", "10", "11", "12", "13", "14" },
  64. // { "15", "16", "17", "18", "19", "20", "21" },
  65. // { "22", "23", "24", "25", "26", "27", "28" },
  66. // { "29", "30", "31", "32", "33", "34", "35" }
  67. // },
  68. //
  69. // {
  70. // { "1", "2", "3", "4", "5", "6", "7" },
  71. // { "8", "9", "10", "11", "12", "13", "14" },
  72. // { "15", "16", "17", "18", "19", "20", "21" },
  73. // { "22", "23", "24", "25", "26", "27", "28" },
  74. // { "29", "30", "31", "32", "33", "34", "35" }
  75. // },
  76. //
  77. // {
  78. // { "1", "2", "3", "4", "5", "6", "7" },
  79. // { "8", "9", "10", "11", "12", "13", "14" },
  80. // { "15", "16", "17", "18", "19", "20", "21" },
  81. // { "22", "23", "24", "25", "26", "27", "28" },
  82. // { "29", "30", "31", "32", "33", "34", "35" }
  83. // }
  84. //};
  85.  
  86. int numberOfWalls, x, y, lastx, lasty;
  87.  
  88. string player = "O";
  89.  
  90. int playerX = 1;
  91.  
  92. int playerY = 0;
  93.  
  94. int mainFunction();
  95.  
  96. int difficulty();
  97.  
  98. void easyDifficulty();
  99.  
  100. void normalDifficulty();
  101.  
  102. void hardDifficulty();
  103.  
  104. void gameStart();
  105.  
  106. void customGame();
  107.  
  108. void gameControls();
  109.  
  110. void gameCredits();
  111.  
  112. void exitProgram();
  113.  
  114. void backToMenu();
  115.  
  116. //void board(string input[rowsCount][colsCount], string output[rowsCount][colsCount]);
  117.  
  118. void board();
  119.  
  120. void drawColums();
  121.  
  122. void startPoint();
  123. string startingPoint;
  124.  
  125. void endPoint();
  126. string endingPoint;
  127.  
  128. void typeOfWalls();
  129.  
  130. int typeNumberOfWalls();
  131.  
  132. void generateWalls();
  133.  
  134. void generateWallsWithRandomNumberOfWalls();
  135.  
  136. void generateWallsWithRandomSetWalls();
  137.  
  138. void wallReset();
  139.  
  140. int randomShuffle();
  141.  
  142. int isInputValid(string currentInput, string lastInput);
  143.  
  144. int isDigitValid();
  145.  
  146. void ShowConsoleCursor(bool showFlag);
  147.  
  148. void move(int& , int& , int , int );
  149.  
  150. void setValueAtPosition(string input, string symbols);
  151.  
  152. typedef void(*menuOption)();
  153.  
  154. menuOption menuOptions[] = { gameStart, customGame, gameControls, gameCredits, exitProgram };
  155.  
  156. int main()
  157. {
  158. system("color 04");
  159.  
  160. ShowWindow(GetConsoleWindow(), SW_MAXIMIZE);
  161.  
  162. SendMessage(GetConsoleWindow(), WM_SYSKEYDOWN, VK_RETURN, 0x2000000);
  163.  
  164. ShowConsoleCursor(false);
  165.  
  166. string menu[5] = { "Start Game", "Create a Custom Game", "Controls", "Credits", "Exit" };
  167.  
  168. int pointer = 0;
  169.  
  170. bool lastUpKeyState = false;
  171.  
  172. bool lastDownKeyState = false;
  173.  
  174. bool lastReturnKeyState = false;
  175.  
  176. bool arrowVisible = true;
  177.  
  178. while (true)
  179. {
  180. system("cls");
  181.  
  182. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
  183.  
  184. cout << "The Labyrinth" << "\n" << "\n";
  185.  
  186. cout << "Main Menu" << "\n" << "\n";
  187.  
  188. for (int i = 0; i < 5; i++)
  189. {
  190. if (i == pointer)
  191. {
  192. if (arrowVisible)
  193. {
  194. cout << "-> ";
  195.  
  196. arrowVisible = false;
  197. }
  198. else
  199. {
  200. cout << " "; // Prints 4 spaces to cover the previous "-> "
  201.  
  202. arrowVisible = true;
  203. }
  204.  
  205. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN);
  206.  
  207. cout << menu[i] << endl;
  208. }
  209.  
  210. else
  211. {
  212. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
  213.  
  214. cout << menu[i] << endl;
  215. }
  216. }
  217.  
  218. bool upKeyState = GetAsyncKeyState(VK_UP);
  219.  
  220. if (upKeyState && !lastUpKeyState)
  221. {
  222. pointer -= 1;
  223.  
  224. if (pointer == -1)
  225. {
  226. pointer = 4;
  227. }
  228. }
  229.  
  230. lastUpKeyState = upKeyState;
  231.  
  232. bool downKeyState = GetAsyncKeyState(VK_DOWN);
  233.  
  234. if (downKeyState && !lastDownKeyState)
  235. {
  236. pointer += 1;
  237.  
  238. if (pointer == 5)
  239. {
  240. pointer = 0;
  241. }
  242. }
  243.  
  244. lastDownKeyState = downKeyState;
  245.  
  246. bool returnKeyState = GetAsyncKeyState(VK_RETURN);
  247.  
  248. if (returnKeyState && !lastReturnKeyState)
  249. {
  250. menuOptions[pointer]();
  251. }
  252.  
  253. lastReturnKeyState = returnKeyState;
  254.  
  255. this_thread::sleep_for(chrono::milliseconds(200));
  256. }
  257.  
  258. return 0;
  259. }
  260.  
  261. /*int mainFunction()
  262. {
  263. startPoint();
  264.  
  265. endPoint();
  266.  
  267. typeOfWalls();
  268.  
  269. int isValid = -1;
  270.  
  271. int i = 0;
  272.  
  273. string choice = "Start";
  274.  
  275. string lastInput = "";
  276.  
  277. cout << endl << endl;
  278.  
  279. do
  280. {
  281. board(boardSquareInitialState, boardSquareCurrentState);
  282.  
  283. cout << "\n" << "\n";
  284.  
  285. cout << "The used blocks will be marked with XX" << endl << endl;
  286.  
  287. cout << "Enter an integer number between 1 and 35, excluding the numbers, that were chosen for walls!" << "\n" << "\n";
  288.  
  289. cout << "REPEATING IS FORBIDDEN!" << "\n" << "\n";
  290.  
  291. cout << "Restart buttons:" << "\n";
  292. cout << "Restart / restart / R / r" << "\n" << "\n";
  293.  
  294. cout << "Quit buttons:" << "\n";
  295. cout << "Quit / quit / Q / q" << "\n" << "\n";
  296.  
  297. cout << "Your starting point is " << startingPoint << ". You need to enter the labyrinth from there." << "\n";
  298. cout << "Your ending point is " << endingPoint << ". You need to exit the labyrinth from there." << "\n" << "\n";
  299.  
  300.  
  301. cout << "Chosen number by You: ";
  302.  
  303. cin >> choice;
  304.  
  305. for (int i = 0; i < choice.length(); i++)
  306. {
  307. choice[i] = tolower(choice[i]);
  308. }
  309.  
  310. if (choice == "restart" || choice == "r")
  311. {
  312. wallReset();
  313. lastInput = "";
  314. continue;
  315. }
  316.  
  317. if (GetAsyncKeyState('R'))
  318. {
  319. wallReset();
  320. lastInput = "";
  321. continue;
  322. }
  323.  
  324. if (choice == "quit" || choice == "q")
  325. {
  326. return 1; //NOTE TO BE FIXED!: back to setting start and the rest
  327. }
  328.  
  329. if (GetAsyncKeyState('Q'))
  330. {
  331. return 1;
  332. }
  333.  
  334. isValid = isInputValid(choice, lastInput);
  335.  
  336. if (isValid == -1)
  337. {
  338. i = -1;
  339. }
  340.  
  341. else if (isValid == 0)
  342. {
  343. i = 0;
  344. }
  345.  
  346. else
  347. {
  348. setValueAtPosition(choice, "XX");
  349.  
  350. if (choice == endingPoint)
  351. {
  352. i = 1;
  353. }
  354.  
  355. else
  356. {
  357. i = -1;
  358.  
  359. lastInput = choice;
  360. }
  361. }
  362.  
  363. } while (i == -1);
  364.  
  365. if (i != 0)
  366. {
  367. board(boardSquareInitialState, boardSquareCurrentState);
  368. }
  369.  
  370. if (i == 1)
  371. {
  372. cout << "\n" << "\n";
  373.  
  374. cout << "Good job! You found the way out!" << "\n";
  375.  
  376. cin.ignore();
  377.  
  378. cin.get();
  379.  
  380. gameCredits();
  381. }
  382. }*/
  383.  
  384. using menuOption = void(*)();
  385.  
  386. menuOption menuDifficulties[] = { easyDifficulty, normalDifficulty, hardDifficulty };
  387.  
  388. int difficulty()
  389. {
  390. string difficulty[3] = { "Easy", "Normal", "Hard" };
  391.  
  392. int pointer = 0;
  393.  
  394. bool lastUpKeyState = false;
  395.  
  396. bool lastDownKeyState = false;
  397.  
  398. bool lastReturnKeyState = false;
  399.  
  400. bool arrowVisible = true;
  401.  
  402. while (true)
  403. {
  404. system("cls");
  405.  
  406. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
  407.  
  408. cout << "Choose difficulty" << "\n" << "\n";
  409.  
  410. for (int i = 0; i < 3; i++)
  411. {
  412. if (i == pointer)
  413. {
  414. if (arrowVisible)
  415. {
  416. cout << "-> ";
  417.  
  418. arrowVisible = false;
  419. }
  420. else
  421. {
  422. cout << " "; // Prints 4 spaces to cover the previous "-> "
  423.  
  424. arrowVisible = true;
  425. }
  426.  
  427. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN);
  428.  
  429. cout << difficulty[i] << endl;
  430. }
  431.  
  432. else
  433. {
  434. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
  435.  
  436. cout << difficulty[i] << endl;
  437. }
  438. }
  439.  
  440. bool upKeyState = GetAsyncKeyState(VK_UP);
  441.  
  442. if (upKeyState && !lastUpKeyState)
  443. {
  444. pointer -= 1;
  445.  
  446. if (pointer == -1)
  447. {
  448. pointer = 2;
  449. }
  450. }
  451.  
  452. lastUpKeyState = upKeyState;
  453.  
  454. bool downKeyState = GetAsyncKeyState(VK_DOWN);
  455.  
  456. if (downKeyState && !lastDownKeyState)
  457. {
  458. pointer += 1;
  459.  
  460. if (pointer == 3)
  461. {
  462. pointer = 0;
  463. }
  464. }
  465.  
  466. lastDownKeyState = downKeyState;
  467.  
  468. bool returnKeyState = GetAsyncKeyState(VK_RETURN);
  469.  
  470. if (returnKeyState && !lastReturnKeyState)
  471. {
  472. menuDifficulties[pointer]();
  473. }
  474.  
  475. backToMenu();
  476.  
  477. lastReturnKeyState = returnKeyState;
  478.  
  479. this_thread::sleep_for(chrono::milliseconds(200));
  480. }
  481. }
  482.  
  483. void easyDifficulty()
  484. {
  485. string levelOneEasy[rowsCount][colsCount] =
  486. {
  487. { "1", "2", "3", "##", "5", "6", "7" },
  488. { "##", "##", "10", "##", "12", "##", "14" },
  489. { "15", "16", "17", "18", "19", "20", "21" },
  490. { "22", "##", "##", "##", "##", "##", "28" },
  491. { "29", "30", "31", "32", "33", "34", "35" }
  492. };
  493.  
  494. this_thread::sleep_for(chrono::milliseconds(1));
  495. }
  496.  
  497. void normalDifficulty()
  498. {
  499.  
  500. this_thread::sleep_for(chrono::milliseconds(1));
  501. }
  502.  
  503. void hardDifficulty()
  504. {
  505.  
  506. this_thread::sleep_for(chrono::milliseconds(1));
  507. }
  508.  
  509. void gameStart()
  510. {
  511. difficulty();
  512.  
  513. this_thread::sleep_for(chrono::seconds(1));
  514. }
  515.  
  516. void customGame()
  517. {
  518. system("cls");
  519.  
  520. //mainFunction();
  521.  
  522. board();
  523.  
  524. this_thread::sleep_for(chrono::seconds(1));
  525. }
  526.  
  527. void gameControls()
  528. {
  529. system("cls");
  530.  
  531. cout << "\n";
  532.  
  533. cout << " --------------CONTROLS---------------" << "\n";
  534. cout << " | |" << "\n";
  535. cout << " | |" << "\n";
  536. cout << " | ^ --> Up Arrow to move up |" << "\n";
  537. cout << " | < --> Left Arrow to move left |" << "\n";
  538. cout << " | > --> Right Arrow to move right |" << "\n";
  539. cout << " | v --> Down Arrow to move down |" << "\n";
  540. cout << " | |" << "\n";
  541. cout << " | Enter --> Enter |" << "\n";
  542. cout << " | Escape --> Back |" << "\n";
  543. cout << " | |" << "\n";
  544. cout << " | |" << "\n";
  545. cout << " -------------------------------------" << "\n";
  546.  
  547. while (true)
  548. {
  549. backToMenu();
  550. }
  551.  
  552. this_thread::sleep_for(chrono::seconds(1));
  553. }
  554.  
  555. void gameCredits()
  556. {
  557. system("cls");
  558.  
  559. cout << "Thanks for playing!" << "\n";
  560.  
  561. cout << "\n";
  562.  
  563. cout << "Creator: Petar Bogdanov" << "\n";
  564.  
  565. cout << "The people who helped me with this project: eng. Stoyan Filipov and eng. Monika Gencheva" << "\n";
  566.  
  567. while (true)
  568. {
  569. backToMenu();
  570. }
  571.  
  572. this_thread::sleep_for(chrono::seconds(1));
  573. }
  574.  
  575. void exitProgram()
  576. {
  577. exit(0);
  578. }
  579.  
  580. void backToMenu()
  581. {
  582. if (GetAsyncKeyState(VK_ESCAPE))
  583. {
  584. main();
  585. }
  586. }
  587.  
  588. /*void board(string input[rowsCount][colsCount], string output[rowsCount][colsCount])
  589. {
  590. string singleLine = "";
  591.  
  592. system("cls"); //making it only 1 board on the console, which updates itself after every input
  593.  
  594. cout << endl;
  595.  
  596. drawColums();
  597.  
  598. for (int i = 0; i < rowsCount; i++)
  599. {
  600.  
  601. singleLine = "";
  602.  
  603. for (int j = 0; j < colsCount; j++)
  604. {
  605. if (j == 0)
  606. {
  607. singleLine += " ";
  608. }
  609.  
  610. else if (j == 1)
  611. {
  612. singleLine += " | ";
  613. }
  614.  
  615. else if (j < colsCount)
  616. {
  617. singleLine += " | ";
  618. }
  619.  
  620. if
  621. (
  622. output[i][j] != "XX" && output[i][j] != "##"
  623. && output[i][j] != "SP" && output[i][j] != "EP"
  624. && stoi(output[i][j]) < 10
  625. )
  626. {
  627. singleLine += " " + output[i][j];
  628. }
  629.  
  630. else
  631. {
  632. singleLine += output[i][j];
  633. }
  634. }
  635.  
  636. cout << singleLine << endl;
  637.  
  638. if (i < (rowsCount - 1))
  639. {
  640.  
  641. cout << "__________|__________|__________|__________|__________|__________|__________" << endl;
  642. cout << " | | | | | | " << endl;
  643. }
  644.  
  645. drawColums();
  646. }
  647. }*/
  648.  
  649. void board()
  650. {
  651. bool breakAfterRender = false;
  652.  
  653. while (true)
  654. {
  655. system("cls");
  656.  
  657. int colour = rand() % 15 + 1;
  658.  
  659. for (int i = 0; i < rowsCount; i++)
  660. {
  661. for (int j = 0; j < colsCount; j++)
  662. {
  663. if (i == playerX && j == playerY)
  664. {
  665. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colour);
  666.  
  667. cout << player;
  668.  
  669. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
  670. }
  671.  
  672. else
  673. {
  674. cout << labyrinth[i][j];
  675. }
  676. }
  677.  
  678. cout << "\n";
  679. }
  680.  
  681. if (breakAfterRender)
  682. {
  683. cout << "\n";
  684.  
  685. cout << "Congratulations! You have reached the end of the labyrinth!" << "\n";
  686.  
  687. cin.get();
  688.  
  689. break;
  690. }
  691.  
  692. char key = _getch();
  693.  
  694. labyrinth[playerX][playerY] = " ";
  695.  
  696. if (key == 72) // The ASCII CODE FOR THE UP ARROW KEY
  697. {
  698. move(playerX, playerY, -1, 0);
  699. }
  700.  
  701. else if (key == 75) // The ASCII CODE FOR THE LEFT ARROW KEY
  702. {
  703. move(playerX, playerY, 0, -1);
  704. }
  705.  
  706. else if (key == 80) // The ASCII CODE FOR THE DOWN ARROW KEY
  707. {
  708. move(playerX, playerY, 1, 0);
  709. }
  710.  
  711. else if (key == 77) // The ASCII CODE FOR THE RIGHT ARROW KEY
  712. {
  713. move(playerX, playerY, 0, 1);
  714. }
  715.  
  716. if (labyrinth[playerX][playerY] == "E")
  717. {
  718. breakAfterRender = true;
  719. }
  720.  
  721. labyrinth[playerX][playerY] = player;
  722.  
  723. this_thread::sleep_for(chrono::milliseconds(10));
  724. }
  725. }
  726.  
  727. void drawColums()
  728. {
  729.  
  730. // cout << " | | | | | | " << endl;
  731.  
  732. string singleLine;
  733.  
  734. singleLine = "";
  735.  
  736. for (int j = 0; j < colsCount; j++)
  737. {
  738.  
  739. if (j == colsCount - 1)
  740. {
  741. singleLine += " ";
  742. }
  743.  
  744. else
  745. {
  746. singleLine += " |";
  747. }
  748. }
  749.  
  750. cout << singleLine << endl;
  751.  
  752. // cout << " | | | | | | " << endl;
  753.  
  754. }
  755.  
  756. void startPoint()
  757. {
  758. cout << endl << endl;
  759. cout << "Where do You want the start of the Labyrinth to be?" << "\n";
  760. cout << "Number of the start: ";
  761. cin >> startingPoint;
  762.  
  763. /*
  764. if (stoi(startingPoint) < 10)
  765. {
  766. startingPoint = "0" + startingPoint;
  767. }
  768. */
  769.  
  770. setValueAtPosition(startingPoint, "SP");
  771.  
  772. cout << "Your starting point is " << startingPoint << ". You need to enter the labyrinth from there.";
  773. }
  774.  
  775. void endPoint()
  776. {
  777. cout << endl << endl;
  778. cout << "Where do You want the end of the Labyrinth to be?" << "\n";
  779. cout << "Number of the end: ";
  780. cin >> endingPoint;
  781.  
  782. /*
  783. if (stoi(endingPoint) < 10)
  784. {
  785. endingPoint = "0" + endingPoint;
  786. }
  787. */
  788.  
  789. setValueAtPosition(endingPoint, "EP");
  790.  
  791. cout << "Your ending point is " << endingPoint << ". You need to exit the labyrinth from there." << "\n";
  792. }
  793.  
  794. void typeOfWalls()
  795. {
  796. cout << endl << endl;
  797. cout << "Do You want random generated walls or You want to placed them yourself?" << "\n";
  798. cout << "If You want random generated walls type:" << "\n";
  799. cout << "Random / random" << "\n";
  800.  
  801. cout << endl;
  802.  
  803. cout << "If You want to place them yourself type:" << "\n";
  804. cout << "Myself / myself" << "\n" << "\n";
  805.  
  806. cout << "Your answer is: ";
  807.  
  808. string answer;
  809. cin >> answer;
  810.  
  811. for (int i = 0; i < answer.length(); i++)
  812. {
  813. answer[i] = tolower(answer[i]);
  814. }
  815.  
  816. if (answer == "random")
  817. {
  818. cout << endl;
  819. cout << "Do You want random number of walls? If not you can place them yourself." << "\n";
  820. cout << "If You want random number of walls type:" << "\n";
  821. cout << "Random / random" << "\n";
  822.  
  823. cout << endl;
  824.  
  825. cout << "If You want to place them yourself type:" << "\n";
  826. cout << "Myself / myself" << "\n" << "\n";
  827.  
  828. cout << "Your answer is: ";
  829.  
  830. string answer2;
  831. cin >> answer2;
  832.  
  833. for (int i = 0; i < answer2.length(); i++)
  834. {
  835. answer2[i] = tolower(answer2[i]);
  836. }
  837.  
  838. if (answer2 == "random")
  839. {
  840. generateWallsWithRandomNumberOfWalls();
  841. }
  842.  
  843. else if (answer2 == "myself")
  844. {
  845. generateWallsWithRandomSetWalls();
  846. }
  847. }
  848.  
  849. else if (answer == "myself")
  850. {
  851. generateWalls();
  852. }
  853. }
  854.  
  855. int typeNumberOfWalls()
  856. {
  857. cout << endl << endl;
  858.  
  859. cout << "How many walls do You want the Labyrinth to have?" << "\n";
  860. cout << "Number of walls: ";
  861.  
  862. cin >> numberOfWalls;
  863.  
  864. return numberOfWalls;
  865. }
  866.  
  867. void generateWalls()
  868. {
  869. typeNumberOfWalls();
  870.  
  871. cout << "\n";
  872.  
  873. for (int i = 0; i < numberOfWalls; i++)
  874. {
  875. cout << "Which number do You want it to be a wall?" << "\n";
  876. cout << "Number: ";
  877.  
  878. int wall = isDigitValid();
  879.  
  880. cout << "\n";
  881.  
  882. /*
  883. if (stoi(wall) < 10)
  884. {
  885. wall = "0" + wall;
  886. }
  887. */
  888.  
  889. if (wall != stoi(startingPoint) && wall != stoi(endingPoint))
  890. {
  891. setValueAtPosition(to_string(wall), "##");
  892. }
  893.  
  894. else
  895. {
  896. cout << "You can't place a wall here!" << "\n";
  897.  
  898. i--;
  899.  
  900. system("pause > 0");
  901. }
  902. }
  903. }
  904.  
  905. void generateWallsWithRandomNumberOfWalls()
  906. {
  907. srand(time(NULL));
  908. int min = 8;
  909. int max = 12;
  910. int range = max - min + 1;
  911. int numberOfWalls = rand() % range + min;
  912.  
  913. for (int i = 0; i < numberOfWalls; i++)
  914. {
  915. int randomIndex = randomShuffle();
  916.  
  917. if (randomIndex != (stoi(startingPoint) - 1) && randomIndex != (stoi(endingPoint) - 1))
  918. {
  919. to_string(randomIndex);
  920. setValueAtPosition(wallsArray[randomIndex], "##");
  921. }
  922.  
  923. else
  924. {
  925. i--;
  926. }
  927. }
  928. }
  929.  
  930. void generateWallsWithRandomSetWalls()
  931. {
  932. typeNumberOfWalls();
  933.  
  934. for (int i = 0; i < numberOfWalls; i++)
  935. {
  936. int randomIndex = randomShuffle();
  937.  
  938. if (randomIndex != (stoi(startingPoint) - 1) && randomIndex != (stoi(endingPoint) - 1))
  939. {
  940. to_string(randomIndex);
  941. setValueAtPosition(wallsArray[randomIndex], "##");
  942. }
  943.  
  944. else
  945. {
  946. i--;
  947. }
  948. }
  949. }
  950.  
  951. void wallReset()
  952. {
  953. for (int i = 0; i < rowsCount; i++)
  954. {
  955. for (int j = 0; j < colsCount; j++)
  956. {
  957. if (boardSquareCurrentState[i][j] == "XX")
  958. {
  959. boardSquareCurrentState[i][j] = boardSquareInitialState[i][j];
  960. }
  961. }
  962. }
  963. }
  964.  
  965. int randomShuffle()
  966. {
  967. int min = 2;
  968. int max = 34;
  969. int range = max - min + 1;
  970. return rand() % range + min;
  971. }
  972.  
  973. int isInputValid(string currentInput, string lastInputLocal)
  974. {
  975. int foundRowIndex = 0;
  976. int foundColIndex = 0;
  977.  
  978. if (lastInputLocal == "" && currentInput != startingPoint)
  979. {
  980. cout << endl << endl;
  981.  
  982. cout << "You need to start from position " << startingPoint << "!" << endl;
  983.  
  984. system("pause > 0");
  985.  
  986. return -1;
  987. }
  988.  
  989. else if (lastInputLocal == "" && currentInput == startingPoint)
  990. {
  991. return 1;
  992. }
  993.  
  994. for (int i = 0; i < rowsCount; i++)
  995. {
  996.  
  997. for (int j = 0; j < colsCount; j++)
  998. {
  999.  
  1000. if (boardSquareInitialState[i][j] == currentInput)
  1001. {
  1002. foundRowIndex = i;
  1003. foundColIndex = j;
  1004. }
  1005. }
  1006. }
  1007.  
  1008. if (boardSquareInitialState[foundRowIndex][foundColIndex] == "##")
  1009. {
  1010.  
  1011. cout << endl << endl;
  1012. cout << "Invalid move! That's a wall!" << "\n";
  1013.  
  1014. system("pause > 0");
  1015.  
  1016. return -1;
  1017. }
  1018.  
  1019. if (boardSquareCurrentState[foundRowIndex][foundColIndex] == "XX")
  1020. {
  1021.  
  1022. cout << endl << endl;
  1023. cout << "Invalid move! You have been here! Don't repeat!" << "\n";
  1024.  
  1025. system("pause > 0");
  1026.  
  1027. return 1;
  1028. }
  1029.  
  1030. if ((foundRowIndex - 1) >= 0)
  1031. {
  1032.  
  1033. //NOTE: I have a neighbor above me - last row wasn't outside
  1034. if (boardSquareInitialState[foundRowIndex - 1][foundColIndex] == lastInputLocal)
  1035. {
  1036. return 1;
  1037. }
  1038. }
  1039.  
  1040. if ((foundRowIndex + 1) <= rowsCount)
  1041. {
  1042.  
  1043. //NOTE: I have a neighbor under me - next one isn't outside of the board
  1044. if (boardSquareInitialState[foundRowIndex + 1][foundColIndex] == lastInputLocal)
  1045. {
  1046. return 1;
  1047. }
  1048. }
  1049.  
  1050. if ((foundColIndex - 1) >= 0)
  1051. {
  1052.  
  1053. //NOTE: I have a neighbor on my left - last column wasn't outside
  1054. if (boardSquareInitialState[foundRowIndex][foundColIndex - 1] == lastInputLocal)
  1055. {
  1056. return 1;
  1057. }
  1058. }
  1059.  
  1060. if ((foundColIndex + 1) <= colsCount)
  1061. {
  1062.  
  1063. //NOTE: I have a neighbor on my left - next column isn't outside
  1064. if (boardSquareInitialState[foundRowIndex][foundColIndex + 1] == lastInputLocal)
  1065. {
  1066. return 1;
  1067. }
  1068. }
  1069.  
  1070. cout << "Invalid move! You can choose only numbers, that are near each other!" << endl;
  1071.  
  1072. system("pause > 0");
  1073.  
  1074. return -1;
  1075. }
  1076.  
  1077. int isDigitValid()
  1078. {
  1079. string wall;
  1080. cin >> wall;
  1081.  
  1082. if (isdigit(wall[0]))
  1083. {
  1084. return stoi(wall);
  1085. }
  1086.  
  1087. else
  1088. {
  1089. return isDigitValid();
  1090. }
  1091. }
  1092.  
  1093. void ShowConsoleCursor(bool showFlag)
  1094. {
  1095. HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
  1096.  
  1097. CONSOLE_CURSOR_INFO cursorInfo;
  1098.  
  1099. GetConsoleCursorInfo(out, &cursorInfo);
  1100. cursorInfo.bVisible = showFlag; // set the cursor visibility
  1101. SetConsoleCursorInfo(out, &cursorInfo);
  1102. }
  1103.  
  1104. void move(int& x, int& y, int dx, int dy)
  1105. {
  1106. if (labyrinth[x + dx][y + dy] == " " || labyrinth[x + dx][y + dy] == "E")
  1107. {
  1108. x += dx;
  1109. y += dy;
  1110. }
  1111. }
  1112.  
  1113. void setValueAtPosition(string input, string symbols)
  1114. {
  1115. for (int z = 0; z < rowsCount; z++)
  1116. {
  1117. for (int j = 0; j < colsCount; j++)
  1118. {
  1119. if (boardSquareInitialState[z][j] == input)
  1120. {
  1121. boardSquareCurrentState[z][j] = symbols;
  1122. }
  1123. }
  1124. }
  1125. }
  1126.  
  1127. /*
  1128.  
  1129. system("cls");
  1130. cout << endl << endl;
  1131.  
  1132. cout << " Labyrinth with 3 possible way outs!" << endl << endl;
  1133. cout << endl;
  1134. cout << " Start" << "\n";
  1135. cout << " | | | | | | " << endl;
  1136. cout << " " << square[1] << " | " << square[2] << " | " << square[3] << " | " << "##" << " | " << square[5] << " | " << square[6] << " | " << square[7] << endl;
  1137.  
  1138. cout << "__________|__________|__________|__________|__________|__________|__________" << endl;
  1139. cout << " | | | | | | " << endl;
  1140.  
  1141. cout << " " << " ##" << " | " << " ##" << " | " << square[10] << " | " << "##" << " | " << square[12] << " | " << "##" << " | " << square[14] << endl;
  1142.  
  1143. cout << "__________|__________|__________|__________|__________|__________|__________" << endl;
  1144. cout << " | | | | | | " << endl;
  1145.  
  1146. cout << " " << square[15] << " | " << square[16] << " | " << square[17] << " | " << square[18] << " | " << square[19] << " | " << square[20] << " | " << square[21] << endl;
  1147.  
  1148. cout << "__________|__________|__________|__________|__________|__________|__________" << endl;
  1149. cout << " | | | | | | " << endl;
  1150.  
  1151. cout << " " << square[22] << " | " << " ##" << " | " << "##" << " | " << "##" << " | " << "##" << " | " << "##" << " | " << square[28] << endl;
  1152.  
  1153. cout << "__________|__________|__________|__________|__________|__________|__________" << endl;
  1154. cout << " | | | | | | " << endl;
  1155.  
  1156. cout << " " << square[29] << " | " << square[30] << " | " << square[31] << " | " << square[32] << " | " << square[33] << " | " << square[34] << " | " << square[35] << " End" << endl;
  1157.  
  1158. cout << " | | | | | | " << endl;
  1159.  
  1160. */
Add Comment
Please, Sign In to add comment