Nisheeth

Dungeon Crawl

Jul 1st, 2011
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <sstream>
  6. #include <vector>
  7. #include <time.h>
  8.  
  9. using std::cout;
  10. using std::cin;
  11. using std::endl;
  12. using std::string;
  13. using std::vector;
  14.  
  15. enum Direction { None, Left, Right, Up, Down };
  16. enum Controller_t {PC , User};
  17.  
  18. //Global Variables:
  19. char*board;
  20. int Height, Width;
  21.  
  22. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  23. //Classes - Prototypes
  24. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  25. class board_t;
  26. class peg_t;
  27.  
  28. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  29. //Classes Decleratios
  30. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  31. class peg_t
  32. {
  33. private:
  34.     bool Moving;
  35.     Controller_t controller;
  36. public:
  37.     char Peg;
  38.     peg_t (char,bool,Controller_t);
  39.     Direction Move;
  40. };
  41.  
  42. class board_t
  43. {
  44. private:
  45.     void SetBoard(int,int);
  46. public:
  47.     void SetValues(int, int);
  48.     void PrintBoard();
  49.     friend class peg_t;
  50. }Board;
  51.  
  52.  
  53. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  54. //Class Functions:
  55. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  56. //PEG_T_FUNCTIONS:
  57.  
  58. peg_t::peg_t(const char a, bool b, Controller_t c)
  59. {
  60.     Peg = a;
  61.     Moving = b;
  62.     controller = c;
  63. }
  64.  
  65. //BOARD_T_FUNCTIONS:
  66.  
  67. void board_t::SetValues(int a, int b)
  68. {
  69.     SetBoard(a,b);
  70. }
  71.  
  72. void board_t::SetBoard(int a, int b)
  73. {
  74.     board = new char [a*b];
  75.  
  76.     for (int i = 0; i < a; i++)
  77.     {
  78.         for (int j = 0;j<b;j++)
  79.         {
  80.             board[i*b + j] = '.';
  81.         }
  82.     }
  83. }
  84.  
  85. void board_t::PrintBoard()
  86. {
  87.     for (int i = 0; i < Height; i++)
  88.     {
  89.         cout << "\t";
  90.         for (int j = 0;j < Width ;j++)
  91.         {
  92.             cout << board[i*Width + j] << " ";;
  93.         }
  94.         cout << endl << endl;
  95.     }
  96. }
  97.  
  98. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  99. //Function:
  100. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  101. void DrawLine (char x)
  102. {
  103.     cout << endl;
  104.     for (int i = 0; i < 80;i++)
  105.     {
  106.         cout << x;
  107.     }
  108.     cout << endl;
  109. }
  110.  
  111.  
  112. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  113. //Main Function:
  114. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  115.  
  116. int main ()
  117. {  
  118.     srand (time(NULL));
  119.     //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  120.     //Variables:
  121.     //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  122.     peg_t Hero ('H',true, User);
  123.     peg_t Enemy ('X', true, PC);
  124.     peg_t Trap ('O', false, PC);
  125.     peg_t Treasure ('T', false, PC);
  126.     peg_t Rival ('R', true, PC);
  127.     char Move;
  128.     bool DumVar = true;
  129.  
  130.     cout << "Please enter the Dimensions of the Board you want to play on[Min - 5*5 | Max - 8*8]: " << endl;
  131.    
  132.     cout << "Height:  ";
  133.     cin >> Height;
  134.         if (Height > 8)
  135.             Height = 8;
  136.         if (Height < 5)
  137.             Height = 5;
  138.            
  139.     cout << "Width: ";
  140.     cin >> Width;
  141.         if (Width > 8)
  142.             Width = 8;
  143.         if (Width < 5)
  144.             Width = 5;
  145.  
  146.     Board.SetValues(Height,Width);
  147.    
  148.     int playh = 1, playw = 1;
  149.  
  150.     DrawLine('*');
  151.     cout << "The target of the game is to reach the Treasure [T] without coming in contact with a Trap [O] or an Enemy[X].";
  152.     cout << "The character is controlled by W,S,A & D, that alllow the character to move Up, Down, Left and Right respectively\n";
  153.     DrawLine ('*');
  154.  
  155.     if (Height <= 6 || Width <= 6)
  156.     {
  157.         int rand1,rand2;
  158.        
  159.         for (int i = 0;i < 7; i++)
  160.         {
  161.             rand1 = rand()%Height;
  162.             rand2 = rand()%Width;
  163.             board[(rand1 * Width) + rand2] = Trap.Peg;
  164.         }
  165.     }
  166.  
  167.     else if (Height > 6 || Width > 6)
  168.     {
  169.         int rand1,rand2;
  170.        
  171.         for (int i = 0 ;i < 8; i++)
  172.         {
  173.             rand1 = rand()%Height;
  174.             rand2 = rand()%Width;
  175.             board[(rand1 * Width) + rand2] = Trap.Peg;
  176.         }
  177.     }
  178.  
  179.     board [ (Height*Width) - 1 ] = Treasure.Peg;
  180.  
  181.     board [ Width-1] = Enemy.Peg;
  182.  
  183.     board [(Height-1) * Width] = Rival.Peg;
  184.  
  185.     cout << endl;
  186.     while (DumVar)
  187.     {  
  188.         //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  189.         //board[ (Row-1)   *   Width +   (Column-1)  ] = CHARACTER
  190.         //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  191.         board[ (playh-1)  * (Width) + (playw-1)] = Hero.Peg;
  192.         Board.PrintBoard();
  193.        
  194.         cout << "In What direction do you want to move:  ";
  195.         cin >> Move;
  196.         cin.sync();
  197.         switch (Move)
  198.         {
  199.         case 'W':
  200.         case 'w':
  201.             Hero.Move = Up;
  202.             break;
  203.         case 'S':
  204.         case 's':
  205.             Hero.Move = Down;
  206.             break;
  207.         case 'A':
  208.         case 'a':
  209.             Hero.Move = Left;
  210.             break;
  211.         case 'D':
  212.         case 'd':
  213.             Hero.Move = Right;
  214.             break;
  215.         }
  216.  
  217.         switch (Hero.Move)
  218.         {
  219.         case Up:
  220.             if (playh != 1)
  221.             {
  222.                 if (board[ (playh-2) * Width + (playw-1)] == '.')
  223.                 {
  224.                     board[ (playh-1) * Width + (playw-1)] = '.';
  225.                     playh -= 1;
  226.                 }
  227.                 else if (board[ (playh-2) * Width + (playw-1)] == Treasure.Peg)
  228.                 {
  229.                     board[ (playh-1) * Width + (playw-1)] = '.';
  230.                     board[ (playh-2) * Width + (playw-1)] = Hero.Peg;
  231.                     Board.PrintBoard();
  232.                     cout << "Congratulations. You have won the Game!!!";
  233.                     DumVar = false;
  234.                 }
  235.  
  236.                 else
  237.                 {
  238.                     board[ (playh-1) * Width + (playw-1)] = '.';
  239.                     board[ (playh-2) * Width + (playw-1)] = Hero.Peg;
  240.                     Board.PrintBoard();
  241.                     cout << "You were Killed! You LOSE!!!";
  242.                     DumVar = false;
  243.                 }
  244.             }
  245.             break;
  246.  
  247.         case Down:
  248.             if (playh != Height)
  249.             {
  250.                 if (board[ (playh) * Width + (playw-1)] == '.' )
  251.                 {
  252.                     board[ (playh-1) * Width + (playw-1)] = '.';
  253.                     playh += 1;
  254.                 }
  255.                 else if (board[ (playh) * Width + (playw-1)] == Treasure.Peg)
  256.                 {
  257.                     board[ (playh-1) * Width + (playw-1)] = '.';
  258.                     board[ (playh) * Width + (playw-1)] = Hero.Peg;
  259.                     Board.PrintBoard();
  260.                     cout << "Congratulations. You have won the Game!!!";
  261.                     DumVar = false;
  262.                 }
  263.                 else
  264.                 {
  265.                     board[ (playh-1) * Width + (playw-1)] = '.';
  266.                     board[ (playh) * Width + (playw-1)] = Hero.Peg;
  267.                     Board.PrintBoard();
  268.                     cout << "You were Killed! You LOSE!!!";
  269.                     DumVar = false;
  270.                 }
  271.             }
  272.             break;
  273.  
  274.         case Left:
  275.             if (playw != 1)
  276.             {
  277.                 if (board[ (playh-1) * Width + (playw-2)] == '.' )
  278.                 {
  279.                     board[ (playh-1) * Width + (playw-1)] = '.';
  280.                     playw -= 1;
  281.                 }
  282.                 else if (board[ (playh-1) * Width + (playw-2)] == Treasure.Peg)
  283.                 {
  284.                     board[ (playh-1) * Width + (playw-1)] = '.';
  285.                     board[ (playh-1) * Width + (playw-2)] = Hero.Peg;
  286.                     Board.PrintBoard();
  287.                     cout << "Congratulations. You have won the Game!!!";
  288.                     DumVar = false;
  289.                 }
  290.                 else
  291.                 {
  292.                     board[ (playh-1) * Width + (playw-1)] = '.';
  293.                     board[ (playh-1) * Width + (playw-2)] = Hero.Peg;
  294.                     Board.PrintBoard();
  295.                     cout << "You were Killed! You LOSE!!!";
  296.                     DumVar = false;
  297.                 }
  298.             }
  299.             break;
  300.  
  301.         case Right:
  302.             if (playw != Width)
  303.             {
  304.                 if (board[ (playh-1) * Width + (playw)] == '.' )
  305.                 {
  306.                     board[ (playh-1) * Width + (playw-1)] = '.';
  307.                     playw += 1;
  308.                 }
  309.                 else if (board[ (playh-1) * Width + (playw-2)] == Treasure.Peg)
  310.                 {
  311.                     board[ (playh-1) * Width + (playw-1)] = '.';
  312.                     board[ (playh-1) * Width + (playw)] = Hero.Peg;
  313.                     Board.PrintBoard();
  314.                     cout << "Congratulations. You have won the Game!!!";
  315.                     DumVar = false;
  316.                 }
  317.                 else
  318.                 {
  319.                     board[ (playh-1) * Width + (playw-1)] = '.';
  320.                     board[ (playh-1) * Width + (playw)] = Hero.Peg;
  321.                     Board.PrintBoard();
  322.                     cout << "You were Killed! You LOSE!!!";
  323.                     DumVar = false;
  324.                 }
  325.             }
  326.             break;
  327.         }
  328.  
  329.         DrawLine('-');
  330.     }
  331.     cout << endl << endl;
  332.     DrawLine ('=');
  333.     cout << endl;
  334.     cout << "Press ENTER/RETURN to Exit . . .";
  335.     cin.sync();
  336.     cin.get();
  337.     return 0;
  338. }
Advertisement
Add Comment
Please, Sign In to add comment