RPG Strategy

RPG Strategy

The colossal RPG is our first really big project. It's great practice for how to approach big things. For those of you who don't know how to start, here is how I would do it.

The first few steps will help us get a big picture of what's going on. Skip to step 4

Step One

First, I would write a list of all the small actions that need to be taken.

character creation
walking
running from a fight
fighting
enemy creation
attacking enemy
enemy attacking
enemy dying (including dropping an item)
character dying

Step Two

The assignment write up already has a list of requirements. You could also use that as your list of things that need to be done. We can then define empty functions. This helps us conceptualize what needs to be done in smaller steps.

function walk(){

}

function run(){

}

function fight(){

}

function attackEnemy(){

}

function enemyAttack(){

}

function die(){

}

function enemyDie(){

}

function enemyCreation(){

}

Step Four

Writing pseudo code is also helpful.

//ask user for name and give greeting

function walk(){
   //ask user to push "w" to walk
    if (user pushed w){
        if(1 in 4){
            fight();
        } else {
            //tell user they didn't run into a monster
            //****THIS PART IS IMPORTANT. DO NOT CALL WALK()****
        }
    } else {
        //tell user what's in their inventory, and their health, and then tell them to push w to walk
}

function run(){
    if(1 in 2){
        //tell user that they successfully got away and can continue walking
        //****THIS PART IS IMPORTANT. DO NOT CALL WALK()****
    } else {
        //tell user they were not able to run
        //****THIS PART IS IMPORTANT. DO NOT CALL fight() You could, however, call one of the attack functions****
}

function fight(){
    ask user to either fight or run
    enemyCreation()
    if (user is trying to run){
        run()
    } else {
        attackEnemy()
    }
}

//****you get the idea****

function attackEnemy(){

}

function enemyAttack(){

}

function die(){

}

function enemyDie(){

}

The reason that you do not want to call those functions that I warned about in the code is because you do not want to create loops by having functions call functions. Having functions call functions is fine, just so it isn't circular.

walk() calling fight() calling run() calling walk() = bad

walk() calling fight() calling attack() calling die() = OK

Do you see how the first one is circular and the second is linear?

The circular one causes recursion and an eventual stack overflow. Both of which are hard to deal with. Recursion can be useful if a very few situations. This is not one of them.

So, how to we continue walking after we fight a monster or run away with it?

A while loop.

I'd put one that calls walk:

//ask user for name and give greeting

while(player.health > 0){
    walk();
}

and one in my fight function:

function fight(){
    ask user to either fight or run
    enemyCreation();
    if (user is trying to run){
        run();
    } else {
        enemyAttack();
    }
    while (enemy.health > 0){
        attackEnemy();
        enemyAttack();
    }
}

Warning!

Laying your code out this way makes it hard to test along the way. When you get to about this point, it's good to comment out all but a bit of your code and start writing and testing small snippets and building it out that way.

Also, do not define functions inside of functions.

Step Four

That that we've begun to map out our approach, we can start writing code. If we were to run the snippet we had previously, we'd get all sorts of errors. Comment out all code that breaks our game. We want to start being able to write small bits of code, and get confirmation that we're coding in the right direction one small step at a time.

We don't need to have functions written out, but we do want to text to see if our functions are getting called at the right time. If you don't have them written, you can console.log() them instead.

while(player.health > 0){
    walk();
}

function walk(){
    if(Math.random() > .25){
        fight()
    } else {
        console.log("you have done some peaceful walking")
    }
}
function fight(){
    // enemyCreation();
    const response = readline.question("would you like to run or fight?")
    if (response === "run"){
        console.log("run()");
    } else {
        console.log("enemyAttack()");
    }
}

You'll end up doing a bit of step 3, and then a bit of step 4. This is ok!!