Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************************
- Online C Compiler.
- Code, Compile, Run and Debug C program online.
- Write your code in this editor and press "Run" button to compile and execute it.
- *******************************************************************************/
- #include <stdio.h>
- #include <string.h>
- typedef struct
- {
- char* name; // name - unknown size
- int status; // status - 1-behind tree, 2-behind stone, 3-behind ice
- int strength; // strength - integer, the strength of the pig
- }Pig;
- typedef struct
- {
- char* name;
- char color[10];
- int injuryPower; // integer. strength of the pig lowers
- int penetrationType; // 1-tree, 2-stone, 3-ice
- }Bird;
- typedef struct
- {
- Bird** birdsArr; // array of poiters to birds
- int birdsSize; // size of array
- Pig** pigsArr; // array of poiters to pigs
- int pigSize; // size of array
- }Level;
- int isPassableLevel(const Level* level)
- /* (A) This function gets a pointer to the level,
- and returns 1 if the level is passable, 0 if not */
- {
- int countPigsStatus1 = 0;
- int strengthPigsStatus1 = 0;
- int countPigsStatus2 = 0;
- int strengthPigsStatus2 = 0;
- int countPigsStatus3 = 0;
- int strengthPigsStatus3 = 0;
- int countBirdsPenetrationType1 = 0;
- int injuryPowerBirdsType1 = 0;
- int countBirdsPenetrationType2 = 0;
- int injuryPowerBirdsType2 = 0;
- int countBirdsPenetrationType3 = 0;
- int injuryPowerBirdsType3 = 0;
- Pig * pointerPig = level->pigsArr[0];
- for (int i = 0; i < level->pigSize; i++)
- {
- switch (pointerPig->status)
- {
- case 1:
- {
- countPigsStatus1++;
- strengthPigsStatus1 += pointerPig->strength;
- pointerPig++;
- break;
- }
- case 2:
- {
- countPigsStatus2++;
- strengthPigsStatus2 += pointerPig->strength;
- pointerPig++;
- break;
- }
- case 3:
- {
- countPigsStatus3++;
- strengthPigsStatus3 += pointerPig->strength;
- pointerPig++;
- break;
- }
- }
- }
- Bird * pointerBird = level->birdsArr[0];
- for (int i = 0; i < level->birdsSize; i++)
- {
- switch (pointerBird->penetrationType)
- {
- case 1:
- {
- countBirdsPenetrationType1++;
- injuryPowerBirdsType1 += pointerBird->injuryPower;
- pointerBird++;
- break;
- }
- case 2:
- {
- countBirdsPenetrationType2++;
- injuryPowerBirdsType2 += pointerBird->injuryPower;
- pointerBird++;
- break;
- }
- case 3:
- {
- countBirdsPenetrationType3++;
- injuryPowerBirdsType3 += pointerBird->injuryPower;
- pointerBird++;
- break;
- }
- }
- }
- // If the counter of pigs from status x is bigger than the count of birds from status x - not passable
- if (countPigsStatus1 > countBirdsPenetrationType1 || countPigsStatus2 > countBirdsPenetrationType2 || countPigsStatus3 > countBirdsPenetrationType3)
- return 0;
- // if there are more/equal number of birds from type x and they have more power - the level is passable
- if (countPigsStatus1 <= countBirdsPenetrationType1 && strengthPigsStatus1 <= injuryPowerBirdsType1 && \
- countPigsStatus2 <= countBirdsPenetrationType2 && strengthPigsStatus2 <= injuryPowerBirdsType1 && \
- countPigsStatus3 <= countBirdsPenetrationType3 && strengthPigsStatus3 <= injuryPowerBirdsType3)
- return 1;
- return 0;
- }
- Level* fixLevel(Level* level, int max)
- /* (B) This function gets a pointer to the level and max injury power,
- the function cherck if the level is passable.
- If not, the function should add the level minimal number of matching birds
- to pass the level. Birds won't have more than the mox injury power */
- {
- int countPigsStatus1 = 0;
- int strengthPigsStatus1 = 0;
- int countPigsStatus2 = 0;
- int strengthPigsStatus2 = 0;
- int countPigsStatus3 = 0;
- int strengthPigsStatus3 = 0;
- int countBirdsPenetrationType1 = 0;
- int injuryPowerBirdsType1 = 0;
- int countBirdsPenetrationType2 = 0;
- int injuryPowerBirdsType2 = 0;
- int countBirdsPenetrationType3 = 0;
- int injuryPowerBirdsType3 = 0;
- int powerToAdd = 0;
- int numberOfBirdsToAddMax = 0;
- // Calculate strength and number of pigs and birds
- Pig * pointerPig = level->pigsArr[0];
- for (int i = 0; i < level->pigSize; i++)
- {
- switch (pointerPig->status)
- {
- case 1:
- {
- countPigsStatus1++;
- strengthPigsStatus1 += pointerPig->strength;
- pointerPig++;
- break;
- }
- case 2:
- {
- countPigsStatus2++;
- strengthPigsStatus2 += pointerPig->strength;
- pointerPig++;
- break;
- }
- case 3:
- {
- countPigsStatus3++;
- strengthPigsStatus3 += pointerPig->strength;
- pointerPig++;
- break;
- }
- }
- }
- Bird * pointerBird = level->birdsArr[0];
- for (int i = 0; i < level->birdsSize; i++)
- {
- switch (pointerBird->penetrationType)
- {
- case 1:
- {
- countBirdsPenetrationType1++;
- injuryPowerBirdsType1 += pointerBird->injuryPower;
- pointerBird++;
- break;
- }
- case 2:
- {
- countBirdsPenetrationType2++;
- injuryPowerBirdsType2 += pointerBird->injuryPower;
- pointerBird++;
- break;
- }
- case 3:
- {
- countBirdsPenetrationType3++;
- injuryPowerBirdsType3 += pointerBird->injuryPower;
- pointerBird++;
- break;
- }
- }
- }
- if (isPassableLevel(level) == 0) // the level is not passable
- {
- if (strengthPigsStatus1 > injuryPowerBirdsType1) // Pigs have more strength 1
- {
- powerToAdd = strengthPigsStatus1 - injuryPowerBirdsType1; // the amount of power to add to birds
- numberOfBirdsToAddMax = powerToAdd / max; // number of birds to add with max strength
- for (int i = 0; i < numberOfBirdsToAddMax; i++)
- {
- Bird newBird;
- newBird.name = "new";
- strcpy(newBird.color, "black");
- newBird.injuryPower = max;
- newBird.penetrationType = 1;
- int i = 0;
- Bird * lastBird = level->birdsArr[0];
- for (int i = 0; i < level->birdsSize; i++)
- {
- lastBird++;
- }
- *lastBird = newBird;
- level->birdsSize++;
- }
- if (powerToAdd % max != 0) // add one bird with less than max strength
- {
- Bird newBird;
- newBird.name = "new";
- strcpy(newBird.color, "black");
- newBird.injuryPower = powerToAdd % max;
- newBird.penetrationType = 1;
- int i = 0;
- Bird * lastBird = level->birdsArr[0];
- for (int i = 0; i < level->birdsSize; i++)
- {
- lastBird++;
- }
- *lastBird = newBird;
- level->birdsSize++;
- }
- }
- if (strengthPigsStatus2 > injuryPowerBirdsType2) // Pigs have more strength 2
- {
- powerToAdd = strengthPigsStatus2 - injuryPowerBirdsType2; // the amount of power to add to birds
- numberOfBirdsToAddMax = powerToAdd / max; // number of birds to add with max strength
- for (int i = 0; i < numberOfBirdsToAddMax; i++)
- {
- Bird newBird;
- newBird.name = "new";
- strcpy(newBird.color, "black");
- newBird.injuryPower = max;
- newBird.penetrationType = 2;
- int i = 0;
- Bird * lastBird = level->birdsArr[0];
- for (int i = 0; i < level->birdsSize; i++)
- {
- lastBird++;
- }
- *lastBird = newBird;
- level->birdsSize++;
- }
- if (powerToAdd % max != 0) // add one bird with less than max strength
- {
- Bird newBird;
- newBird.name = "new";
- strcpy(newBird.color, "black");
- newBird.injuryPower = powerToAdd % max;
- newBird.penetrationType = 2;
- int i = 0;
- Bird * lastBird = level->birdsArr[0];
- for (int i = 0; i < level->birdsSize; i++)
- {
- lastBird++;
- }
- *lastBird = newBird;
- level->birdsSize++;
- }
- }
- if (strengthPigsStatus3 > injuryPowerBirdsType3) // Pigs have more strength 3
- {
- powerToAdd = strengthPigsStatus3 - injuryPowerBirdsType3; // the amount of power to add to birds
- numberOfBirdsToAddMax = powerToAdd / max; // number of birds to add with max strength
- for (int i = 0; i < numberOfBirdsToAddMax; i++)
- {
- Bird newBird;
- newBird.name = "new";
- strcpy(newBird.color, "black");
- newBird.injuryPower = max;
- newBird.penetrationType = 3;
- int i = 0;
- Bird * lastBird = level->birdsArr[0];
- for (int i = 0; i < level->birdsSize; i++)
- {
- lastBird++;
- }
- *lastBird = newBird;
- level->birdsSize++;
- }
- if (powerToAdd % max != 0) // add one bird with less than max strength
- {
- Bird newBird;
- newBird.name = "new";
- strcpy(newBird.color, "black");
- newBird.injuryPower = powerToAdd % max;
- newBird.penetrationType = 3;
- int i = 0;
- Bird * lastBird = level->birdsArr[0];
- for (int i = 0; i < level->birdsSize; i++)
- {
- lastBird++;
- }
- *lastBird = newBird;
- level->birdsSize++;
- }
- }
- }
- }
- void deleteLevel(Level * level)
- /* (C) This function gets a pointer to the level and delets all the Level
- and the data related to it */
- {
- free(level->birdsArr);
- free(level->pigArr);
- }
- Level* hi(Level* level)
- {
- // adding new bied to the level
- Bird newBird;
- newBird.name = "new";
- strcpy(newBird.color, "black");
- newBird.injuryPower = 5;
- newBird.penetrationType = 1;
- int i = 0;
- Bird * lastBird = level->birdsArr[0];
- for (int i = 0; i < level->birdsSize; i++)
- {
- lastBird++;
- }
- *lastBird = newBird;
- level->birdsSize++;
- printNames(&level[0]);
- }
- int printNames(Level* level)
- {
- // printing names of all birds
- for (int i = 0; i < level->birdsSize; i++)
- {
- printf("%s\t", (*level->birdsArr)->name);
- printf("%d\t",(*level->birdsArr)->injuryPower);
- printf("%d\t",(*level->birdsArr)->penetrationType);
- printf("\n");
- (*level->birdsArr)++;
- }
- return 0;
- }
- int main()
- {
- Level l1;
- Pig p1, p2;
- p1.name = "p1";
- p1.status = 2;
- p1.strength = 21;
- //p2.name = "p2";
- //p2.status = 3;
- //p2.strength = 3;
- Bird b1, b2; //, b3;
- b1.name = "b1";
- strcpy(b1.color, "red") ;
- b1.injuryPower = 3;
- b1.penetrationType = 1;
- b2.name = "b2";
- strcpy(b2.color, "blue");
- b2.injuryPower = 3;
- b2.penetrationType = 1;
- //b3.name = "b3";
- // strcpy(b2.color, "grey");
- // b3.injuryPower = 3;
- // b3.penetrationType = 3;
- Pig parr[] = { p1 };//, p2 };
- Pig* pigArr = parr;
- l1.pigsArr = &pigArr;
- l1.pigSize = 1;
- Bird barr[] = { b1, b2 }; //, b3 };
- Bird* birdArr = barr;
- l1.birdsArr = &birdArr;
- l1.birdsSize = 2;
- printf("%d", isPassableLevel(&l1));
- printNames(&l1);
- fixLevel(&l1, 5);
- printNames(&l1);
- //hi(&l1);
- //printf("%d", isPassableLevel(&l1));
- }
- /*int main()
- {
- Level l1;
- Pig p1;
- p1.name = "p1";
- p1.status = 1;
- p1.strength = 1;
- Bird b1, b2;
- b1.name = "b1";
- strcpy(b1.color, "red");
- b1.injuryPower = 1;
- b1.penetrationType = 2;
- b2.name = "b2";
- strcpy(b2.color, "blue");
- b2.injuryPower = 1;
- b2.penetrationType = 2;
- Pig parr[] = { p1 };
- Pig* pigArr = parr;
- l1.pigsArr = &pigArr;
- l1.pigSize = 1;
- Bird barr[] = { b1, b2 };
- Bird* birdArr = barr;
- l1.birdsArr = &birdArr;
- l1.birdsSize = 2;
- printf("%d", isPassableLevel(&l1));
- }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement