Nisheeth

Dungeon Crawl

Jun 28th, 2011
1,105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <sstream>
  6. #include <vector>
  7.  
  8. using std::cout;
  9. using std::cin;
  10. using std::endl;
  11. using std::string;
  12. using std::vector;
  13.  
  14. enum Direction { None, Left, Right, Up, Down };
  15. enum Controller_t {PC , User};
  16.  
  17. //Global Variables:
  18. char*board;
  19. int Height, Width;
  20.  
  21. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  22. //Classes - Prototypes
  23. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  24. class board_t;
  25. class peg_t;
  26.  
  27. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  28. //Classes Decleratios
  29. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  30. class peg_t
  31. {
  32. private:
  33.     bool Moving;
  34.     Controller_t controller;
  35. public:
  36.     char Peg;
  37.     peg_t (char,bool,Controller_t);
  38.     Direction Move;
  39. };
  40.  
  41. class board_t
  42. {
  43. private:
  44.     void SetBoard(int,int);
  45. public:
  46.     void SetValues(int, int);
  47.     void PrintBoard();
  48.     friend class peg_t;
  49. }Board;
  50.  
  51.  
  52. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  53. //Class Functions:
  54. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  55. //PEG_T_FUNCTIONS:
  56.  
  57. peg_t::peg_t(const char a, bool b, Controller_t c)
  58. {
  59.     Peg = a;
  60.     Moving = b;
  61.     controller = c;
  62. }
  63.  
  64. //BOARD_T_FUNCTIONS:
  65.  
  66. void board_t::SetValues(int a, int b)
  67. {
  68.     SetBoard(a,b);
  69. }
  70.  
  71. void board_t::SetBoard(int a, int b)
  72. {
  73.     board = new char [a*b];
  74.  
  75.     for (int i = 0; i < a; i++)
  76.     {
  77.         for (int j = 0;j<b;j++)
  78.         {
  79.             board[i*b + j] = '.';
  80.         }
  81.     }
  82. }
  83.  
  84. void board_t::PrintBoard()
  85. {
  86.     for (int i = 0; i < Height; i++)
  87.     {
  88.         cout << "\t";
  89.         for (int j = 0;j < Width ;j++)
  90.         {
  91.             cout << board[i*Width + j];
  92.         }
  93.         cout << endl;
  94.     }
  95. }
  96.  
  97. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  98. //Function:
  99. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  100. void DrawLine (char x)
  101. {
  102.     cout << endl;
  103.     for (int i = 0; i < 80;i++)
  104.     {
  105.         cout << x;
  106.     }
  107.     cout << endl;
  108. }
  109.  
  110.  
  111. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  112. //Main Function:
  113. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  114.  
  115. int main ()
  116. {  
  117.     //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  118.     //Variables:
  119.     //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  120.     peg_t Hero ('H',true, User);
  121.     peg_t Enemy ('X', true, PC);
  122.     peg_t Trap ('O', false, PC);
  123.     peg_t Treasure ('T', false, PC);
  124.     char Move;
  125.  
  126.  
  127.     cout << "Please enter the Dimensions of the Board you want to play on[Min - 5*5 | Max - 8*8]: " << endl;
  128.    
  129.     cout << "Height:  ";
  130.     cin >> Height;
  131.         if (Height > 8)
  132.             Height = 8;
  133.         if (Height < 5)
  134.             Height = 5;
  135.            
  136.     cout << "Width: ";
  137.     cin >> Width;
  138.         if (Width > 8)
  139.             Width = 8;
  140.         if (Width < 5)
  141.             Width = 5;
  142.  
  143.     Board.SetValues(Height,Width);
  144.    
  145.     int playh = 1, playw = 1;
  146.  
  147.     board [ (Height*Width) - 1 ] = Treasure.Peg;
  148.    
  149.     DrawLine('*');
  150.     cout << "The target of the game is to reach the Treasure [T] without coming in contact with a Trap [O] or an Enemy[X].";
  151.     cout << "The character is controlled by W,S,A & D, that alllow the character to move Up, Down, Left and Right respectively\n";
  152.     DrawLine ('*');
  153.  
  154.     cout << endl;
  155.     while (true)
  156.     {  
  157.         //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  158.         //board[ (Row-1)   *   Width +   (Column-1)  ] = CHARACTER
  159.         //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  160.         board[ (playh-1)  * (Width) + (playw-1)] = Hero.Peg;
  161.         Board.PrintBoard();
  162.        
  163.         if (board [(Height*Width)-1] != Treasure.Peg)
  164.         {
  165.             cout << "Congratulations. You have won the Game!!!";
  166.             break;
  167.         }
  168.  
  169.         cout << "In What direction do you want to move:  ";
  170.         cin >> Move;
  171.         cin.sync();
  172.         switch (Move)
  173.         {
  174.         case 'W':
  175.         case 'w':
  176.             Hero.Move = Up;
  177.             break;
  178.         case 'S':
  179.         case 's':
  180.             Hero.Move = Down;
  181.             break;
  182.         case 'A':
  183.         case 'a':
  184.             Hero.Move = Left;
  185.             break;
  186.         case 'D':
  187.         case 'd':
  188.             Hero.Move = Right;
  189.             break;
  190.         }
  191.  
  192.         switch (Hero.Move)
  193.         {
  194.         case Up:
  195.             if (playh != 1)
  196.             {
  197.                 board[ (playh-1) * Width + (playw-1)] = '.';
  198.                 playh -= 1;
  199.             }
  200.             break;
  201.  
  202.         case Down:
  203.             if (playh != Height)
  204.             {
  205.                 board[ (playh-1) * Width + (playw-1)] = '.';
  206.                 playh += 1;
  207.             }
  208.             break;
  209.  
  210.         case Left:
  211.             if (playw != 1)
  212.             {
  213.                 board[ (playh-1) * Width + (playw-1)] = '.';
  214.                 playw -= 1;
  215.             }
  216.             break;
  217.  
  218.         case Right:
  219.             if (playw != Width)
  220.             {
  221.                 board[ (playh-1) * Width + (playw-1)] = '.';
  222.                 playw += 1;
  223.             }
  224.             break;
  225.         }
  226.  
  227.         DrawLine('-');
  228.     }
  229.     cout << endl << endl;
  230.     DrawLine ('=');
  231.     cout << endl;
  232.     cout << "Press ENTER/RETURN to Exit . . .";
  233.     cin.sync();
  234.     cin.get();
  235.     return 0;
  236. }
Advertisement
Add Comment
Please, Sign In to add comment