Wan_o_war - Programming Fundamentals Mid Exam Retake - 6 August 2019
Здравейте, може ли помощ с тази задача? Поради някаква причина изпълнява функциите със старите стойности, когато индексите са невалидни и не знам защо...
Условието:
Create a program that tracks the battle and either chooses a winner or prints a stalemate. On the first line you will receive the status of the pirate ship, which is a string representing integer sections separated by '>'. On the second line you will receive the same type of status, but for the warship:
"{section1}>{section2}>{section3}… {sectionn}"
On the third line you will receive the maximum health capacity a section of the ship can reach.
The following lines represent commands until "Retire":
-
Fire {index} {damage} – the pirate ship attacks the warship with the given damage at that section. Check if the index is valid and if not skip the command. If the section breaks (health <= 0) the warship sinks, print the following and stop the program:
"You won! The enemy ship has sunken."
-
Defend {startIndex} {endIndex} {damage} - the warship attacks the pirate ship with the given damage at that range (indexes are inclusive). Check if both indexes are valid and if not skip the command. If the section breaks (health <= 0) the pirate ship sinks, print the following and stop the program:
"You lost! The pirate ship has sunken."
-
Repair {index} {health} - the crew repairs a section of the pirate ship with the given health. Check if the index is valid and if not skip the command. The health of the section cannot exceed the maximum health capacity.
-
Status – prints the count of all sections of the pirate ship that need repair soon, which are all sections that are lower than 20% of the maximum health capacity. Print the following:
"{count} sections need repair."
In the end if a stalemate occurs print the status of both ships, which is the sum of their individual sections in the following format:
"Pirate ship status: {pirateShipSum}"
"Warship status: {warshipSum}"
12>13>11>20>66 12>22>33>44>55>32>18 70 Fire 2 11 Fire 8 100 Defend 3 6 11 Defend 0 3 5 Repair 1 33 Status Retire |
2 sections need repair. Pirate ship status: 135 Warship status: 205 |