DEV Community

mthomas7503
mthomas7503

Posted on • Updated on

Combination of hobby and career changing.

I have only recently started coding and I have been looking into just how to practice more when I get the chance. With this in mind, I decided to just write out a simple bit of code that produces a life total message whenever you fall below or at a certain percentage in this game I play called Magic The Gathering. To do this, I decided to incorporate a few of the things I learned in my first phase of the program I'm working with: if...else statements, and just a basic function. I added in the built-in method of .random() just to help see if additional messages would come up.

I wanted to keep it relatively simple given that there are thousands of cards and so many different creatures. So I started by having these variables assigned from the start.

`const damage = (Math.random() * 10) + 10;

const startingLifeTotal = 40

const postCombatLifeTotal = startingLifeTotal - damage`

This way we know how starting total is 40 and our life total after damage, postCombatLifeTotal, is equal to the starting minus the damage.

The next part is where I wanted to get some practice with functions and if...else if statements.

function lifeMessage(cLife) {let message
if (cLife === 0) {message = 'You have died'}
else if(cLife < startingLifeTotal * .2) {message = 'Engaging Emergency Protocols'}
else if (cLife <= startingLifeTotal * .5) {message =
${cLife} is pretty bad, but you can come back from it!... Maybe?}
else if (cLife <= startingLifeTotal * .75) {message = 'Tis just a flesh wound!'}
return message}

I ran a couple of tests and it works, but the issue here is given that the damage only falls into a specific range, it ends up that postCombatLifeTotal never ends up going below 50%. This is the one issue I have run into. I am of course going to work on this and fix this as time goes on. I could use a fetch() to get the power of a specific creature card or have the number be a user input instead. I will of course keep everyone posted when I do that. Until later everyone! Happy coding!

Top comments (0)