goose-game
Version:
A lightweight npm library to simulate and play the classic Goose Game with customizable rules and boards
37 lines (33 loc) • 1.1 kB
JavaScript
const dices = require("dices"); // scaricata come tramite npm install
const {mapping} = require("./boxes"); // import di un file diverso
let players = [];
let boxes = 70;
let map = null;
exports.start = (inputBoxes = 70, inputPlayers = ["Mario", "Carlo", "Giovanni", "Dario"], inputMapping = mapping) => {
players = inputPlayers;
boxes = inputBoxes;
map = inputMapping;
players = players.map(item => {
return {nick: item, currentBox: 0}
})
return players;
}
exports.run = () => {
players = players.map(item => {
const dice = dices.extract();
item.currentBox = item.currentBox + dice > boxes ? (boxes - ((item.currentBox + dice) % boxes)) : item.currentBox + dice;
return item;
}).map(item => {
if (item.currentBox in map) {
item.currentBox = item.currentBox + map[item.currentBox];
}
return item;
})
return players;
}
exports.somebodyWon = () => {
const winnerPlayer = players.find(item => item.currentBox === boxes);
if (winnerPlayer) {
return winnerPlayer.nick;
}
}