Nisheeth

Dungeon Crawl

Jun 28th, 2011
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.91 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. //Main Function:
  99. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  100.  
  101. int main ()
  102. {  
  103.     //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  104.     //Variables:
  105.     //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  106.     peg_t Hero ('H',true, User);
  107.     peg_t Enemy ('X', true, PC);
  108.     peg_t Trap ('O', false, PC);
  109.     peg_t Treasure ('T', false, PC);
  110.     char Move;
  111.  
  112.  
  113.     cout << "Please enter the Dimensions of the Board you want to play on[Min - 5*5 | Max - 8*8]: " << endl;
  114.    
  115.     cout << "Height:  ";
  116.     cin >> Height;
  117.         if (Height > 8)
  118.             Height = 8;
  119.         if (Height < 5)
  120.             Height = 5;
  121.            
  122.     cout << "Width: ";
  123.     cin >> Width;
  124.         if (Width > 8)
  125.             Width = 8;
  126.         if (Width < 5)
  127.             Width = 5;
  128.  
  129.     Board.SetValues(Height,Width);
  130.    
  131.     int playh = 1, playw = 1;
  132.  
  133.     board [ (Height-1)*Width + (Width-1)] = Treasure.Peg;
  134.    
  135.     while (true)
  136.     {  
  137.         //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  138.         //board[ (Height-1)   *   Width +   (Width-1)  ] = CHARACTER
  139.         //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  140.         board[ (playh-1)  * (Width) + (playw-1)] = Hero.Peg;
  141.         Board.PrintBoard();
  142.         cout << "In What direction do you want to move:  ";
  143.         cin >> Move;
  144.         cin.sync();
  145.         switch (Move)
  146.         {
  147.         case 'W':
  148.         case 'w':
  149.             Hero.Move = Up;
  150.             break;
  151.         case 'S':
  152.         case 's':
  153.             Hero.Move = Down;
  154.             break;
  155.         case 'A':
  156.         case 'a':
  157.             Hero.Move = Left;
  158.             break;
  159.         case 'D':
  160.         case 'd':
  161.             Hero.Move = Right;
  162.             break;
  163.         }
  164.  
  165.         switch (Hero.Move)
  166.         {
  167.         case Up:
  168.             if (playh != 1)
  169.             {
  170.                 board[ (playh-1) * Width + (playw-1)] = '.';
  171.                 playh -= 1;
  172.             }
  173.             break;
  174.         case Down:
  175.             if (playh != Height)
  176.             {
  177.                 board[ (playh-1) * Width + (playw-1)] = '.';
  178.                 playh += 1;
  179.             }
  180.         case Left:
  181.             if (playw != 1)
  182.             {
  183.                 board[ (playh-1) * Width + (playw-1)] = '.';
  184.                 playw -= 1;
  185.             }
  186.             break;
  187.         case Right:
  188.             if (playw != Width)
  189.             {
  190.                 board[ (playh-1) * Width + (playw-1)] = '.';
  191.                 playw += 1;
  192.             }
  193.             break;
  194.         }
  195.  
  196.     }
  197.     cout << endl << endl;
  198.     cout  << "================================================================================";
  199.     cout << endl;
  200.     cout << "Press ENTER/RETURN to Exit . . .";
  201.     cin.sync();
  202.     cin.get();
  203.     return 0;
  204. }
Advertisement
Add Comment
Please, Sign In to add comment