Find my hat

Hey folks,

I have created this command line game using JS. Do check it out and let me know if you had fun. (GitHub - dhyani15/find-my-hat)

This one took some time to create because of so many moving parts, would appreciate a second set of eyes to review the code for any improvements. If you have any ideas that can be added in the game, you are more than welcome to suggest them!


1 Like

Nicely done! I really like how you’ve set this up!

There are a couple errors I found that you might want to look at.
The first I saw was that you want to keep the user from leaving the board, but currently your conditions ending the game does not work to keep a user from leaving:

if (i<0 && i>newGame.field.length-1){
  console.log( `You left the board, game over`);
  flag = false;
} else if (j<0 && j>newGame.field[0].length-1){
  console.log( `You left the board, game over`);
  flag = false;

Currently you are using an and operator (&&) in your conditions. This means it’s only trigger if I pass the boundary on both sides of the map, which of course can’t be done. Not only does this mean you can run around outside of bounds on the left and right sides of the board, but trying to go up or down crashes the game as it can’t read the index of an undefined array.

Second thing is that you don’t want the user to retrace their steps but currently you only tell the user this if they run over their path, nothing is actually done to correct it. A couple ways you could go about fixing that are

  1. Don’t update their position if the direction their going is part of their path.
  2. If the direction they went is part of their path, change their position back to were they just were.

Personally I’d go with the first option as it is a lot smoother. So something like:

if (dir === 'd' && newGame.field[i++][j] != pathCharacter) {
  // update position
} else {
  // inform user they can't move there

You could also follow this same strategy to keep a user from going out of bounds without ending the game.

There are a few things you could improve that aren’t quiet errors, or just improvements to help keep it running smoothly.

One thing I’d love to see you do is make use of console.clear() to clear the console before printing the board again, this would make it so you don’t have to keep scrolling down every time the board is printed.

Another is that currently you can end up with an impossible to win game, since holes are placed entirely at random. Often there is no way to make it to the hat. Of course this is a bit of an undertaking to make sure you have a path from start to the hat, but it certainly can be done.

A similar instance is if you make a path in a way that you can’t move without crossing your path again. Now you are stuck but the game still doesn’t end. Perhaps you could create a function to check if there is a space around you that you can move to, and if not end the game.

In your randomChar function, you currently have this condition:

if (index>=0 && index<7){
  randChar = fieldCharacter;

Currently index can’t be less than 10 so you don’t actually need to check if it is more than or equal to 0.

Last thing I noticed is you seem a little inconsistent in your use of semicolons, though they aren’t usually necessary in JS it is still considered best practice to include them at the end of lines.

Overall nicely done! You have a working game, and a great place to build off of in the future if you end up wanting to make something bigger!! I’d love seeing you continue, and possibly make a full game out of it. :slight_smile:


Thanks a lot for reviewing my code. I have made some changes to correct the error for out of bounds and to not end the game when a user retraces its step. To ensure the probability of insolvable puzzles is minimised, I have ensured that there are no more holes near the starting point and the hat coordinate.

Once again I appreciate your feedback, and it helped me gain new perspective. Best regards