Public
Edited
Jan 6, 2024
Fork of Mausefalle
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Game = function() {
//create local copy of the board
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) {
//roll hops
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
}
}
}

Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
<svg>
${mausgroup}
</svg>
Insert cell
mausgroup = {
return htl.html`<g>
<path d="M68.1088 154.503C69.7223 151.814 70.4244 147.855 70.5738 146.211L89.1739 150.021C89.398 151.515 88.9498 155.713 85.3643 160.553C81.7787 165.394 76.8486 168.397 74.8317 169.293L77.0727 174.447C77.8197 175.269 77.5657 177.45 70.5738 179.602C61.834 182.291 54.2147 170.638 56.9039 166.38C59.5931 162.122 66.0919 157.864 68.1088 154.503Z" fill="#784623" stroke="black" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"/>
<path d="M127.495 161.002C124.581 156.968 111.808 147.332 102.172 146.659L118.083 130.973L130.856 143.97C133.097 143.223 137.982 142.177 139.596 143.97C141.613 146.211 142.733 147.78 142.285 151.365C141.837 154.951 130.408 165.035 127.495 161.002Z" fill="#784623" stroke="black" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"/>
<path d="M142.061 95.5651C135.428 101.84 123.311 102.662 118.083 102.288L109.791 83.4639C112.555 84.5096 119.562 86.6012 125.478 86.6012C131.394 86.6012 140.343 83.4639 144.078 81.8952C148.336 79.5795 156.089 78.1303 153.042 90.8591C149.232 106.77 150.353 87.7217 142.061 95.5651Z" fill="#784623" stroke="black" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"/>
<path d="M44.8026 119.544L51.0774 103.409L59.369 118.423C58.6967 120.44 56.5453 125.37 53.3183 128.956C49.2846 133.438 42.5616 136.127 37.8556 128.956C34.0908 123.219 40.9183 120.291 44.8026 119.544Z" fill="#784623" stroke="black" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"/>
<path d="M79.0896 15.7864C79.0896 19.1927 76.251 27.066 74.8317 30.5768L66.9883 33.0419C63.8509 29.531 57.3969 22.0163 56.6798 20.0443C55.7834 17.5792 56.2316 7.71889 58.9208 4.80562C61.6099 1.89235 69.4534 1.44416 71.6943 2.56464C73.9353 3.68513 79.0896 11.5285 79.0896 15.7864Z" fill="#784623" stroke="black" stroke-width="4"/>
<path d="M45.2508 33.7142C46.685 35.3277 55.8581 37.8227 60.2653 38.8684L67.4365 32.3696C66.3907 28.0371 63.9405 18.7893 62.5063 16.4587C60.7135 13.5454 49.9569 10.184 47.0436 10.4081C44.1303 10.6322 38.9761 15.1141 38.5279 19.372C38.0797 23.6298 43.458 31.6973 45.2508 33.7142Z" fill="#784623" stroke="black" stroke-width="4"/>
<path d="M2 118.647C19.4191 145.091 48.4629 142.103 58.6967 137.247L62.9546 142.626C50.6292 148.004 20.6001 146.883 2 118.647Z" fill="black" stroke="black" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"/>
<path d="M77.2968 29.9045C97.6897 34.3118 138.879 43.8882 140.492 46.936C142.355 50.4539 128.93 75.3738 115.393 80.5815C115.092 80.6972 114.792 80.8033 114.491 80.8991C113.366 81.4628 112.032 82.7664 112.032 85.2566C112.032 89.2904 123.909 104.529 122.564 117.975C121.22 131.421 105.085 153.382 80.6583 150.021C56.2316 146.659 53.0943 123.353 53.0943 122.905C53.0943 122.457 47.2677 105.65 56.0075 87.2735C64.7473 68.8975 41.4412 69.3457 53.0943 48.9528C62.4167 32.6385 73.1136 29.4563 77.2968 29.9045Z" fill="#EC8532"/>
<path d="M112.032 81.447C113.147 81.3097 114.271 81.0134 115.393 80.5815M115.393 80.5815C128.93 75.3738 142.355 50.4539 140.492 46.936C138.879 43.8882 97.6897 34.3118 77.2968 29.9045C73.1136 29.4563 62.4167 32.6385 53.0943 48.9528C41.4412 69.3457 64.7473 68.8975 56.0075 87.2735C47.2677 105.65 53.0943 122.457 53.0943 122.905C53.0943 123.353 56.2316 146.659 80.6583 150.021C105.085 153.382 121.22 131.421 122.564 117.975C123.909 104.529 112.032 89.2904 112.032 85.2566C112.032 82.0296 114.273 80.7953 115.393 80.5815Z" stroke="black" stroke-width="4"/>
<path d="M91.2076 50.2974C91.2076 54.9708 87.6056 58.6059 83.3474 58.6059C79.0891 58.6059 75.4871 54.9708 75.4871 50.2974C75.4871 45.6241 79.0891 41.9889 83.3474 41.9889C87.6056 41.9889 91.2076 45.6241 91.2076 50.2974Z" fill="white" stroke="black" stroke-width="4"/>
<path d="M70.5738 45.8155C70.1816 47.3842 71.0967 54.8541 72.8148 57.9167C72.8148 49.3114 85.0655 42.3793 91.1908 39.9889C88.5016 38.9432 82.451 36.9412 79.7618 37.2998C76.4004 37.748 71.6942 41.3335 70.5738 45.8155Z" fill="#EC8532" stroke="black" stroke-width="3" stroke-linejoin="round"/>
<circle cx="79.0895" cy="54.5553" r="4.70605" fill="black"/>
<circle cx="138.475" cy="49.401" r="3.13737" fill="black"/>
<path d="M132.649 50.7456C125.03 52.9866 116.066 54.3312 109.343 48.0564" stroke="black" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"/>
<path d="M134.442 53.6589C119.875 62.1746 112.928 61.0541 104.189 58.1408" stroke="black" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"/>
<path d="M136.01 55.8999C128.615 63.7433 123.013 68.4493 110.015 68.4493" stroke="black" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"/>
<path d="M139.372 45.3673C142.957 41.1094 143.181 36.6275 142.509 33.9383" stroke="black" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"/>
<path d="M140.268 47.8323C142.584 47.0854 147.932 44.202 150.801 38.6443" stroke="black" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"/>
<path d="M140.492 50.0733C142.36 50.5215 147.26 50.7904 151.921 48.2805" stroke="black" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"/>
<path d="M99.4825 73.6036C99.9399 75.5475 99.6387 77.3748 98.9661 78.7578M96.3451 81.2229C97.3328 81.0034 98.3205 80.0852 98.9661 78.7578M98.9661 78.7578C99.8852 80.3265 102.978 83.2398 107.998 82.3434C114.273 81.2229 115.393 81.2229 118.307 78.7578" stroke="black" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"/>
</g>`
}
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more