Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. //POP_2019_12_10_projekt_1_Topolska_Martyna_IBM_1_180002.cpp Microsoft Visual Studio Community 2017 Wersja 15.8.6
  2. #include "pch.h"
  3. #include <iostream>
  4. #include <cstdlib>
  5. #include <time.h>
  6. #include <cstdio>
  7. #include <iostream>
  8. using namespace std;
  9.  
  10. const int path = 0;
  11. const int wall = 1;
  12. const int treasure = 2;
  13. const int player = 3;
  14.  
  15.  
  16. void copy_elem(int part[][5], int map[][20], int mapx, int mapy)
  17. {
  18. int x = mapx * 5;
  19. int y = mapy * 5;
  20.  
  21. for (int i = 0; i < 5; i++)
  22. {
  23. for (int j = 0; j < 5; j++)
  24. {
  25. map[y + i][x + j] = part[i][j];
  26. }
  27. }
  28. }
  29.  
  30. void randomize_treasure(int map[][20])
  31. {
  32. srand(time(0));
  33.  
  34. for (int i = 0; i < 5; i++)
  35. {
  36. int a, b;
  37. do {
  38. a = rand() % 10;
  39. b = rand() % 20;
  40. } while (map[a][b] != 0);
  41.  
  42. map[a][b] = treasure;
  43. }
  44. }
  45.  
  46. void randomize_player(int map[][20], int* playerx, int* playery)
  47. {
  48. srand(time(0));
  49. int a, b;
  50.  
  51. do {
  52. a = rand() % 10;
  53. b = rand() % 20;
  54. } while (map[a][b] != path); //dopóki wylosowane miejsce nie będzie ścieżką
  55.  
  56. map[a][b] = player;
  57. *playerx = a;
  58. *playery = b;
  59.  
  60. }
  61. void draw(int map[][20], int *playerx, int *playery, bool visibility, int counter, int treasures_left)
  62. {
  63. system("CLS");
  64. cout << "Witaj w poszukiwaczu skarbow!" << endl << "Oto zasady: " << endl << "Aby sie poruszac uzywaj klawiatury numerycznej. 8 - w gore, 5 - w dol, 4 - w lewo, 6 - w prawo" << endl << "Aby wylaczyc ograniczone widzenie, wcisnij 0." << endl << "POWODZENIA!" << endl << endl;
  65. cout << endl << "Liczba twoich ruchow: " << counter << endl;
  66. cout << "Skarbow do zebrania pozostalo: " << treasures_left << endl;
  67.  
  68. for (int i = 0; i < 10; i++)
  69. {
  70. for (int j = 0; j < 20; j++)
  71. {
  72. if (visibility || (i >= *playerx - 2 && i <= *playerx + 2 && j >= *playery - 2 && j <= *playery + 2)) //jeżeli włączona widoczność, rysuj całą mapę, w przeciwnym wypadku otoczenie
  73. {
  74. switch (map[i][j])
  75. {
  76. case 0:
  77. printf("%c", (char)32);
  78. break;
  79. case 1:
  80. printf("%c", (char)219);
  81. break;
  82. case 2:
  83. printf("%c", (char)158);
  84. break;
  85. case 3:
  86. printf("%c", (char)79);
  87. break;
  88. }
  89. }
  90. else
  91. {
  92. printf("%c", (char)178);
  93. }
  94. }
  95. printf("\n");
  96. }
  97. }
  98.  
  99. void move(int map[][20], int *playerx, int *playery, int movex, int movey, int *counter, int *treasures_left)
  100. {
  101. if (*playerx + movex >= 0 && *playery + movey >= 0 && *playerx + movex <= 9 && *playery + movey <= 19) //czy gracz po poruszeniu się nie natrafi na krawędź
  102. {
  103. int next_tile = map[*playerx + movex][*playery + movey];
  104.  
  105. if (next_tile == treasure)
  106. {
  107. (*treasures_left)--;
  108. }
  109. if (next_tile == treasure || next_tile == path)
  110. {
  111. map[*playerx][*playery] = path;
  112. map[*playerx + movex][*playery + movey] = player;
  113. *playerx += movex;
  114. *playery += movey;
  115. (*counter)++;
  116. }
  117. }
  118. }
  119.  
  120. void get_input(int map[][20], int *playerx, int *playery, int *counter, int *treasures_left, bool *visibility)
  121. {
  122. char input;
  123. cin.read(&input, 1);
  124.  
  125. switch (input)
  126. {
  127. case 56: //góra
  128. move(map, playerx, playery, -1, 0, counter, treasures_left);
  129. break;
  130.  
  131. case 53: //dół
  132. move(map, playerx, playery, 1, 0, counter, treasures_left);
  133. break;
  134.  
  135. case 52: //lewo
  136. move(map, playerx, playery, 0, -1, counter, treasures_left);
  137. break;
  138.  
  139. case 54: //prawo
  140. move(map, playerx, playery, 0, 1, counter, treasures_left);
  141. break;
  142.  
  143. case 48: //widoczność
  144. *visibility = !*visibility;
  145. break;
  146. }
  147. }
  148.  
  149.  
  150.  
  151.  
  152.  
  153. void game_loop(int map[][20], int *playerx, int *playery)
  154. {
  155. int counter = 0;
  156. bool visibility = false;
  157. int treasures_left = 5;
  158.  
  159. while (treasures_left > 0)
  160. {
  161. draw(map, playerx, playery, visibility, counter, treasures_left);
  162. get_input(map, playerx, playery, &counter, &treasures_left, &visibility);
  163. }
  164.  
  165. draw(map, playerx, playery, visibility, counter, treasures_left);
  166. printf("Gratulacje, zebrano wszystkie skarby!");
  167. }
  168.  
  169. int main()
  170. {
  171. srand(time(0));
  172. int map[10][20];
  173. int playerx, playery;
  174. int part[6][5][5] =
  175. {
  176. {
  177. {1,1,0,1,1},
  178. {1,0,0,1,1},
  179. {0,0,1,0,0},
  180. {1,0,0,0,1},
  181. {1,1,0,1,1},
  182. },
  183. {
  184. {1,1,0,0,1},
  185. {0,1,1,0,1},
  186. {0,0,0,0,0},
  187. {1,0,0,0,1},
  188. {1,1,0,0,1},
  189. },
  190. {
  191. {0,0,0,0,0},
  192. {0,0,1,0,0},
  193. {0,0,1,1,0},
  194. {1,0,1,1,1},
  195. {1,0,0,0,0},
  196. },
  197. {
  198. {1,1,0,0,0},
  199. {1,0,0,1,0},
  200. {0,0,1,0,0},
  201. {0,1,0,0,1},
  202. {1,0,0,1,1},
  203. },
  204. {
  205. {0,0,0,1,1},
  206. {1,0,0,0,1},
  207. {0,0,1,0,0},
  208. {0,0,1,0,0},
  209. {0,0,0,0,1},
  210. },
  211. {
  212. {1,1,0,1,1},
  213. {1,1,0,1,1},
  214. {0,0,0,0,0},
  215. {1,1,0,1,1},
  216. {1,1,0,1,1}
  217. },
  218. };
  219.  
  220. for (int i = 0; i < 2; i++)
  221. {
  222. for (int j = 0; j < 4; j++)
  223. {
  224. copy_elem(part[rand() % 6], map, j, i); //wybieranie losowej czesci mapy
  225. }
  226. }
  227. randomize_treasure(map);
  228. randomize_player(map, &playerx, &playery);
  229.  
  230. game_loop(map, &playerx, &playery);
  231.  
  232.  
  233.  
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement