Advertisement
daniv1

Maze

Apr 18th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.76 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <string>
  4. #include <array>
  5. #include <random>
  6.  
  7. const int cMazeColumns = 30;
  8. const int cMazeRows = 20;
  9.  
  10. const char cEmptySymbol = ' ';
  11. const char cWallSymbol = 'X';
  12. const char cCharacterSymbol = '@';
  13. const char cExitSymbol = '#';
  14. const char cEnemySymbol = '$';
  15.  
  16. // Checks if user want so play
  17. // Return true if user wants to play; false otherwise
  18. bool doesUserWantsToPlay()
  19. {
  20.     bool rResult = false;
  21.  
  22.     std::cout << "Maze Game" << std::endl;
  23.  
  24.     while (true)
  25.     {
  26.         std::cout << "Do you want to play? (1 - yes, 0 - no):";
  27.  
  28.         std::string answer;
  29.         std::cin >> answer;
  30.  
  31.         if ((answer == "1") || (answer == "0"))
  32.         {
  33.             rResult = (answer == "1");
  34.             break;
  35.         }
  36.  
  37.         std::cout << "Sorry, I did not understand." << std::endl;
  38.     }
  39.  
  40.     return rResult;
  41. }
  42.  
  43. // Generates maze
  44. // Parameters:
  45. //maze - reference to maze field that will be modified
  46. //(walls added, added character and exit)
  47. void generateMaze(std::array<std::array<char, cMazeColumns>, cMazeRows> &prMaze)
  48. {
  49.     for (int row = 0; row < cMazeRows; row++)
  50.     {
  51.         for (int column = 0; column < cMazeColumns; column++)
  52.         {
  53.             if ((row == 0) || (row == cMazeRows - 1) || (column == 0) || (column == cMazeColumns - 1))
  54.             {
  55.                 prMaze[row][column] = cWallSymbol;
  56.             }
  57.             else
  58.             {
  59.                 prMaze[row][column] = cEmptySymbol;
  60.             }
  61.         }
  62.     }
  63.  
  64.     // Place character - always at positon 1,1
  65.     prMaze[1][1] = cCharacterSymbol;
  66.  
  67.     // Place exit randomly
  68.     static std::random_device rd;
  69.     static std::mt19937 mt{ rd() };
  70.     static std::uniform_int_distribution<int> rowGenerator = std::uniform_int_distribution<int>(2, cMazeRows - 2);
  71.     static std::uniform_int_distribution<int> columnGenerator = std::uniform_int_distribution<int>(2, cMazeColumns - 2);
  72.     prMaze[rowGenerator(mt)][columnGenerator(mt)] = cExitSymbol;
  73.     prMaze[rowGenerator(mt)][columnGenerator(mt)] = cEnemySymbol;
  74.  
  75. }
  76.  
  77. // Draws maze onto screen
  78. // Parameters:
  79. // maze - maze field to draw
  80. void drawMaze(const std::array<std::array<char, cMazeColumns>, cMazeRows> &maze)
  81. {
  82.     for (int row = 0; row < cMazeRows; row++)
  83.     {
  84.         for (int column = 0; column < cMazeColumns; column++)
  85.         {
  86.             std::cout << maze[row][column];
  87.         }
  88.  
  89.         std::cout << std::endl;
  90.     }
  91. }
  92.  
  93. // Searches given char on the given maze field,
  94. // returns if found and filling row and column with coordinates
  95. // Parameters:
  96. //      maze - maze field where character will be serched
  97. //      charToFind - char that should be found on maze field
  98. //      prCharRow - reference to row variable for which will be assigned row position
  99. //      prCharColumn - reference to column variable for which will be assigned column position
  100. // Returns true if found; false otherwise.
  101. bool scanForChar(
  102.     const std::array<std::array<char, cMazeColumns>, cMazeRows> &maze,
  103.     const char charToFind,
  104.     int &prCharRow,
  105.     int &prCharColumn)
  106. {
  107.     for (int row = 0; row < cMazeRows; row++)
  108.     {
  109.         for (int column = 0; column < cMazeColumns; column++)
  110.         {
  111.             if (maze[row][column] == charToFind)
  112.             {
  113.                 prCharRow = row;
  114.                 prCharColumn = column;
  115.                 return true;
  116.             }
  117.         }
  118.     }
  119.  
  120.     return false;
  121. }
  122.  
  123. // Moves character according to given command and retuns eaten symbol (if any)
  124. // Parameters:
  125. //      row - character row position
  126. //      column - character column position
  127. //      command - test string with given command ("l" - left, "r" - right, "u" - up, "d" - down)
  128. //      prMaze - reference to maze field; will be modified as a result of command execution
  129. char moveAndGather(int row,
  130.     int column,
  131.     const std::string &command,
  132.     std::array<std::array<char, cMazeColumns>, cMazeRows> &prMaze)
  133. {
  134.     char rCharMovedOnto = cEmptySymbol;
  135.  
  136.     // Take character out from map
  137.     prMaze[row][column] = cEmptySymbol;
  138.  
  139.     if (command == "a")
  140.     {
  141.         rCharMovedOnto = prMaze[row][column - 1];
  142.  
  143.         if (rCharMovedOnto != cWallSymbol)
  144.         {
  145.             column--;
  146.         }
  147.     }
  148.  
  149.     if (command == "d")
  150.     {
  151.         rCharMovedOnto = prMaze[row][column + 1];
  152.  
  153.         if (rCharMovedOnto != cWallSymbol)
  154.         {
  155.             column++;
  156.         }
  157.     }
  158.  
  159.     if (command == "w")
  160.     {
  161.         rCharMovedOnto = prMaze[row - 1][column];
  162.  
  163.         if (rCharMovedOnto != cWallSymbol)
  164.         {
  165.             row--;
  166.         }
  167.     }
  168.  
  169.     if (command == "s")
  170.     {
  171.         rCharMovedOnto = prMaze[row + 1][column];
  172.  
  173.         if (rCharMovedOnto != cWallSymbol)
  174.         {
  175.             row++;
  176.         }
  177.     }
  178.  
  179.     // Place character back with new position
  180.     prMaze[row][column] = cCharacterSymbol;
  181.  
  182.     return rCharMovedOnto;
  183. }
  184.  
  185. // Prints message to player.
  186. // Parameters:
  187. //      message - text message to player
  188. void gameMessage(const std::string& message)
  189. {
  190.     std::cout << std::endl << message << std::endl << std::endl;
  191. }
  192.  
  193. // Moves character and check if exit was found as a result of that move.
  194. // Parameters:
  195. // prMaze - reference to maze field; will be modified while player moves.
  196. bool moveCharacterAndCheckIfExitFound(std::array<std::array<char, cMazeColumns>, cMazeRows> &prMaze)
  197. {
  198.     bool rExitFound = false;
  199.     int charRow = 1;
  200.     int charColumn = 1;
  201.     if (scanForChar(prMaze, cCharacterSymbol, charRow, charColumn))
  202.     {
  203.         std::cout << "Command (a - left, d - right, w - up, s- down):";
  204.         std::string command;
  205.         std::cin >> command;
  206.  
  207.         const char charMovedOnto = moveAndGather(charRow, charColumn, command, prMaze);
  208.  
  209.         if (charMovedOnto == cWallSymbol)
  210.         {
  211.             gameMessage("Cannot move here!");
  212.         }
  213.  
  214.         if (charMovedOnto == cExitSymbol)
  215.         {
  216.             gameMessage("Exit found!");
  217.             rExitFound = true;
  218.         }
  219.     }
  220.     else
  221.     {
  222.         gameMessage("Error: cannot find char!");
  223.     }
  224.  
  225.     return rExitFound;
  226. }
  227.  
  228. // Executes one round of the game
  229. void playMazeGame()
  230. {
  231.     std::cout << "LETS START!" << std::endl;
  232.  
  233.     std::array<std::array<char, cMazeColumns>, cMazeRows> maze;
  234.     generateMaze(maze);
  235.  
  236.     do
  237.     {
  238.         drawMaze(maze);
  239.     } while (!moveCharacterAndCheckIfExitFound(maze));
  240. }
  241.  
  242. int main()
  243. {
  244.     while (doesUserWantsToPlay())
  245.     {
  246.         playMazeGame();
  247.     }
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement