Redux Road Project Solution

This is my solution. There are, of course, others. But, this one works perfectly for me.
ReactReview/ExampleReducer.js at master · rickmansfield/ReactReview (github.com)
Hope this helps anyone that needs it.

1 Like

I just post mine here as well:

const initialWagonState = {
  supplies: 100,
  distance: 0,
  days: 0,
  cash: 200,
}

const useReducer = (state = initialWagonState, {type, payload}) => {
  let { supplies, distance, days, cash } = state;
  switch(type) {
    case "gather": { 
      supplies += 15;
      days ++;
      return { ...state, supplies, days }; 
      }
    case "travel": {
      supplies -= (20 * payload);
      distance += (10 * payload);
      days += payload;
      return supplies < 0 ? state : { ...state, supplies, distance, days };
    }
    case "tippedWagon": {  
      supplies = supplies - 30 < 0 ? 0: supplies - 30;
      days += 1;
      return { ...state, supplies, days }; 
    }
    case "sell": { 
      supplies -= 20;
      cash += 5;
      return supplies < 0 ? state : { ...state, supplies, cash }; 
    }
    case "buy": { 
      supplies += 25;
      cash -= 15;
      return cash < 0 ? state : { ...state, supplies, cash }; 
    }
    case "theft": { 
      cash /= 2;
      return { ...state, cash }; 
    }
    default: { return state; }  
  }
}

let wagon = useReducer(undefined, {});
console.log(wagon);
wagon = useReducer(wagon, { type: "travel", payload: 1 })
console.log(wagon);
wagon = useReducer(wagon, { type: "gather" })
console.log(wagon);
wagon = useReducer(wagon, { type: "tippedWagon" })
console.log(wagon)
wagon = useReducer(wagon, { type: "travel", payload: 3 })
console.log(wagon);
wagon = useReducer(wagon, { type: "buy" })
console.log(wagon);
wagon = useReducer(wagon, { type: "theft" })
console.log(wagon);
wagon = useReducer(wagon, { type: "sell" })
console.log(wagon);

this was fun! i think i’ll build this one out and give it a nice UI to use for my portfolio. i’ll need to make a factory fx though bc hard coding the actions at the bottom isn’t a viable solution.


//initial wagon object holding key/value pairs to indicate progress on road journey
const initialWagonState = {
    supplies: 100,
    distance: 0,
    days: 0,
    cash: 200
  };
  
  //reducer to manage state of the journey
  const gameProgress = (state=initialWagonState, action) => {
    switch (action.type) {
      case 'gather': {
        return {
          ...state, 
          supplies: state.supplies + 15,
          distance: state.distance,
          days: state.days + 1
        };
      }
  
      case 'travel': {
        return {
          ...state, 
          supplies: state.supplies - (20 * action.payload),
          distance: state.distance + (10 * action.payload),
          days: state.days + action.payload
        }; 
        // if supplies are not sufficient to travel, return current state    
        if (!state.supplies === action.payload) {
          return state
        }
      }
  
      case 'tippedWagon': {
        return {
          ...state,
          supplies: state.supplies - 30,
          days: state.days + 1
        };
      }
  
      case 'sell': {
        return {
          ...state,
          supplies: state.supplies - 20,
          cash: state.cash + 5,
        };
      }
  
      case 'buy': {
        return {
          ...state,
          supplies: state.supplies + 25,
          cash: state.cash - 15,
        };
      }
  
      case 'theft': {
        return {
          ...state,
          cash: state.cash / 2,
        };
      }
  
      default: {
        return state
      }
    }
  }
  
  //initialization
  let wagon = gameProgress(undefined, {});
  
  //day 1 actions
  wagon = gameProgress(wagon, {type: 'travel', payload: 1});
  console.log(wagon)
  
  //day 2 actions
  wagon = gameProgress(wagon, {type: 'gather', payload: 0});
  console.log(wagon)
  
  //river accident actions
  wagon = gameProgress(wagon, {type: 'tippedWagon', payload: 0});
  console.log(wagon)
  
  //buy supplies to replace lost from wagon accident
  wagon = gameProgress(wagon, {type: 'buy', payload: 0});
  console.log(wagon)
  
  //day 3 actions
  wagon = gameProgress(wagon, {type: 'travel', payload: 3});
  console.log(wagon)
  
  //theft
  wagon = gameProgress(wagon, {type: 'theft'});
  console.log(wagon)

1 Like