Advertisement
ElooEminem

Untitled

Nov 13th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3. #include <stdlib.h>
  4. #include <conio.h>
  5. #include <sstream>
  6.  
  7. using namespace std;
  8.  
  9. string IntToString(int a){
  10. string ret;
  11. stringstream elo;
  12. elo<<a;
  13. elo>>ret;
  14. return ret;
  15. }
  16.  
  17. class Thing{
  18. public:
  19. string name;
  20. int value;
  21. Thing(string name,int value){
  22. this->name=name;
  23. this->value=value;
  24. }
  25. string GetInfo(){
  26. return"\nNazwa : "+this->name+" , wartosc : "+IntToString(this->value);
  27. }
  28. };
  29.  
  30. class Node{
  31. public:
  32. Thing *item;
  33. Node(){
  34. this->item=NULL;
  35. }
  36. ~Node(){
  37. if(this->item)
  38. delete this->item;
  39. }
  40. };
  41.  
  42. class Graph{
  43. public:
  44. Node ***nodes;
  45. int x;
  46. int y;
  47. int step;
  48. int height;
  49. int width;
  50. Graph(int height,int width,string *def){
  51. this->step=0;
  52. this->x=0;
  53. this->y=0;
  54. this->height=height;
  55. this->width=width;
  56. nodes=new Node **[height];
  57. for(int i=0;i<height;i++){
  58. nodes[i]=new Node *[width];
  59. for(int j=0;j<width;j++)
  60. if(def[i][j]=='#')
  61. nodes[i][j]=new Node();
  62. else
  63. nodes[i][j]=NULL;
  64. }
  65. int rand_x;
  66. int rand_y;
  67. int placed_items=0;
  68. for(int attempts=0;attempts<100;attempts++){
  69. rand_x=rand()%height;
  70. rand_y=rand()%width;
  71. if(nodes[rand_x][rand_y])
  72. if(!nodes[rand_x][rand_y]->item){
  73. nodes[rand_x][rand_y]->item=new Thing("Przedmiot_"+IntToString(attempts),rand());
  74. placed_items++;
  75. }
  76. if(placed_items==2)
  77. break;
  78. }
  79. }
  80. void up(){
  81. if(this->x>0)
  82. if(this->nodes[x-1][y]){
  83. this->step++;
  84. this->x--;
  85. return;
  86. }
  87. cout<<"\nCannot move up.";
  88. return;
  89. }
  90. void down(){
  91. if(this->x<this->height-1)
  92. if(this->nodes[x+1][y]){
  93. this->step++;
  94. this->x++;
  95. return;
  96. }
  97. cout<<"\nCannot move down.";
  98. return;
  99. }
  100. void left(){
  101. if(this->y>0)
  102. if(this->nodes[x][y-1]){
  103. this->step++;
  104. this->y--;
  105. return;
  106. }
  107. cout<<"\nCannot move left.";
  108. return;
  109. }
  110. void right(){
  111. if(this->y<this->width-1)
  112. if(this->nodes[x][y+1]){
  113. this->step++;
  114. this->y++;
  115. return;
  116. }
  117. cout<<"\nCannot move right.";
  118. return;
  119. }
  120. void Print(){
  121. cout<<"\nIm at H.\nIt's "<<this->step<<" step.\n";
  122. if(nodes[this->x][this->y])
  123. if(nodes[this->x][this->y]->item)
  124. cout<<"\nI'm in room with an item:"<<nodes[this->x][this->y]->item->GetInfo()<<"\n";
  125. for(int i=0;i<this->height;i++){
  126. cout<<endl;
  127. for(int j=0;j<this->width;j++)
  128. if(i==this->x && j==this->y)
  129. cout<<"H";
  130. else if(this->nodes[i][j])
  131. cout<<"#";
  132. else
  133. cout<<"X";
  134. }
  135. return;
  136. }
  137. ~Graph(){
  138. for(int i=0;i<this->height;i++)
  139. for(int j=0;j<this->width;j++)
  140. if(this->nodes[i][j])
  141. delete this->nodes[i][j];
  142. }
  143. };
  144.  
  145. int main(){
  146. srand(time(0));
  147.  
  148. int height=3;
  149. int width=8;
  150.  
  151. string def[3];
  152.  
  153. def[0]="##X#X###";
  154. def[1]="X#####X#";
  155. def[2]="##X#X###";
  156.  
  157. Graph *graph=new Graph(height,width,def);
  158.  
  159. string route="essweneesnnseeneewwssee";
  160.  
  161. for(int i=0;i<route.length();i++){
  162. switch(route[i]){
  163. case 'n':
  164. graph->up();
  165. break;
  166. case 's':
  167. graph->down();
  168. break;
  169. case 'w':
  170. graph->left();
  171. break;
  172. case 'e':
  173. graph->right();
  174. break;
  175. }
  176. if(graph->nodes[graph->x][graph->y]->item)
  177. graph->Print();
  178. }
  179.  
  180. getche();
  181. return 0;
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement