Last active
December 7, 2020 06:11
-
-
Save pombadev/663c5729f5488ce6aaa603d57f32f2dd to your computer and use it in GitHub Desktop.
Snakes and Ladders (aka chutes and ladders) game
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const range = (limit = 0) => [...Array(limit)].map((_, i) => i) | |
const random = (down = 0, up = 0) => Math.floor(Math.random() * (up - down + 1)) | |
class Player { | |
position = 0; | |
constructor(name) { | |
this.name = name; | |
} | |
move(next) { | |
this.position += next; | |
} | |
climb(up) { | |
this.move(up); | |
} | |
fall(down) { | |
this.position -= down; | |
} | |
} | |
class Game { | |
state = 'notStarted'; | |
constructor(players = 2) { | |
this.board = range(100).reduce((acc, curr) => { | |
if (curr === 4) { | |
acc[curr] = 25; | |
} else if (curr === 13) { | |
acc[curr] = 46; | |
} else if (curr === 33) { | |
acc[curr] = 49; | |
} else if (curr === 42) { | |
acc[curr] = 63; | |
} else if (curr === 50) { | |
acc[curr] = 69; | |
} else if (curr === 62) { | |
acc[curr] = 81; | |
} else if (curr === 74) { | |
acc[curr] = 92; | |
} else if (curr === 27) { | |
acc[curr] = -4; | |
} else if (curr === 40) { | |
acc[curr] = -3; | |
} else if (curr === 43) { | |
acc[curr] = -18; | |
} else if (curr === 53) { | |
acc[curr] = -31; | |
} else if (curr === 65) { | |
acc[curr] = -45; | |
} else if (curr === 76) { | |
acc[curr] = -58; | |
} else if (curr === 91) { | |
acc[curr] = -53; | |
} else if (curr === 99) { | |
acc[curr] = -41; | |
} else { | |
acc[curr] = curr; | |
} | |
return acc; | |
}, {}); | |
this.players = range(1, players + 1).reduce((acc, cur) => { | |
acc[cur] = new Player(cur); | |
return acc; | |
}, {}); | |
} | |
roll() { | |
return random(1, 6); | |
} | |
play(player) { | |
this.state = 'playing'; | |
console.log('Player: %d', player); | |
// window.confirm(); | |
const dice = this.roll(); | |
console.log(`${player} rolled: ${dice}`); | |
const currentPlayer = this.players[player]; | |
const playerPosition = this.board[currentPlayer.position]; | |
console.log(`${player} in: ${playerPosition}`); | |
const nextPosition = playerPosition ? this.board[playerPosition + dice] : dice; | |
console.log(`${player} next: ${nextPosition}`); | |
if (nextPosition === 100) { | |
this.state = 'over'; | |
return console.log('%o is the winner', currentPlayer); | |
} | |
if (nextPosition > 0) { | |
currentPlayer.move(dice); | |
} else { | |
currentPlayer.fall(Math.abs(dice)); | |
} | |
if (dice === 6) { | |
this.play(player); | |
} | |
} | |
} | |
let turn = 1; | |
let round = 0; | |
const players = 2; | |
const game = new Game(players); | |
while (game.state !== 'over') { | |
console.log('========= Start ===================='); | |
console.log(`Round: ${Math.floor(round / 2)}, player's ${turn}, turn`); | |
game.play(turn); | |
if (turn === 1) { | |
turn = 2; | |
} else { | |
turn = 1; | |
} | |
round++; | |
console.log('=========== End =================='); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment