Find Your Hat Challenge Project (JavaScript)

This was a hard one! I did have to glance at the solution for help sometimes. :confused:

1 Like

It has cost me a lot to do it in relation to the previous exercises, but after 2 days and a lot of coffee I managed to finish it without help.
Painting the field was easy, the hard part was moving the path character and updating the field correctly with the changes.
(I didn’t follow the instructions as I found them unintuitive in this case).

const prompt = require('prompt-sync')({ sigint: true });

const hat = '^';
const hole = 'O';
const fieldCharacter = '░';
const pathCharacter = '*';

class Game {
    constructor(arr) {
        this.field = arr
        this.pos = { h: 0, w: 0 }
        this.len = { h: this.field.length, w: this.field[0].length }
        this.end = false
    }

    print() {
        for (let i = 0; i < this.field.length; i++) {
            console.log(this.field[i].join(''))
        }
    }
    static generateField(height, width, maxHoleProb) {
        const outerArr = []
        for (let i = 0; i < height; i++) {
            const innerArr = []
            for (let j = 0; j < width; j++) {
                const randomHoleProb = Math.random()
                if (randomHoleProb < maxHoleProb) {
                    innerArr.push(hole)
                } else {
                    innerArr.push(fieldCharacter)
                }
            }
            outerArr.push(innerArr)
        }
        // set random position for hat
        const hatPos = {}
        hatPos.h = Math.floor(Math.random() * height)
        hatPos.w = Math.floor(Math.random() * width)
        outerArr[hatPos.h][hatPos.w] = hat
        // set initial path
        outerArr[0][0] = pathCharacter
        return outerArr
    }
    input() {
        const key = prompt()
        const validKey = function (key) {
            return String(key).toLowerCase()
        }
        const vKey = validKey(key)
        if (vKey === 'w' || vKey === 'a' || vKey === 's' || vKey === 'd') {
            return vKey
        }
        console.log('Invalid key, try again')
    }
    move(key) {
        if (key === 'w') {
            this.pos.h--
        } else if (key === 's') {
            this.pos.h++
        } else if (key === 'a') {
            this.pos.w--
        } else {
            this.pos.w++
        }
    }
    checkMove() {
        const isOut = () => {
            if (this.pos.h < 0 || this.pos.w < 0 || this.pos.h >= this.len.h || this.pos.w >= this.len.w) {
                return true
            }
            return false
        }
        const isHole = () => {
            if (this.field[this.pos.h][this.pos.w] === hole) {
                return true
            }
            return false
        }
        const isHat = () => {
            if (this.field[this.pos.h][this.pos.w] === hat) {
                return true
            }
            return false
        }
        const isField = () => {
            if (this.field[this.pos.h][this.pos.w] === fieldCharacter) {
                return true
            }
            return false
        }

        if (isOut()) {
            console.log('Out of boundaries. Game Over')
            this.end = true
        } else if (isHole()) {
            console.log('Fall in the hole. Game Over')
            this.end = true
        } else if (isHat()) {
            console.log('You win!!')
            this.end = true
        } else if (isField()) {
            console.log('Moving...')
            this.field[this.pos.h][this.pos.w] = pathCharacter
        } else {
            console.log("You can't go back. Game Over")
            this.end = true
        }
    }
    run() {
        console.log('Find your hat')
        console.log('wasd ↑←↓→ to move, Ctrl+C to exit ')
        this.print() // generated field
        while (true) {
            this.move(this.input())
            this.checkMove()
            this.print()
            if (this.end) {
                break
            }
        }
    }

}

function play() {
    const game = new Game(Game.generateField(10, 20, 0.2))
    game.run()
}
play()

Solution Find Your Hat

Hi all,

Finished this challenge some time ago. Thought I should post it here. Was fun!

Here’s my code: GitHub.

I’ve added some extra functionalities just for kicks:

  • user can generate field;
  • mock countdown to “wait” for construction of the field;
  • addition of arrows to represent the player’s previous movements in the field;
  • a Score class that keeps track of the player’s results and prints them when the player leaves.

Check out README.md for further details. At the bottom, it includes a little .gif that illustrates the game play. Hope it can be of use to others. Feedback is always welcome!