16
9
Welcome, brave code golfer! Today you will stand in the midst of a great battle between a goblin and an elf!
goblin attacks elf!
elf dodges!
elf attacks goblin!
elf hits goblin for 13 damage!
goblin has 37 health left.
goblin attacks elf!
goblin hits elf for 1 damage!
elf has 49 health left.
elf attacks goblin!
elf hits goblin for 19 damage!
goblin has 18 health left.
goblin attacks elf!
goblin hits elf for 26 damage!
elf has 23 health left.
elf attacks goblin!
elf hits goblin for 20 damage!
goblin has been slain!
The Challenge
Your challenge is to simulate a battle, like the above one. You will recieve input in this form:
creatureName health strength defense accuracy agility
For example, the battle between the goblin and elf would be:
goblin 50 40 35 3 2 elf 50 35 30 4 5
The first and second creatures will alternate attacks.
- Print 'creatureName attacks otherCreatureName!'
- Check to see if the creature dodges. The creature will dodge an attack if (its agility times rand() divided by 2) is greater than (the attacker's accuracy times rand()).
- If the creature dodges, print 'creatureName dodges!'
- Otherwise, calculate the damage dealt by subtracting (the attacker's strength times rand()) and (the defender's defense times rand() divided by 2). Minimum damage is 1. Print 'creatureName hits otherCreatureName for (x) damage!' Then print 'creatureName has (x) health left.' unless the creature's health is 0 or less, in which case...
- If the creature's health is 0 or less, print 'creatureName has been slain!' and end the program.
Rules
- Shortest code wins.
- Don't literally print 'creatureName,' but replace it with the creature's name. Don't print '(x) damage' or '(x) health;' replace them with the actual amount. (I have to specify this because some people are very creative with bending the rules. :P)
2Why complicate the spec by halving the agility and defence? – Peter Taylor – 2013-08-05T13:21:06.760
@Peter Because otherwise the battles took too long. – Doorknob – 2013-08-05T13:22:32.673
2My point was: why not change the input e.g. to
goblin 50 40 18 3 2 elf 50 35 15 4 5
? – Peter Taylor – 2013-08-05T13:30:05.190@Peter Meh, that just seemed too odd - why would the defense be so much lower? Anyway, it is a puzzle; no reason not to :P – Doorknob – 2013-08-05T13:32:17.793
I think it's not clear whether 'creatureName has (x) health left.' should be printed on the last round (when the creature is slain), and in case it should (i assumed it shouldn't) if (x) should be always 0 or it can be a negative number. – epidemian – 2013-08-06T05:57:02.890
@epid See sample output. – Doorknob – 2013-08-06T06:03:16.433
@Doorknob, yes; i was referring to the battle steps' description. But you're right, it's clear from the sample output. – epidemian – 2013-08-06T06:11:12.120
@epid Yes, the steps were unclear; I tried to clarify them a bit. – Doorknob – 2013-08-06T07:12:05.440
btw, nice challenge =) – C5H8NNaO4 – 2013-08-06T07:55:33.537
I squeezed 4 from my solution by switching to raw_input, but this means the input now needs to be quoted. Not sure if this means I need to add 2 to the score for the quotes or not. – None – 2013-08-07T00:45:20.623
@Lego Meh, some programs are run much more verbosely; I'd consider that as part of the execution. Don't add 2. – Doorknob – 2013-08-07T00:48:51.113