Created
October 23, 2022 18:54
-
-
Save kriradevska/bb360447e09eafdc484c1e4140b9efec to your computer and use it in GitHub Desktop.
Project to build an adventure game using reducers, state, and actions.
This file contains 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 reducerWagon = (state = initialWagonState, action) => { | |
switch (action.type) { | |
default: { | |
return state; | |
} | |
case 'gather': { | |
return { | |
...state, | |
supplies: state.supplies + 15, | |
distance: state.distance, | |
days: state.days + 1 | |
} | |
} | |
case 'travel': { | |
if ((state.supplies - (20 * action.payload)) < 0) { | |
return state; | |
} else { | |
return { | |
...state, | |
supplies: state.supplies - (20 * action.payload), | |
distance: state.distance + (10 * action.payload), | |
days: state.days + action.payload | |
} | |
} | |
} | |
case 'tippedWagon': { | |
return { | |
...state, | |
supplies: state.supplies - 30, | |
distance: state.distance, | |
days: state.days + 1 | |
} | |
} | |
} | |
} | |
const initialWagonState = { | |
supplies: 100, | |
distance: 0, | |
days: 0, | |
} | |
// day zero | |
let wagon = reducerWagon(undefined, {}); | |
console.log(wagon); | |
// day one | |
wagon = reducerWagon(wagon, {type: 'travel', payload: 1}); | |
console.log(wagon); | |
// day two | |
wagon = reducerWagon(wagon, {type: 'gather'}); | |
console.log(wagon); | |
// day three | |
wagon = reducerWagon(wagon, {type: 'tippedWagon'}); | |
console.log(wagon); | |
// day six | |
wagon = reducerWagon(wagon, {type: 'travel', payload: 3}); | |
console.log(wagon); | |
// day seven | |
wagon = reducerWagon(wagon, {type: 'travel', payload: 1}); | |
console.log(wagon); | |
wagon = reducerWagon(wagon, {type: 'gather'}); | |
console.log(wagon); | |
// day eight | |
wagon = reducerWagon(wagon, {type: 'travel', payload: 1}); | |
console.log(wagon); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment