Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.21 KB | None | 0 0
  1. /******************************************************************************
  2.  
  3. Online C Compiler.
  4. Code, Compile, Run and Debug C program online.
  5. Write your code in this editor and press "Run" button to compile and execute it.
  6.  
  7. *******************************************************************************/
  8.  
  9. #include <stdio.h>
  10. #include <string.h>
  11.  
  12. typedef struct
  13. {
  14. char* name; // name - unknown size
  15. int status; // status - 1-behind tree, 2-behind stone, 3-behind ice
  16. int strength; // strength - integer, the strength of the pig
  17. }Pig;
  18.  
  19. typedef struct
  20. {
  21. char* name;
  22. char color[10];
  23. int injuryPower; // integer. strength of the pig lowers
  24. int penetrationType; // 1-tree, 2-stone, 3-ice
  25. }Bird;
  26.  
  27. typedef struct
  28. {
  29. Bird** birdsArr; // array of poiters to birds
  30. int birdsSize; // size of array
  31. Pig** pigsArr; // array of poiters to pigs
  32. int pigSize; // size of array
  33. }Level;
  34.  
  35. int isPassableLevel(const Level* level)
  36. /* (A) This function gets a pointer to the level,
  37. and returns 1 if the level is passable, 0 if not */
  38. {
  39. int countPigsStatus1 = 0;
  40. int strengthPigsStatus1 = 0;
  41. int countPigsStatus2 = 0;
  42. int strengthPigsStatus2 = 0;
  43. int countPigsStatus3 = 0;
  44. int strengthPigsStatus3 = 0;
  45. int countBirdsPenetrationType1 = 0;
  46. int injuryPowerBirdsType1 = 0;
  47. int countBirdsPenetrationType2 = 0;
  48. int injuryPowerBirdsType2 = 0;
  49. int countBirdsPenetrationType3 = 0;
  50. int injuryPowerBirdsType3 = 0;
  51.  
  52. Pig * pointerPig = level->pigsArr[0];
  53. for (int i = 0; i < level->pigSize; i++)
  54. {
  55. switch (pointerPig->status)
  56. {
  57. case 1:
  58. {
  59. countPigsStatus1++;
  60. strengthPigsStatus1 += pointerPig->strength;
  61. pointerPig++;
  62. break;
  63. }
  64. case 2:
  65. {
  66. countPigsStatus2++;
  67. strengthPigsStatus2 += pointerPig->strength;
  68. pointerPig++;
  69. break;
  70. }
  71. case 3:
  72. {
  73. countPigsStatus3++;
  74. strengthPigsStatus3 += pointerPig->strength;
  75. pointerPig++;
  76. break;
  77. }
  78. }
  79. }
  80. Bird * pointerBird = level->birdsArr[0];
  81. for (int i = 0; i < level->birdsSize; i++)
  82. {
  83. switch (pointerBird->penetrationType)
  84. {
  85. case 1:
  86. {
  87. countBirdsPenetrationType1++;
  88. injuryPowerBirdsType1 += pointerBird->injuryPower;
  89. pointerBird++;
  90. break;
  91. }
  92. case 2:
  93. {
  94. countBirdsPenetrationType2++;
  95. injuryPowerBirdsType2 += pointerBird->injuryPower;
  96. pointerBird++;
  97. break;
  98. }
  99. case 3:
  100. {
  101. countBirdsPenetrationType3++;
  102. injuryPowerBirdsType3 += pointerBird->injuryPower;
  103. pointerBird++;
  104. break;
  105. }
  106. }
  107. }
  108. // If the counter of pigs from status x is bigger than the count of birds from status x - not passable
  109. if (countPigsStatus1 > countBirdsPenetrationType1 || countPigsStatus2 > countBirdsPenetrationType2 || countPigsStatus3 > countBirdsPenetrationType3)
  110. return 0;
  111.  
  112. // if there are more/equal number of birds from type x and they have more power - the level is passable
  113. if (countPigsStatus1 <= countBirdsPenetrationType1 && strengthPigsStatus1 <= injuryPowerBirdsType1 && \
  114. countPigsStatus2 <= countBirdsPenetrationType2 && strengthPigsStatus2 <= injuryPowerBirdsType1 && \
  115. countPigsStatus3 <= countBirdsPenetrationType3 && strengthPigsStatus3 <= injuryPowerBirdsType3)
  116. return 1;
  117. return 0;
  118. }
  119.  
  120.  
  121.  
  122. Level* fixLevel(Level* level, int max)
  123. /* (B) This function gets a pointer to the level and max injury power,
  124. the function cherck if the level is passable.
  125. If not, the function should add the level minimal number of matching birds
  126. to pass the level. Birds won't have more than the mox injury power */
  127. {
  128. int countPigsStatus1 = 0;
  129. int strengthPigsStatus1 = 0;
  130. int countPigsStatus2 = 0;
  131. int strengthPigsStatus2 = 0;
  132. int countPigsStatus3 = 0;
  133. int strengthPigsStatus3 = 0;
  134. int countBirdsPenetrationType1 = 0;
  135. int injuryPowerBirdsType1 = 0;
  136. int countBirdsPenetrationType2 = 0;
  137. int injuryPowerBirdsType2 = 0;
  138. int countBirdsPenetrationType3 = 0;
  139. int injuryPowerBirdsType3 = 0;
  140. int powerToAdd = 0;
  141. int numberOfBirdsToAddMax = 0;
  142.  
  143. // Calculate strength and number of pigs and birds
  144. Pig * pointerPig = level->pigsArr[0];
  145. for (int i = 0; i < level->pigSize; i++)
  146. {
  147. switch (pointerPig->status)
  148. {
  149. case 1:
  150. {
  151. countPigsStatus1++;
  152. strengthPigsStatus1 += pointerPig->strength;
  153. pointerPig++;
  154. break;
  155. }
  156. case 2:
  157. {
  158. countPigsStatus2++;
  159. strengthPigsStatus2 += pointerPig->strength;
  160. pointerPig++;
  161. break;
  162. }
  163. case 3:
  164. {
  165. countPigsStatus3++;
  166. strengthPigsStatus3 += pointerPig->strength;
  167. pointerPig++;
  168. break;
  169. }
  170. }
  171. }
  172.  
  173. Bird * pointerBird = level->birdsArr[0];
  174. for (int i = 0; i < level->birdsSize; i++)
  175. {
  176. switch (pointerBird->penetrationType)
  177. {
  178. case 1:
  179. {
  180. countBirdsPenetrationType1++;
  181. injuryPowerBirdsType1 += pointerBird->injuryPower;
  182. pointerBird++;
  183. break;
  184. }
  185. case 2:
  186. {
  187. countBirdsPenetrationType2++;
  188. injuryPowerBirdsType2 += pointerBird->injuryPower;
  189. pointerBird++;
  190. break;
  191. }
  192. case 3:
  193. {
  194. countBirdsPenetrationType3++;
  195. injuryPowerBirdsType3 += pointerBird->injuryPower;
  196. pointerBird++;
  197. break;
  198. }
  199. }
  200. }
  201. if (isPassableLevel(level) == 0) // the level is not passable
  202. {
  203. if (strengthPigsStatus1 > injuryPowerBirdsType1) // Pigs have more strength 1
  204. {
  205. powerToAdd = strengthPigsStatus1 - injuryPowerBirdsType1; // the amount of power to add to birds
  206. numberOfBirdsToAddMax = powerToAdd / max; // number of birds to add with max strength
  207. for (int i = 0; i < numberOfBirdsToAddMax; i++)
  208. {
  209. Bird newBird;
  210. newBird.name = "new";
  211. strcpy(newBird.color, "black");
  212. newBird.injuryPower = max;
  213. newBird.penetrationType = 1;
  214. int i = 0;
  215. Bird * lastBird = level->birdsArr[0];
  216. for (int i = 0; i < level->birdsSize; i++)
  217. {
  218. lastBird++;
  219. }
  220. *lastBird = newBird;
  221. level->birdsSize++;
  222. }
  223. if (powerToAdd % max != 0) // add one bird with less than max strength
  224. {
  225. Bird newBird;
  226. newBird.name = "new";
  227. strcpy(newBird.color, "black");
  228. newBird.injuryPower = powerToAdd % max;
  229. newBird.penetrationType = 1;
  230. int i = 0;
  231. Bird * lastBird = level->birdsArr[0];
  232. for (int i = 0; i < level->birdsSize; i++)
  233. {
  234. lastBird++;
  235. }
  236. *lastBird = newBird;
  237. level->birdsSize++;
  238. }
  239. }
  240.  
  241. if (strengthPigsStatus2 > injuryPowerBirdsType2) // Pigs have more strength 2
  242. {
  243. powerToAdd = strengthPigsStatus2 - injuryPowerBirdsType2; // the amount of power to add to birds
  244. numberOfBirdsToAddMax = powerToAdd / max; // number of birds to add with max strength
  245. for (int i = 0; i < numberOfBirdsToAddMax; i++)
  246. {
  247. Bird newBird;
  248. newBird.name = "new";
  249. strcpy(newBird.color, "black");
  250. newBird.injuryPower = max;
  251. newBird.penetrationType = 2;
  252. int i = 0;
  253. Bird * lastBird = level->birdsArr[0];
  254. for (int i = 0; i < level->birdsSize; i++)
  255. {
  256. lastBird++;
  257. }
  258. *lastBird = newBird;
  259. level->birdsSize++;
  260. }
  261. if (powerToAdd % max != 0) // add one bird with less than max strength
  262. {
  263. Bird newBird;
  264. newBird.name = "new";
  265. strcpy(newBird.color, "black");
  266. newBird.injuryPower = powerToAdd % max;
  267. newBird.penetrationType = 2;
  268. int i = 0;
  269. Bird * lastBird = level->birdsArr[0];
  270. for (int i = 0; i < level->birdsSize; i++)
  271. {
  272. lastBird++;
  273. }
  274. *lastBird = newBird;
  275. level->birdsSize++;
  276. }
  277. }
  278.  
  279. if (strengthPigsStatus3 > injuryPowerBirdsType3) // Pigs have more strength 3
  280. {
  281. powerToAdd = strengthPigsStatus3 - injuryPowerBirdsType3; // the amount of power to add to birds
  282. numberOfBirdsToAddMax = powerToAdd / max; // number of birds to add with max strength
  283. for (int i = 0; i < numberOfBirdsToAddMax; i++)
  284. {
  285. Bird newBird;
  286. newBird.name = "new";
  287. strcpy(newBird.color, "black");
  288. newBird.injuryPower = max;
  289. newBird.penetrationType = 3;
  290. int i = 0;
  291. Bird * lastBird = level->birdsArr[0];
  292. for (int i = 0; i < level->birdsSize; i++)
  293. {
  294. lastBird++;
  295. }
  296. *lastBird = newBird;
  297. level->birdsSize++;
  298. }
  299. if (powerToAdd % max != 0) // add one bird with less than max strength
  300. {
  301. Bird newBird;
  302. newBird.name = "new";
  303. strcpy(newBird.color, "black");
  304. newBird.injuryPower = powerToAdd % max;
  305. newBird.penetrationType = 3;
  306. int i = 0;
  307. Bird * lastBird = level->birdsArr[0];
  308. for (int i = 0; i < level->birdsSize; i++)
  309. {
  310. lastBird++;
  311. }
  312. *lastBird = newBird;
  313. level->birdsSize++;
  314. }
  315. }
  316. }
  317. }
  318.  
  319. void deleteLevel(Level * level)
  320. /* (C) This function gets a pointer to the level and delets all the Level
  321. and the data related to it */
  322. {
  323. free(level->birdsArr);
  324. free(level->pigArr);
  325. }
  326. Level* hi(Level* level)
  327. {
  328. // adding new bied to the level
  329. Bird newBird;
  330. newBird.name = "new";
  331. strcpy(newBird.color, "black");
  332. newBird.injuryPower = 5;
  333. newBird.penetrationType = 1;
  334. int i = 0;
  335. Bird * lastBird = level->birdsArr[0];
  336. for (int i = 0; i < level->birdsSize; i++)
  337. {
  338. lastBird++;
  339. }
  340. *lastBird = newBird;
  341. level->birdsSize++;
  342. printNames(&level[0]);
  343. }
  344.  
  345. int printNames(Level* level)
  346. {
  347. // printing names of all birds
  348. for (int i = 0; i < level->birdsSize; i++)
  349. {
  350. printf("%s\t", (*level->birdsArr)->name);
  351. printf("%d\t",(*level->birdsArr)->injuryPower);
  352. printf("%d\t",(*level->birdsArr)->penetrationType);
  353. printf("\n");
  354. (*level->birdsArr)++;
  355. }
  356. return 0;
  357. }
  358.  
  359. int main()
  360. {
  361. Level l1;
  362. Pig p1, p2;
  363. p1.name = "p1";
  364. p1.status = 2;
  365. p1.strength = 21;
  366. //p2.name = "p2";
  367. //p2.status = 3;
  368. //p2.strength = 3;
  369. Bird b1, b2; //, b3;
  370. b1.name = "b1";
  371. strcpy(b1.color, "red") ;
  372. b1.injuryPower = 3;
  373. b1.penetrationType = 1;
  374. b2.name = "b2";
  375. strcpy(b2.color, "blue");
  376. b2.injuryPower = 3;
  377. b2.penetrationType = 1;
  378. //b3.name = "b3";
  379. // strcpy(b2.color, "grey");
  380. // b3.injuryPower = 3;
  381. // b3.penetrationType = 3;
  382. Pig parr[] = { p1 };//, p2 };
  383. Pig* pigArr = parr;
  384. l1.pigsArr = &pigArr;
  385. l1.pigSize = 1;
  386.  
  387. Bird barr[] = { b1, b2 }; //, b3 };
  388. Bird* birdArr = barr;
  389.  
  390. l1.birdsArr = &birdArr;
  391. l1.birdsSize = 2;
  392. printf("%d", isPassableLevel(&l1));
  393. printNames(&l1);
  394.  
  395. fixLevel(&l1, 5);
  396. printNames(&l1);
  397. //hi(&l1);
  398. //printf("%d", isPassableLevel(&l1));
  399. }
  400.  
  401. /*int main()
  402. {
  403. Level l1;
  404. Pig p1;
  405. p1.name = "p1";
  406. p1.status = 1;
  407. p1.strength = 1;
  408. Bird b1, b2;
  409. b1.name = "b1";
  410. strcpy(b1.color, "red");
  411. b1.injuryPower = 1;
  412. b1.penetrationType = 2;
  413. b2.name = "b2";
  414. strcpy(b2.color, "blue");
  415. b2.injuryPower = 1;
  416. b2.penetrationType = 2;
  417. Pig parr[] = { p1 };
  418. Pig* pigArr = parr;
  419. l1.pigsArr = &pigArr;
  420. l1.pigSize = 1;
  421.  
  422. Bird barr[] = { b1, b2 };
  423. Bird* birdArr = barr;
  424.  
  425. l1.birdsArr = &birdArr;
  426. l1.birdsSize = 2;
  427.  
  428. printf("%d", isPassableLevel(&l1));
  429. }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement