+ Нов въпрос
evgeni4 17 Точки

## Battle Manager

Здравейте, тази задача реших но някъде греша не мога да разбера къде

judge:91 / 100

https://pastebin.com/yhTsvt0S

Battle Manager

Create a program that manages battles. You need to keep information about people, the health and energy they have. You will be receiving lines with commands until you receive the "Results" command. There are three possible commands:

• Add the person, his/her health and energy to your records. If person with the given name already exists, just increase the health of the person with the current one that is given.
• "Attack:{attackerName}:{defenderName}:{damage}":
• Check if both people exist and if they do, reduce the defender’s health with the damage given. If the defender’s health reaches 0 or less, the person is disqualified, and you need to remove him/her from your records and print the following message:
• "{defenderName} was disqualified!"
• You also have to reduce the attacker’s energy by 1. If it reaches 0, he/she is disqualified, and you need to remove him/her from your records and print the following message:
• "{attackerName} was disqualified!"
• Delete all records of the given user, if he exists. If "All" is given as username - delete all records you have.

In the end, you have to print the count of people left, each person with his/her health and energy sorted in descending order by the health and then by their name in ascending order in the following format:

People count: {count}

{personName} - {health} - {energy}

{personName} - {health} - {energy}

## Input

• You will be receiving lines until you receive the "Results" command.
• The health is an integer number in the range [1...100000].
• The energy is an integer number in the range [1...100].
• The input will always be valid.

## Output

• Print the appropriate message after the "Attack" command, if someone is disqualified.
• Print the people with their health and energy in the format described above.

## Examples

 Input Output Add:Mark:1000:5 Add:Clark:1000:3 Attack:Clark:Mark:500 Add:Allison:2500:5 Attack:Clark:Mark:300 Add:Charlie:4000:10 Attack:Clark:Mark:500 Results Mark was disqualified! Clark was disqualified! People count: 2 Charlie - 4000 - 10 Allison - 2500 - 5 Comments First, we receive the "Add:Mark:1000:5" command, so we add the person Mark in our records with his health (1000) and energy (5). Then we do the same for Clark. Afterwards Clark attacks Mark multiple times, and Mark’s health is below 0 and Clark’s energy drops to 0, meaning both of them are disqualified. Meanwhile, Allison and Charlie were added. In the end, we print the results- 2 people, then the collection ordered as described above. Add:Bonnie:3000:5 Add:Kent:10000:10 Add:Johny:4000:10 Attack:Johny:Bonnie:400 Add:Chicken:1000:1 Add:Rabbit:3000:5 Add:Buggy:1259:10 Delete:Kent Attack:Chicken:Rabbit:1000 Results Chicken was disqualified! People count: 4 Johny - 4000 - 9 Bonnie - 2600 - 5 Rabbit - 2000 - 5 Buggy - 1259 - 10 Add:Bonnie:3000:5 Add:Johny:4000:10 Delete:All Add:Bonnie:3333:3 Results People count: 1 Bonnie - 3333 - 3

Тагове:
1
15/09/2019 15:01:47
willystyle 1964 Точки

Всичките проблеми идват от сортировката:

<?php
\$battles = [];
while (\$input != "Results") {
\$comand = explode(":", \$input);
switch (\$comand[0]) {
\$personName = \$comand[1];
\$health = intval(\$comand[2]);
\$energy = intval(\$comand[3]);
if (!key_exists(\$personName, \$battles)) {
\$battles[\$personName] = [];
\$battles[\$personName]['health'] = 0;
\$battles[\$personName]['energy'] = \$energy;
}
\$battles[\$personName]['health'] += \$health;
break;
case "Delete":
\$personName = \$comand[1];
if (\$personName == "All") {
\$battles = [];
}
if (key_exists(\$personName, \$battles)) {
unset(\$battles[\$personName]);
}
break;
case "Attack":
\$attackerName = \$comand[1];
\$defenderName = \$comand[2];
\$damage = intval(\$comand[3]);
if (key_exists(\$attackerName, \$battles) && key_exists(\$defenderName, \$battles)) {
\$battles[\$defenderName]['health'] -= \$damage;
\$battles[\$attackerName]['energy'] -= 1;
if (\$battles[\$defenderName]['health'] <= 0) {
unset(\$battles[\$defenderName]);
echo "\$defenderName was disqualified!\n";
}
if (\$battles[\$attackerName]['energy'] <= 0) {
unset(\$battles[\$attackerName]);
echo "\$attackerName was disqualified!\n";
}
}
break;
}
}
\$count = count(\$battles);
echo "People count: \$count\n";

uksort(\$battles, 'cmp');

function cmp(\$a, \$b)
{
if (\$GLOBALS['battles'][\$a]['health'] == \$GLOBALS['battles'][\$b]['health']) {
if (\$a == \$b) {
return 0;
}
return strcmp(\$a, \$b);
}
return \$GLOBALS['battles'][\$a]['health'] - \$GLOBALS['battles'][\$b]['health'] > 0 ? -1 : 1;
}

foreach (\$battles as \$key => \$val) {
echo "\$key - \${val['health']} - \${val['energy']}\n";
}

0
evgeni4 17 Точки

благодаря ти много

1