Game = function() {
const localboard = Object.assign({}, board)
const actions = ["skip", "red", "hole", "red", "red", "green"]
const spaces = board.spaces.filter(d => d.label != "trap"),
traps = board.spaces.filter(d => d.label == "trap")
const getSpaceByIndex = index => board.spaces.filter(d => d.index == index)[0],
getExit = (trap, roll) => trap[actions[roll - 1]],
getDestination = (space, roll) => {
return space.index + roll < spaces.length ? getSpaceByIndex(space.index + roll) : space
},
getTrap = (hole, roll) => getSpaceByIndex(hole.trapIndex)
const getSpaceUtil = {
"start": (color) => getSpaceByIndex(color+"Start"),
"space": (space, roll) => space.index + roll < spaces.length ? getSpaceByIndex(space.index + roll) : space,
"hole": (hole, roll) => getSpaceByIndex(hole.trapIndex),
"trap": (trap, roll) => trap[actions[roll - 1]]
}
const getNewDest = (space, roll) => getSpaceUtil[space.label](space, roll)
const board = [
{
index: 4,
type: "hole",
trapIndex: "trap1",
move: function(roll) {
for (let i = 0; i< roll; i++) {
hops.push()
}
const temp = getSpaceUtil[this.type](this, roll)
return (temp && temp.fall) ? temp.fall() : this
},
enter: function() {
return getSpaceByIndex(this.trapIndex)
}
}, {
index: 1,
type: "space",
move: function(roll) {
return getSpaceUtil[this.type](this, roll)
}
}, {
index: "trap1",
type: "trap",
"red": 0,
green: 8,
hole: 4,
skip: this.index,
move: function(roll) {
return getSpaceUtil[this.type](this, roll)
}
}]
//initialize game state, if null
const spec = {}
const init = function() {
spec.state = {
players: players.map(d => {
return {
"color": d,
"space" : getSpaceByIndex(0),
"lostTurns": 0
}
}),
currentPlayer: 0,
roll:"",
hops:[]
}
shell.update(spec.state)
return spec.state
}
init() //initialize
//return method to take a turn and pass to next player
return {
init: init,
takeTurn: function (roll) {
const {players, currentPlayer} = spec.state,
action = actions[roll - 1]
//find player's current location
const {space, color, lostTurns} = players[currentPlayer],
moves = [],
hops = []
let newDestination = false //in case the piece doesn't move
//move or escape a trap
if (space.hasOwnProperty("label")) {
// players[currentPlayer].space = players[currentPlayer].destination
if (space.index == "trap8") { //the cage
//lose 2 turns
if (lostTurns == 2) {
newDestination = getSpaceByIndex(space["red"])
hops.push({
color: color,
move: [space, newDestination]})
}
players[currentPlayer].lostTurns = lostTurns++ % 3 //reset after 2 lost turns
} else if (space.label == "trap") {
//exit the trap if you rolled
console.log(action)
if (action != "skip") {
newDestination = getSpaceByIndex(space[action])
hops.push({
color: color,
move: [space, newDestination]})
} else {
// newDestination = space
//piece doesn't hop
}
} else {
//you're on the board
//if you didn't toll too high
const rolledIndex = space.index + roll
if (rolledIndex < spaces.length) {
//move piece
newDestination = getSpaceByIndex(rolledIndex)
for (let i = 0; i < roll; i++) {
hops.push({
color: color,
move: [getSpaceByIndex(space.index + i) , getSpaceByIndex(space.index + i + 1)]})
}
}
}
if (newDestination != false) {
//check if another piece was kicked out
const kickedOutPiece = players.filter(d => d.space.index == newDestination.index)
kickedOutPiece.map(piece => {
const startingSpace = getSpaceByIndex(0)
//update kickedout player state
const kickedOutindex = players.findIndex(d => d.color == piece.color)
players[kickedOutindex].space = startingSpace
//add a hop
hops.push({
color: piece.color,
move: [piece.space, startingSpace]})
})
//check if the piece feel in a hole, but not if they exited a trap
if (newDestination.label == "hole" && space.label != "trap") {
const trap = getSpaceByIndex(newDestination.trapIndex)
console.log(trap)
newDestination = trap
hops.push({
color: color,
move: [newDestination, trap]})
}
//update player's space
players[currentPlayer].space = newDestination
}
//did you win?
if (newDestination.index == "trap7") {
// if (players[currentPlayer].destination.index == "trap7") {
//change game state
console.log(`${players[currentPlayer].color} won!!` )
} //you won
}
//update game state
//todo: need to highlight the next piece AFTER the previous piece moved
spec.state = {
players: players,
currentPlayer: (spec.state.currentPlayer + 1) % players.length,
roll: [roll, action],
hops: hops
}
shell.update(spec.state)
return spec.state
}
}
}