Functional JavaScript: Writing a Simple Command-line Game (Part 5)

Functional JavaScript: Writing a Simple Command-line Game (Part 5)

Functional JavaScript: Writing a Simple Command-line Game (Part 5). We are at the end of our journey, for now. In this last part, we will quickly look at the maze code. I hope you enjoyed this section and simple get a feel for what you can accomplish in JavaScript without using a whole host of third-party libraries.

We are at the end of our journey, for now. In this last part, we will quickly look at the maze code. The plan here is not to dive in with great detail but to focus on some essential aspects. Firstly we create a room type.

// room.js
const room = function( attrib ){

    attrib = attrib || {};
    const dv = (value,def ) => (typeof value === "undefined")?def:value;
    const _name = dv( attrib.name, "<lost>");
    const _msg = dv( attrib.msg, "");
    const _doors = dv( attrib.doors, {} );
    let _monster = dv( attrib.monster, null );
    let _item = dv( attrib.item, null );

    function name() {
        return _name;
    }
    function monster() {
        const m = _monster;
        _monster = null;
        return m;
    }
    function doors() {
        return _doors;
    }
    function message() {
        return _msg;
    }
    function item() {
        const i = _item;
        _item = null;
        return i;
    }
    return { name, monster, doors, item, message, isRoom : true };
}

module.exports = room;

By this time, the pattern should be feeling very natural; we have created an object that encapsulates the private data for a room, and we have implemented a set of helper methods. We only want the adventurer to fight the monster in the room once, so the monster method ‘nulls’ out the monster. The same happens to items in the room.

javascript nodejs programming

What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Object-oriented Programming in JavaScript: Made Super Simple. DO NOT MISS!!!

Object-oriented Programming in JavaScript: Made Super Simple. Learn all about objects, prototypes, prototypical inheritance, this and more.

Decoding Nodejs

The main goal of this blog is to explain the “Architecture of Nodejs” and to know how the Nodejs works behind the scenes. Generally, most of the server-side languages, like PHP, ASP.NET, Ruby, and including Nodejs follows multi-threaded architecture. That means for each client-side request initiates a new thread or even a new process.

Hire NodeJs Developer

Looking to build dynamic, extensively featured, and full-fledged web applications? **[Hire NodeJs Developer](https://hourlydeveloper.io/hire-dedicated-node-js-developer/ "Hire NodeJs Developer")** to create a real-time, faster, and scalable...

Hire Dedicated JavaScript Developers -Hire JavaScript Developers

Hire dedicated JavaScript Developers who are proficient in AngularJS, ReactJS, NodeJS, & VueJS frameworks. Get flexible hiring models as per your business requirements.

Learning JavaScript: Development Environments for JavaScript Programming

One of the nice things about learning JavaScript these days is that there is a plethora of choices for writing and running JavaScript code. In this article, I’m going to describe a few of these environments and show you the environment I’ll be using in this series of articles.