Battle Simulator

## Introduction

This is a example of using Perchance features to simulate a turn based game or a battle simulator.

## Features

### Executing & Hiding the Output

The battle-simulator-example uses a few sneaky Perchance features. Let's have a look at the `turn` list:

```
turn
	[character.hp -= 5, ""]You were hit in the torso with a stone.
	[character.hp += 20, ""]A nearby priest heals your wounds. ^0.1
	...

```

See how each item has something like `[character.hp -= 5, ""]` before it? What's happening here? Well, `-= 5` just means "subtract five from this variable". So we're just decreasing the character's hitpoints by 5 each time that first item is randomly selected and consequently "evaluated".

Now, to understand the comma and double quotation marks, we need to understand some fundamental stuff about Perchance: You can put multiple commands/variables inside square brackets so long as you separate them by a comma - and importantly *Perchance will only print out the result of the **last** command*. So it executes all the commands, but only prints out the last one. Here our last command was an "empty" command. We're just telling it to print out nothing. If we'd written `[character.hp -= 5, "apple"]`, then it'd print out the text "apple" in the place of that square block. If we wrote `[character.hp -= 5, character.name]`, then it'd print out the character's name. You'll use the `[command, ""]` pattern a lot in your Perchance travels to sneakily execute commands behind the scenes without actually outputting anything.

### Dynamic Odds

Our next lesson from this generator is the dynamic odds notation `^[character.hp > 0]`. Here we're using what are called "boolean expressions". The expression `1 > 100` means "1 is greater than 100" - which is false. One is definitely not greater than one hundred. So this expression evaluates to "false", which is equivalent to the number 0 in Perchance odds notation. So if you wrote `^[1 > 100]` after an item, it'd always get a weight of 0 - which means it's impossible. There's no use in even having an item with those odds in the list! On the other hand, if you wrote `^[1 < 100]`, then the statement is "1 is less than 100" which evaluates to true, which is equivalent to 1. So you might as well just write `^1` - but that's the default value anyway! As you can see, dynamic odds notation is not so useful unless you're using "variables" (things that can vary) like `character.hp`.

So if the expression after `^` is true, it gets a weight of one. Conversely if the expression is false, it gets a weight of zero (equivalent to writing `^0` after the item). There are 4 different boolean "operators" of the greater/less than variety:

* `<` means less than
* `>` means greater than
* `<=` means less than or equal to
* `>=` means greater than or equal to

So coming back to our original expression: `^[character.hp > 0]`, this just means "give this item a weight of 1 if `character.hp` is greater than one, otherwise give it a weight of 0". Remember that dynamic weights don't have to be boolean expressions. They can just be references to other variables like `^[character.age]` or something like that.

### Looping

There is one final pro technique used in this generator. Can you see what it is? Notice that the first item in the `battle` list contains a reference to the `battle` list itself!

```
battle
	[turn]<br>[status]<br>[battle] ^[character.hp > 0]
	It's over. ^[character.hp <= 0]

```

What on earth is going on there?! Well, firstly, `<br>` is just how you output a "line **br**eak" (new line) in HTML. That ensures that each step of the battle is printed on it's own line. Secondly, the "self-referential" behaviour of this `battle` list isn't too hard to understand once we put it in plain English: "Keep doing the battle until the character's health is zero or lower, then output `It's over.`".

So we evaulate `[battle]` *once* and it will select the first item because the character's `hp` is above zero, then it executes a `[turn]`, then prints the `[status]`, then calls `[battle]` which repeats the process again. This keeps happening until the character's health falls below zero - at which point the second item, `It's over.` is chosen and the loop stops. Pretty neat, huh? Here's another example where you might want to use loops (see @syrupyeti's reply).

### Note on Loops

Note that when you're creating "loops" like this, its easy to accidentally create an "infinite loop", which may freeze up the browser tab. It freezes up the browser tab because the infinite loop is trying to execute the loop millions of times, and potentially trying to output way too much text. This won't harm your computer (you can just close the tab), but if you're not saving often, you may lose some work because you can't save a generator while it's in this "frozen" state. So make sure you're saving often. If you do accidentally freeze up your generator, but you've saved it with the infinite loop code (so everytime you visit the page it immediately freezes up) then you can prevent your code from executing by putting `#debugFreeze` at the end of your generator's URL. For example:

```
https://perchance.org/your-generators-name#debugFreeze

```

This will open up the editor panel and prevent it from executing your code, so you've got a chance to make the required edits to stop the infinte loop, and then you can save it.