Find Your Hat Unusual Characters

Hi all,

Every so often when I run my “Find Your Hat” program, node seems to mess up rendering some of the tile characters and would return this (�) instead. Has this happened for anyone else?

Here is my code.

const prompt = require('prompt-sync')({sigint: true}); const hat = '^'; const success = '≜'; const hole = 'O'; const fieldCharacter = '░'; const pathCharacter = '*'; const field = []; const fieldElements = [fieldCharacter, hole, fieldCharacter, fieldCharacter, fieldCharacter]; let pos = 0; let gameOver = false; class Assets { constructor (height, width) { this._height = height; this._width = width; } generateField() { for (let h = 0; h < this._height; h++) { for (let i = 0; i < this._width; i++) { field.push(fieldElements[Math.floor(Math.random()*fieldElements.length)]) } field.push('\n') } const rnd = Math.floor(Math.random()*field.length); pos += Math.floor(Math.random()*field.length); if (field[rnd] !== '\n' && field[rnd] !== pathCharacter) { field.splice(rnd, 1, hat); } if (field[pos] !== '\n' && field[pos] !== hat) { field.splice(pos, 1, pathCharacter); } return field.join(''); } printField() { return field.join(''); } moveDown() { field.splice(pos, 1, fieldCharacter); pos += (this._width + 1); } moveUp() { field.splice(pos, 1, fieldCharacter); pos -= (this._width + 1); } moveLeft() { field.splice(pos, 1, fieldCharacter); pos--; } moveRight() { field.splice(pos, 1, fieldCharacter); pos++; } playGame() { this.generateField(); console.log(this.printField()); while (!gameOver) { let moveInput = prompt('Move to where? (up = w, down = s, left = a, right = d)'); if (moveInput === 's') { this.moveDown(); if (field[pos] === hole) { console.log(this.printField()); return 'You fell in a hole'; gameOver = true; } else if (field[pos] === hat) { field.splice(pos, 1, success); console.log(this.printField()); return 'Congrats! You found your hat'; gameOver = true; } else if (field[pos] !== hole && field[pos] !== hat && field[pos] !== fieldCharacter) { console.log(this.printField()); return 'You fell out of bounds'; gameOver = true; } else { field.splice(pos, 1, pathCharacter); console.log(this.printField()); } } if (moveInput === 'w') { this.moveUp(); if (field[pos] === hole) { console.log(this.printField()); return 'You fell in a hole'; gameOver = true; } else if (field[pos] === hat) { field.splice(pos, 1, success); console.log(this.printField()); return 'Congrats! You found your hat'; gameOver = true; } else if (field[pos] !== hole && field[pos] !== hat && field[pos] !== fieldCharacter) { console.log(this.printField()); return 'You fell out of bounds'; gameOver = true; } else { field.splice(pos, 1, pathCharacter); console.log(this.printField()); } } if (moveInput === 'a') { this.moveLeft(); if (field[pos] === hole) { console.log(this.printField()); return 'You fell in a hole'; gameOver = true; } else if (field[pos] === hat) { field.splice(pos, 1, success); console.log(this.printField()); return 'Congrats! You found your hat'; gameOver = true; } else if (field[pos] !== hole && field[pos] !== hat && field[pos] !== fieldCharacter) { console.log(this.printField()); return 'You fell out of bounds'; gameOver = true; } else { field.splice(pos, 1, pathCharacter); console.log(this.printField()); } } if (moveInput === 'd') { this.moveRight(); if (field[pos] === hole) { console.log(this.printField()); return 'You fell in a hole'; gameOver = true; } else if (field[pos] === hat) { field.splice(pos, 1, success); console.log(this.printField()); return 'Congrats! You found your hat'; gameOver = true; } else if (field[pos] !== hole && field[pos] !== hat && field[pos] !== fieldCharacter) { console.log(this.printField()); return 'You fell out of bounds'; gameOver = true; } else { field.splice(pos, 1, pathCharacter); console.log(this.printField()); } } } } } const newField = new Assets(15, 40); console.log(newField.playGame());

I am by no means an expert on this, but have you checked if it is the same characters it returns the � character for?
It could also be something with the character encoding.

It’s usually replacing the fieldCharacters. Figure it’s just an occasional encoding glitch. I was just curious if anyone else had that issue.