Skip to content

Instantly share code, notes, and snippets.

@avdotion
Forked from killreal17/main.js
Last active January 28, 2019 16:55
Show Gist options
  • Save avdotion/ef1834f62379f73238b09c722d472e1e to your computer and use it in GitHub Desktop.
Save avdotion/ef1834f62379f73238b09c722d472e1e to your computer and use it in GitHub Desktop.
15 puzzle
'use strict'
const readline = require('readline')
const input = readline.createInterface({
input: process.stdin,
output: process.stdout
})
input.setPrompt('> ')
const Fifteen = (size) => {
const setWidth = (convertedNumber, width) => {
while (convertedNumber.length < width) {
convertedNumber = ' ' + convertedNumber
}
return convertedNumber
}
const f = {}
f.size = size || 4
f.blankTileSymbol = setWidth(
' ',
(f.size * f.size - 1).toString().length
)
f.blank = {
x: f.size - 1,
y: f.size - 1
}
f.board = new Array(f.size)
for (let i = 0; i < f.size; i++) {
f.board[i] = new Array(f.size)
for (let j = 0; j < f.size; j++) {
f.board[i][j] = setWidth(
(f.size * i + j + 1).toString(),
(f.size * f.size - 1).toString().length
)
}
}
f.board[f.blank.x][f.blank.y] = f.blankTileSymbol
f.score = 0
f.moveTile = (tile) => {
f.board[f.blank.y][f.blank.x] = f.board[tile.y][tile.x]
f.board[tile.y][tile.x] = f.blankTileSymbol
f.blank = tile
f.score++
}
f.moveUp = () => {
if (f.blank.y > 0) {
f.moveTile({ x: f.blank.x, y: f.blank.y - 1 })
}
}
f.moveDown = () => {
if (f.blank.y < 3) {
f.moveTile({ x: f.blank.x, y: f.blank.y + 1 })
}
}
f.moveLeft = () => {
if (f.blank.x > 0) {
f.moveTile({ x: f.blank.x - 1, y: f.blank.y })
}
}
f.moveRight = () => {
if (f.blank.x < 3) {
f.moveTile({ x: f.blank.x + 1, y: f.blank.y })
}
}
f.draw = () => {
process.stdout.write('\u001b[2J\u001b[0;0H')
f.board.forEach((row) => {
console.log(row)
})
console.log(`Player Score: ${f.score}`)
}
f.shuffle = (seed) => {
const chooseRandomDirection = () => {
const directions = [f.moveUp, f.moveDown, f.moveRight, f.moveLeft]
const index = Math.floor(Math.random() * 4)
return directions[index]
}
for (let i = 0; i < seed; i++) {
const randomDirection = chooseRandomDirection()
randomDirection()
}
f.score = 0
}
f.isCompleted = () => {
for (let i = 0; i < f.size; i++) {
for (let j = 0; j < f.size; j++) {
if (f.board[i][j] !== f.blankTileSymbol) {
const a = setWidth(
(f.size * i + j + 1).toString(),
(f.size * f.size - 1).toString().length
)
if (f.board[i][j] !== a) {
return false
}
} else if (i * f.size + j + 1 !== f.size * f.size) {
return false
}
}
}
return true
}
return f
}
const fifteen = Fifteen(4)
while (fifteen.isCompleted()) {
fifteen.shuffle(100)
}
fifteen.draw()
input.prompt()
input.on('line', reply => {
if (reply === 'exit') {
input.close()
} else if (reply === 'w') {
fifteen.moveUp()
} else if (reply === 's') {
fifteen.moveDown()
} else if (reply === 'a') {
fifteen.moveLeft()
} else if (reply === 'd') {
fifteen.moveRight()
}
fifteen.draw()
if (fifteen.isCompleted()) {
console.log(`Congratulations! Your score is ${fifteen.score}!`)
input.close()
}
input.prompt()
}).on('close', () => {
process.exit(0)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment