Advertisement
Guest User

C++ Adventure Game Skeleton Main.cpp

a guest
Dec 8th, 2014
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.71 KB | None | 0 0
  1. /*
  2. ============================================================================
  3. ==========================Adventure Game Skeleton!==========================
  4. ============================================================================
  5.  
  6. - This has all of the basic features of a text based adventure game.
  7.  
  8. - The AI isn't very good(all randomized), but it's a start. If you want a really smart enemy, have them go directly for the
  9.     player.
  10.  
  11. - Nothing can go through walls. It will stop you if you try.
  12.  
  13. - The map can be adjusted and changed, ' ' will always be walkable ground, '#' will always be walls
  14.  
  15. - No matter what, you can have the walls anywhere and it will work (As long as there is spawn space, and spawns are in the right place)
  16.  
  17. - You could add several maps by making "doors" on specific coordinates that when you walk through them, it causes the new map to load.
  18.     This could possibly lead to a very large map system.
  19.  
  20. - The sword was added and now you can attack enemies. It goes in four directions, and only goes one unit in distance.
  21.  
  22. - A loot system is now possible. Do a check if the enemy is dead, then if you step on them and they are in fact dead,
  23.     add to the player's loot. This could possibly lead to a buying system where you can increase the range of your weapon,
  24.     amongst other things such as lives, armor, etc., etc..
  25.  
  26. - You can make an inventory system with a vector by adding loot, or bought items to it. Then you could add them to a vector
  27.     and iterate through that vector, as well as access the items.
  28.  
  29. - I added classes (Hooray!). Now you can call the member functions of the spawned enemies without having to write the code several times.
  30.  
  31. - You can now enter a number into NUM_OF_ENEMIES and it will spawn that many enemies.
  32. */
  33. #include "Includes.h"
  34.  
  35. void ShowBoard();
  36. void ClearBoard();
  37. const char Wall = '#';
  38. const char Ground = ' ';
  39. const int Length = 20;
  40. const int Height = 20;
  41. char board[Height][Length] = { {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'}, //0
  42.                                {'#', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', '#'}, //1
  43.                                {'#', ' ', '#', ' ', '#', '#', '#', '#', ' ', '#', '#', '#', '#', ' ', '#', ' ', '#', '#', ' ', '#'}, //2
  44.                                {'#', ' ', '#', ' ', '#', ' ', ' ', ' ', ' ', '#', ' ', ' ', '#', ' ', '#', ' ', ' ', '#', ' ', '#'}, //3
  45.                                {'#', ' ', '#', ' ', '#', ' ', '#', '#', '#', '#', ' ', ' ', '#', ' ', '#', '#', '#', '#', ' ', '#'}, //4
  46.                                {'#', ' ', '#', '#', '#', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'}, //5
  47.                                {'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#', '#', '#', '#', '#', ' ', '#', '#', '#', '#', '#', '#'}, //6
  48.                                {'#', ' ', '#', '#', '#', '#', '#', ' ', '#', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', '#'}, //7
  49.                                {'#', ' ', '#', ' ', ' ', ' ', ' ', ' ', '#', ' ', '#', ' ', '#', ' ', '#', ' ', '#', ' ', ' ', '#'}, //8
  50.                                {'#', ' ', '#', '#', '#', '#', '#', '#', '#', ' ', '#', ' ', '#', ' ', '#', ' ', '#', ' ', ' ', '#'}, //9
  51.                                {'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#', ' ', '#', ' ', '#', ' ', ' ', ' ', ' ', '#'}, //10
  52.                                {'#', '#', '#', ' ', '#', '#', '#', '#', '#', ' ', '#', ' ', '#', ' ', '#', ' ', '#', '#', ' ', '#'}, //11
  53.                                {'#', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', '#', ' ', '#', ' ', '#', ' ', ' ', '#', ' ', '#'}, //12
  54.                                {'#', ' ', '#', '#', '#', ' ', '#', '#', '#', ' ', '#', ' ', '#', ' ', '#', '#', ' ', '#', ' ', '#'}, //13
  55.                                {'#', ' ', '#', ' ', ' ', ' ', '#', ' ', '#', ' ', '#', ' ', '#', ' ', ' ', '#', ' ', '#', ' ', '#'}, //14
  56.                                {'#', ' ', '#', ' ', '#', ' ', '#', ' ', '#', ' ', '#', ' ', '#', '#', ' ', '#', ' ', '#', ' ', '#'}, //15
  57.                                {'#', ' ', ' ', ' ', '#', ' ', '#', ' ', '#', ' ', '#', ' ', ' ', '#', ' ', '#', ' ', '#', ' ', '#'}, //16
  58.                                {'#', ' ', '#', '#', '#', ' ', '#', ' ', '#', ' ', '#', '#', ' ', '#', '#', '#', ' ', '#', ' ', '#'}, //17
  59.                                {'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'}, //18
  60.                                {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'}};//19
  61. //                               0    1    2    3    4    5    6    7    8    9    10   11   12   13   14   15   16   17   18   19
  62.  
  63. void ShowBoard()
  64. {
  65.     for (int i = 0; i < Height; i++)
  66.     {
  67.         for (int j = 0; j < Length; j++)
  68.         {
  69.             cout << board[i][j];
  70.         }
  71.         cout << endl;
  72.     }
  73. }
  74.  
  75. class Player
  76. {
  77. public:
  78.     void Move();
  79.     void AddToGame();
  80.     void Attack();
  81.     void GetLoot();
  82.     void TakeDamage();
  83.     char Choice;
  84.     char Sign;
  85.  
  86.     int Gold;
  87.     int Health;
  88.     int PlayerX, PlayerY;
  89.  
  90.     Player (char, int, int);
  91. };
  92.  
  93. Player::Player (char a, int b, int c)
  94. {
  95.     Sign = a;
  96.     PlayerX = b;
  97.     PlayerY = c;
  98.     Gold = 0;
  99.     Health = 10;
  100. }
  101.  
  102. void Player::TakeDamage()
  103. {
  104.     this->Health -= 1;
  105. }
  106.  
  107. Player player('@', 1, 1);
  108.  
  109. class Enemy
  110. {
  111.     bool Random;
  112.     int Choice;
  113.  
  114. public:
  115.     char Sign;
  116.     int EnemyX, EnemyY;
  117.     bool IsDead;
  118.     void Move();
  119.     void AddToGame();
  120.     void RandomSpawn();
  121.     void Attack();
  122.  
  123.     Enemy (char, int, int); //There are several different ways to spawn
  124.     Enemy (int, int);       //enemies now
  125.     Enemy (char);
  126.     Enemy ();
  127. };
  128.  
  129. //These are for holding the enemies that are made and iterating through them
  130. vector<Enemy> EnemyVector;
  131. vector<Enemy>::iterator iter;
  132.  
  133. void Enemy::RandomSpawn()   //For the enemy constructors that don't have
  134. {                           //an input for the X and Y coordinates
  135.     bool ValidPosition = false;
  136.     while (ValidPosition == false)
  137.     {
  138.         int randX, randY;
  139.         randX = (rand() % 20);
  140.         randY = (rand() % 20);
  141.  
  142.         if (randX <= 2 || randY <= 2 || board[randY][randX] == Wall)
  143.         {
  144.             continue;
  145.         }
  146.  
  147.         else
  148.         {
  149.             EnemyX = randX;
  150.             EnemyY = randY;
  151.  
  152.             ValidPosition = true;
  153.         }
  154.     }
  155. }
  156.  
  157. Enemy::Enemy (char enemySign, int enemyX, int enemyY)
  158. {
  159.     Sign = enemySign;
  160.     EnemyX = enemyX;
  161.     EnemyY = enemyY;
  162.     IsDead = false;
  163.  
  164.     EnemyVector.push_back(*this);
  165. }
  166.  
  167. Enemy::Enemy (int enemyX, int enemyY)
  168. {
  169.     char Signs[] = {'!', '$', '%', '^', '&', '*', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
  170.     char randomSign = (rand() % 32);
  171.  
  172.     Sign = Signs[randomSign];
  173.  
  174.     EnemyX = enemyX;
  175.     EnemyY = enemyY;
  176.     IsDead = false;
  177.  
  178.     EnemyVector.push_back(*this);
  179. }
  180.  
  181. Enemy::Enemy (char enemySign)
  182. {
  183.     Sign = enemySign;
  184.     RandomSpawn();
  185.     IsDead = false;
  186.  
  187.     EnemyVector.push_back(*this);
  188. }
  189.  
  190. Enemy::Enemy()
  191. {
  192.     char Signs[] = {'!', '$', '%', '^', '&', '*', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
  193.     char randomSign = (rand() % 32);
  194.  
  195.     Sign = Signs[randomSign];
  196.     IsDead = false;
  197.  
  198.     RandomSpawn();
  199.  
  200.     EnemyVector.push_back(*this);
  201. }
  202.  
  203. void Enemy::AddToGame() //More so to show them on the first round
  204. {
  205.     board[EnemyY][EnemyX] = Sign;
  206. }
  207.  
  208. void Enemy::Move()
  209. {
  210.     char Finished = 'P';
  211.  
  212.     //Can't have dead enemies moving, can we?
  213.     //"this" refers to the enemy currently using this member function
  214.     while (Finished != 'R' && this->IsDead != true)
  215.     {
  216.         Choice = (rand() % 4);
  217.         //up
  218.         if (Choice == 0)
  219.         {
  220.             //A series of checks to see if the move is valid.
  221.             //Same for all moves that the enemy takes
  222.             --EnemyY;
  223.             if (board[EnemyY][EnemyX] == Wall)
  224.             {
  225.                 ++EnemyY;
  226.                 continue;
  227.             }
  228.             ++EnemyY;
  229.  
  230.             --EnemyY;
  231.             if (EnemyY == player.PlayerY && EnemyX == player.PlayerX)
  232.             {
  233.                 ++EnemyY;
  234.                 player.TakeDamage();
  235.                 break;
  236.             }
  237.             ++EnemyY;
  238.  
  239.             board[EnemyY][EnemyX] = Ground;
  240.             --EnemyY;
  241.  
  242.             board[EnemyY][EnemyX] = Sign;
  243.             Finished = 'R';
  244.         }
  245.  
  246.         //down
  247.         else if (Choice == 1)
  248.         {
  249.             ++EnemyY;
  250.             if (board[EnemyY][EnemyX] == Wall)
  251.             {
  252.                 --EnemyY;
  253.                 continue;
  254.             }
  255.             --EnemyY;
  256.  
  257.             ++EnemyY;
  258.             if (EnemyY == player.PlayerY && EnemyX == player.PlayerX)
  259.             {
  260.                 --EnemyY;
  261.                 player.TakeDamage();
  262.                 break;
  263.             }
  264.             --EnemyY;
  265.  
  266.             board[EnemyY][EnemyX] = Ground;
  267.             ++EnemyY;
  268.  
  269.             board[EnemyY][EnemyX] = Sign;
  270.             Finished = 'R';
  271.         }
  272.  
  273.         //Right
  274.         else if (Choice == 2)
  275.         {
  276.             ++EnemyX;
  277.             if (board[EnemyY][EnemyX] == Wall)
  278.             {
  279.                 --EnemyX;
  280.                 continue;
  281.             }
  282.             --EnemyX;
  283.  
  284.             --EnemyX;
  285.             if (EnemyY == player.PlayerY && EnemyX == player.PlayerX)
  286.             {
  287.                 ++EnemyX;
  288.                 player.TakeDamage();
  289.                 break;
  290.             }
  291.             ++EnemyX;
  292.  
  293.             board[EnemyY][EnemyX] = Ground;
  294.             ++EnemyX;
  295.  
  296.             board[EnemyY][EnemyX] = Sign;
  297.             Finished = 'R';
  298.         }
  299.  
  300.         //Left
  301.         else if (Choice == 3)
  302.         {
  303.             --EnemyX;
  304.             if (board[EnemyY][EnemyX] == Wall)
  305.             {
  306.                 ++EnemyX;
  307.                 continue;
  308.             }
  309.             ++EnemyX;
  310.  
  311.             ++EnemyX;
  312.             if (EnemyY == player.PlayerY && EnemyX == player.PlayerX)
  313.             {
  314.                 --EnemyX;
  315.                 player.TakeDamage();
  316.                 break;
  317.             }
  318.             --EnemyX;
  319.  
  320.             board[EnemyY][EnemyX] = Ground;
  321.             --EnemyX;
  322.  
  323.             board[EnemyY][EnemyX] = Sign;
  324.             Finished = 'R';
  325.         }
  326.     }
  327. }
  328.  
  329. void Player::GetLoot()
  330. {
  331.     Gold += 500;
  332. }
  333.  
  334. void Player::Attack()
  335. {
  336.     bool invalid = true;
  337.  
  338.     while (invalid)
  339.     {
  340.         //Up
  341.         if (Choice == 'u')
  342.         {
  343.             //Pretty much the same as the move function, except it loops through
  344.             //the enemy vector and sees if they are in the space in the attack
  345.             //direction. If they are, set them to dead.
  346.             --PlayerY;
  347.             if (board[PlayerY][PlayerX] != Wall && board[PlayerY][PlayerX] != Ground)
  348.             {
  349.                 for (iter = EnemyVector.begin(); iter != EnemyVector.end(); iter++)
  350.                 {
  351.                     if (PlayerY == iter->EnemyY && PlayerX == iter->EnemyX)
  352.                     {
  353.                         iter->IsDead = true;
  354.                         GetLoot();
  355.                     }
  356.                 }
  357.  
  358.                 ++PlayerY;
  359.                 break;
  360.             }
  361.             ++PlayerY;
  362.             break;
  363.         }
  364.  
  365.         //Down
  366.         else if (Choice == 'j')
  367.         {
  368.             ++PlayerY;
  369.             if (board[PlayerY][PlayerX] != Wall && board[PlayerY][PlayerX] != Ground)
  370.             {
  371.                 for (iter = EnemyVector.begin(); iter != EnemyVector.end(); iter++)
  372.                 {
  373.                     if (PlayerY == iter->EnemyY && PlayerX == iter->EnemyX)
  374.                     {
  375.                         iter->IsDead = true;
  376.                         GetLoot();
  377.                     }
  378.                 }
  379.  
  380.                 --PlayerY;
  381.                 break;
  382.             }
  383.             --PlayerY;
  384.             break;
  385.         }
  386.  
  387.         //Left
  388.         else if (Choice == 'h')
  389.         {
  390.             --PlayerX;
  391.             if (board[PlayerY][PlayerX] != Wall && board[PlayerY][PlayerX] != Ground)
  392.             {
  393.                 for (iter = EnemyVector.begin(); iter != EnemyVector.end(); iter++)
  394.                 {
  395.                     if (PlayerY == iter->EnemyY && PlayerX == iter->EnemyX)
  396.                     {
  397.                         iter->IsDead = true;
  398.                         GetLoot();
  399.                     }
  400.                 }
  401.  
  402.                 ++PlayerX;
  403.                 break;
  404.             }
  405.             ++PlayerX;
  406.             break;
  407.         }
  408.  
  409.         //Right
  410.         else if (Choice == 'k')
  411.         {
  412.             ++PlayerX;
  413.             if (board[PlayerY][PlayerX] != Wall && board[PlayerY][PlayerX] != Ground)
  414.             {
  415.                 for (iter = EnemyVector.begin(); iter != EnemyVector.end(); iter++)
  416.                 {
  417.                     if (PlayerY == iter->EnemyY && PlayerX == iter->EnemyX)
  418.                     {
  419.                         iter->IsDead = true;
  420.                         GetLoot();
  421.                     }
  422.  
  423.                     else
  424.                         continue;
  425.                 }
  426.  
  427.                 --PlayerX;
  428.                 break;
  429.             }
  430.             --PlayerX;
  431.             break;
  432.         }
  433.     }
  434. }
  435.  
  436. void Player::AddToGame()//More so that your character is visible on the first round
  437. {
  438.     board[PlayerY][PlayerX] = Sign;
  439. }
  440.  
  441.  
  442. void Player::Move()
  443. {
  444.     bool invalid = true;
  445.  
  446.     while (invalid)
  447.     {
  448.         //up
  449.         if (Choice == 'w')
  450.         {
  451.             //A series of checks to see if the move is valid.
  452.             --PlayerY;
  453.             if (board[PlayerY][PlayerX] == Wall)
  454.             {
  455.                 ++PlayerY;
  456.                 break;
  457.             }
  458.             ++PlayerY;
  459.  
  460.             board[PlayerY][PlayerX] = Ground;
  461.             --PlayerY;
  462.  
  463.             board[PlayerY][PlayerX] = Sign;
  464.             invalid = false;
  465.         }
  466.  
  467.         //down
  468.         else if (Choice == 's')
  469.         {
  470.             ++PlayerY;
  471.             if (board[PlayerY][PlayerX] == Wall)
  472.             {
  473.                 --PlayerY;
  474.                 break;
  475.             }
  476.             --PlayerY;
  477.  
  478.             board[PlayerY][PlayerX] = Ground;
  479.             ++PlayerY;
  480.  
  481.             board[PlayerY][PlayerX] = Sign;
  482.             invalid = false;
  483.         }
  484.  
  485.         //Right
  486.         else if (Choice == 'd')
  487.         {
  488.             ++PlayerX;
  489.             if (board[PlayerY][PlayerX] == Wall)
  490.             {
  491.                 --PlayerX;
  492.                 break;
  493.             }
  494.             --PlayerX;
  495.  
  496.             board[PlayerY][PlayerX] = Ground;
  497.             ++PlayerX;
  498.  
  499.             board[PlayerY][PlayerX] = Sign;
  500.             invalid = false;
  501.         }
  502.  
  503.         //Left
  504.         else if (Choice == 'a')
  505.         {
  506.             --PlayerX;
  507.             if (board[PlayerY][PlayerX] == Wall)
  508.             {
  509.                 ++PlayerX;
  510.                 break;
  511.             }
  512.             ++PlayerX;
  513.  
  514.             board[PlayerY][PlayerX] = Ground;
  515.             --PlayerX;
  516.  
  517.             board[PlayerY][PlayerX] = Sign;
  518.             invalid = false;
  519.         }
  520.     }
  521. }
  522.  
  523. int main()
  524. {
  525.     srand(static_cast<unsigned int>(time(0)));
  526.  
  527.     int Rounds = 0;
  528.     const int NUM_OF_ENEMIES = 10;  //You can set this to any number and it will
  529.     bool GameBool = true;           //spawn that many enemies
  530.  
  531.     Enemy * Pointer[NUM_OF_ENEMIES];//Pointer array to create new enemies. Refer to the
  532.                                     //for loop below.
  533.     for (int i = 0; i < NUM_OF_ENEMIES; i++)
  534.     {
  535.         Pointer[i] = new Enemy;
  536.     }
  537.  
  538.     for (iter = EnemyVector.begin(); iter != EnemyVector.end(); iter++)
  539.     {
  540.         iter->AddToGame();//Adding the enemies to the screen
  541.     }
  542.  
  543.     player.AddToGame();//Adding the player to the screen
  544.  
  545.     ShowBoard();//Show the board
  546.  
  547.     while (GameBool)
  548.     {
  549.         ClearScreen();//Clear the screen
  550.  
  551.         ShowBoard();//Show the board after the enemies have moved
  552.  
  553.         cout << "\nCurrent health: " << player.Health << endl;
  554.         cout << "Current gold: " << player.Gold << endl;
  555.         cout << "\nWhat would you like to do?\nW - Move Up\nS - Move Down\nA - Move Left\nD - Move Right";
  556.         cout << "\nU - Attack Up\nJ - Attack Down\nH - Attack Left\nK - Attack Right\n";
  557.  
  558.         cin >> player.Choice;
  559.  
  560.         switch (player.Choice)  //You can pick any valid move or attack without
  561.         {                       //having to deal with a cout asking if you want
  562.         case 'w':               //to attack or move.
  563.             player.Move();
  564.             break;
  565.  
  566.         case 's':
  567.             player.Move();
  568.             break;
  569.  
  570.         case 'a':
  571.             player.Move();
  572.             break;
  573.  
  574.         case 'd':
  575.             player.Move();
  576.             break;
  577.  
  578.         case 'u':
  579.             player.Attack();
  580.             break;
  581.  
  582.         case 'j':
  583.             player.Attack();
  584.             break;
  585.  
  586.         case 'h':
  587.             player.Attack();
  588.             break;
  589.  
  590.         case 'k':
  591.             player.Attack();
  592.             break;
  593.  
  594.         }
  595.  
  596.         for (iter = EnemyVector.begin(); iter != EnemyVector.end(); iter++)
  597.         {
  598.             iter->Move();//Move all of the enemies
  599.         }
  600.  
  601.         //Check if all enemies are dead
  602.         for (iter = EnemyVector.begin(); iter != EnemyVector.end(); iter++)
  603.         {
  604.             if (iter->IsDead == false)
  605.                 break;//If not, exit the loop
  606.  
  607.             else if (iter == (EnemyVector.end() -1) && iter->IsDead == true)
  608.             {
  609.                 GameBool = false;   //If so, set the GameBool to false and exit the
  610.             }                       //main while loop
  611.         }
  612.  
  613.         if (player.Health <= 0)
  614.             GameBool = false;
  615.  
  616.         ++Rounds;
  617.     }
  618.  
  619.     for (int i = 0; i < NUM_OF_ENEMIES; i++)
  620.     {
  621.         delete Pointer[i];//delete the enemies that were made
  622.         Pointer[i] = NULL;
  623.     }
  624.  
  625.     if (player.Health == 0)
  626.         cout << "\nYou died!\n";                        //You Lost!!
  627.     else
  628.     {
  629.         cout << "\nYou Defeated All Of The Enemies!\n";//You won!!
  630.         cout << "Your score is " << (player.Gold / Rounds) * player.Health;
  631.     }
  632.  
  633.     _getch();//Because system("PAUSE") is a bad thing to use
  634.  
  635.     return 0;
  636. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement