JavaScript to TypeScript in Node.js

A maze needs to be read

Your browser needs to be JavaScript capable to view this video

Try reloading this page, or reviewing your browser settings

In this video segment, you will see how we finished our cli application, a functional game that let’s the end user walk into a maze.

Keywords

  • cli
  • commander
  • loop
  • maze
  • game
  • cli application

Conflict of Interest

The author declares no conflict of interest.

About this video

Author(s)
Joris Hermans
First online
13 May 2022
DOI
https://doi.org/10.1007/978-1-4842-8203-8_9
Online ISBN
978-1-4842-8203-8
Publisher
Apress
Copyright information
© The Editor(s) (if applicable) and The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2022

Video Transcript

Hi. Welcome to JavaScript to TypeScript in Node.js. This is the fourth video, A maze needs to be read.

What will you learn? Well, you will learn how to read a file. And we will implement a maze game. I will show you how you could do it, because I already prepared the game– because it’s a lot of code– but I’m going to walk you through it, and I’m going to show you how to do it right.

So are you with me? Well, let’s go.

So I’ve prepared already something for you guys. Here you have a multidimensional array. It’s 1.json. And we’re going to read it in.

So within Node.js you have a library called fs, which is file system. Because we want to use promises, well, you have that possibility in Node.js, right? And how are we going to do that? Well, you have fs.promises. And then we can do readFile. Then we have a promise, right?

So that’s something that I can recommend you. Because otherwise you have fs, if you go here, and I type here in, fs.readFile, you will see here that you have a path, and then you have some options, and then you have a callback. But of course, callback and callback hell. That’s not great, right? So I prefer to use the promises. It’s a much modern way to do so.

And then you can very easily use async wait to use it within your application. And when we have the result back, we can do JSON.parse of the result. And then we have an end result. And then we can return that end result to our end users. So that’s how we can read the 1.json, right? And I provide here a level, which can be 1, 2, 3, 4, or 5.

So if you go to greetings, I already created something. I created a method called navigation. So first of all, we’re going to read the level, right? And then we have the JSON file, which is the multi-dimensional area. And of course, we need to have an x and a y, so we can navigate through our maze, right?

And I created a function, navigation, where we’re going to ask the question, what is the direction that you want to go? And then of course, I want to see what he typed. So we typed R, for right, and then we can check if he can go right. And then we can adjust the x or we can adjust the y, right, when we have current.up or current.down.

So these are great possibilities that we can do. And then, of course, we have an else, where we can see that we cannot go left or we cannot go up or we cannot go down when you have a false within our .json, file.

So this is just a function. So at the end of it, we always need to ask the function. And whenever a user types Q, we’re going to exit our application and we’re going to end it, right?

Also, for that case, we also need to add– and I don’t think I did it already– the Q. So I can add here Q, so that the end user also knows that he can type Q for quit, right?

All right. So if we now type in node dot, you will see that I had a console.log for our file. If we go to levels.js, you will see here that we have a console.log for end result. Well, you can very easily comment it out so that you don’t see what you got from 1.json, right? But OK. That’s just fine.

So I’m going to say I’m going to go right. And that’s just nice. We can go right, and then we can go right, and we can go right. But then he says, hey, I cannot go right. So we ended up into a wall, right? If I try to go up or I try to go down, well, we cannot do that.

So we can go left and we can go left and we can go left, but then we cannot go left anymore. So we can try to go up or we can try to go down. And hey, we can go down again. So that’s something to do to try to navigate yourself through the maze.

So yeah, I don’t know. I lost a little bit in through the maze. But yeah, you got what we created, right? We created a little game for our end users. And we redid our levels within the .json file.

So what have you learned? Well, we have learned how to read a file. And I also explain to you how to implement the maze game– or a maze game– that you can ask a question and that you can react upon it, and then you can ask the same question again, and you can react on it, and so on. So I hope you enjoyed that.

So what have you learned? Well, we have learned how to create and start with a simple Node.js application and to parse the arguments. Then we saw how to add interaction with the end user. We also made it a little bit nicer with using colors, a box, and styling, right? And at the end, we implemented a maze that we could read, right?

So now we have a little game that we can work further upon within this course. I hope you enjoyed it. And in the next video, we will see how we can get some kind of information from a web server. See you then.