Advertisement
ElooEminem

Untitled

Nov 13th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 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. Thing(string name,int value){
  20. this->name=name;
  21. this->value=value;
  22. }
  23. string name;
  24. int value;
  25. };
  26.  
  27. class Node{
  28. public:
  29. Thing *item;
  30. Node(){
  31. this->item=NULL;
  32. }
  33. ~Node(){
  34. if(this->item)
  35. delete this->item;
  36. }
  37. };
  38.  
  39. class Graph{
  40. public:
  41. Node ***nodes;
  42. int x;
  43. int y;
  44. int height;
  45. int width;
  46. Graph(int width,int height,string *def){
  47. this->x=0;
  48. this->y=0;
  49. this->width=width;
  50. this->height=height;
  51. nodes=new Node **[height];
  52. for(int i=0;i<height;i++)
  53. for(int j=0;j<width;j++)
  54. if(def[i][j]=='X')
  55. nodes[i][j]=new Node();
  56. else
  57. nodes[i][j]=NULL;
  58. int rand_x;
  59. int rand_y;
  60. int places_items=0;
  61. for(int attempts=0;attempts<100;attempts++){
  62. rand_x=rand()%height;
  63. rand_y=rand()%width;
  64. if(nodes[rand_x][rand_y])
  65. if(!nodes[rand_x][rand_y]->item){
  66. nodes[rand_x][rand_y]->item=new Thing("Przedmiot_"+IntToString(attempts),rand());
  67. placed_items++;
  68. }
  69. if(placed_items==2)
  70. break;
  71. }
  72. }
  73. void up(){
  74. if(this->x>0)
  75. if(this->nodes[x-1][y]){
  76. this->x--;
  77. return;
  78. }
  79. cout<<"\nCannot move up.";
  80. return;
  81. }
  82. void down(){
  83. if(this->x<this->height-1)
  84. if(this->nodes[x+1][y]){
  85. this->x++;
  86. return;
  87. }
  88. cout<<"\nCannot move down.";
  89. return;
  90. }
  91. void left(){
  92. if(this->y>0)
  93. if(this->nodes[x][y-1]){
  94. this->y--;
  95. return;
  96. }
  97. cout<<"\nCannot move left.";
  98. return;
  99. }
  100. void right(){
  101. if(this->y<this->width-1)
  102. if(this->nodes[x][y+1]){
  103. this->y++;
  104. return;
  105. }
  106. cout<<"\nCannot move right.";
  107. return;
  108. }
  109. ~Graph(){
  110. for(int i=0;i<this->height;i++)
  111. for(int j=0;j<this->width;j++)
  112. if(this->nodes[i][j])
  113. delete this->nodes[i][j];
  114. }
  115. };
  116.  
  117. int main(){
  118. srand(time(0));
  119.  
  120. int height=3;
  121. int width=6;
  122.  
  123.  
  124.  
  125. getche();
  126. return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement