HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Hunt the wumpus: Functional JavaScript (Node) with Lodash

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
wumpusthelodashjavascriptnodewithfunctionalhunt

Problem

I'd like a code review of the following simple implementation of the classic game Hunt the Wumpus

I'm trying to use JavaScript in the most "functional" way. To help me, I'm using lodash and ES6.

``
const _ = require('lodash'),

ROOMS = 20,
ROOMS_TUNNELS = 3,
BATS = 2,
PITS = 2,
ARROWS = 5,

HELP =

Welcome to "Hunt the Wumpus"
The wumpus lives in a cave of 20 rooms. Each room has 3 tunnels to
other rooms. (Look at a dodecahedron to see how this works. If you
don't know what a dodecahedron is, ask someone.)
Hazards:
Bottomless pits - Two rooms have bottomless pits in them. If you go
there, you fall into the pit (& lose)!
Super bats - Two other rooms have super bats. If you go there, a
bat grabs you and takes you to some other room at random (which
may be troublesome).
Wumpus:
The wumpus is not bothered by hazards. (He has sucker feet and is
too big for a bat to lift.) Usually he is asleep. Two things
wake him up: your shooting an arrow, or your entering his room.
If the wumpus wakes, he moves one room or stays still.
After that, if he is where you are, he eats you up and you lose!
You:
Each turn you may move or shoot a crooked arrow.
Moving: You can move one room (through one tunnel).
Arrows: You have 5 arrows. You lose when you run out.
You can only shoot to nearby rooms.
If the arrow hits the wumpus, you win.
Warnings:
When you are one room away from a wumpus or hazard, the computer
says:
Wumpus: "You smell something terrible nearby."
Bat : "You hear a rustling."
Pit : "You feel a cold wind blowing from a nearby cavern."
`;

///////////////////
// LODASH IMPORT //
///////////////////

// import all lodash functions to the main namespace, but isNaN not to cause conflicts
_.each(_.keys(_), k => global[k === 'isNaN' ? '_isNaN' : k] = _[k]);

///////////
// WORLD //
///////////

// create a cave of 20 interconnected rooms. ea

Solution

You have a ton of code in your processInput function, making it lengthy and hard to read. I'd also recommend using an object with string values as keys, like the below, rather than creating large chains of if/else if/else statements.

let choices = {
    "move": moveFunction,
    ...
}


You can then do something like this to run the function based on player input.

if(choices.hasOwnProperty(processInput.awaiting)) {
    choices[processInput]();
}
else {
    console.log("Say again?");
}


Finally, I'd also recommend adding a few more comments to this code. I personally use JSDoc, but if you find another style, that's okay.

Anyways, other than that, your code looks great! Keep up the good work!

Code Snippets

let choices = {
    "move": moveFunction,
    ...
}
if(choices.hasOwnProperty(processInput.awaiting)) {
    choices[processInput]();
}
else {
    console.log("Say again?");
}

Context

StackExchange Code Review Q#89972, answer score: 3

Revisions (0)

No revisions yet.