To recap our progress so far, this.initializeGame() calls this.startNewBattle(), which in turn calls this.battle().
The battle() method is the main event of the game that will run an indefinite number of times. Each time, it will either be the Player turn or the Enemy turn
Did you notice that we're using previously created methods like getAttackValue(), reduceHealth(), and getHealth()
We're using the inquirer package's type: 'list' option to display a list of choices, where the user must either select 'Attack' or 'Use potion':
Keep in mind that you might have to run the game a few times to get a scenario where the Player is first versus the Enemy
If the inventory is empty, immediately return to end the Player turn
the usePotion() method we set up and tested earlier requires the index of the object in the array (e.g., usePotion(0)
How can we preserve the indexes if the inquirer package only returns a string value?
One solution would be to populate the choices array with strings that contain the Potion name and its index (e.g., '1: health'),
then strip out the index after the user has chosen. We can do this using the Array.prototype.map() method.
The map() method creates a new array based on the results of a callback function used in the original array. The following code shows an example:
We're using that index to create a human-readable number for the user.
Update the inquirer prompt to include a callback function with the necessary split() logic.
Glasp is a social web highlighter that people can highlight and organize quotes and thoughts from the web, and access other like-minded people’s learning.