missionaries_game/js/sketch.js
// Generate new states from parent state.
Generate new states from parent state.
Generate new states from parent state .
function applyOperation(tempState) {
    if(tempState.visited === true) {
      killedState.push(state[state.length - 1]);
      state.splice(state.length - 1, 1);
    }else {
    tempState.visited = true;
    boatPosition = tempState.value[2];
    if(boatPosition === 1) {
      if(tempState.value[0] >= 2) {
        addState(tempState, [tempState.value[0] - 2, tempState.value[1] - 0, 0]);
      }
      if(tempState.value[0] >= 1) {
        addState(tempState, [tempState.value[0] - 1, tempState.value[1] - 0, 0]);
      }
      if(tempState.value[1] >= 2) {
        addState(tempState, [tempState.value[0] - 0, tempState.value[1] - 2, 0]);
      }
       and 1 Cannibal
      if(tempState.value[0] >= 1 && tempState.value[1] >= 1) {
        addState(tempState, [tempState.value[0] - 1, tempState.value[1] - 1, 0]);
      }
      if(tempState.value[1] >= 1) {
        addState(tempState, [tempState.value[0] - 0, tempState.value[1] - 1, 0]);
      }
    } else if(boatPosition === 0) {
       and 1 Cannibal
      if(initialState[0] - tempState.value[0] > 0) {
        addState(tempState, [tempState.value[0] + 1, tempState.value[1] + 0, 1]);
      }
      if(initialState[1] - tempState.value[1] > 0) {
        addState(tempState, [tempState.value[0] + 0, tempState.value[1] + 1, 1]);
      }
      if(initialState[0] - tempState.value[0] > 2) {
        addState(tempState, [tempState.value[0] + 2, tempState.value[1] + 0, 1]);
      }
      if(initialState[1] - tempState.value[1] > 2) {
        addState(tempState, [tempState.value[0] + 0, tempState.value[1] + 2, 1]);
      }
       and 1 Cannibal
      if((initialState[0] - tempState.value[0] > 0) && (initialState[1] - tempState.value[1] > 0)) {
        addState(tempState, [tempState.value[0] + 1, tempState.value[1] + 1, 1]);
      }
    }
  }
}
function applyOperation ( tempState ) { if ( tempState . visited === true ) { killedState . push ( state [ state . length - 1 ] ) ; state . splice ( state . length - 1 , 1 ) ; } else { tempState . visited = true ; boatPosition = tempState . value [ 2 ] ; if ( boatPosition === 1 ) { if ( tempState . value [ 0 ] >= 2 ) { addState ( tempState , [ tempState . value [ 0 ] - 2 , tempState . value [ 1 ] - 0 , 0 ] ) ; } if ( tempState . value [ 0 ] >= 1 ) { addState ( tempState , [ tempState . value [ 0 ] - 1 , tempState . value [ 1 ] - 0 , 0 ] ) ; } if ( tempState . value [ 1 ] >= 2 ) { addState ( tempState , [ tempState . value [ 0 ] - 0 , tempState . value [ 1 ] - 2 , 0 ] ) ; } if ( tempState . value [ 0 ] >= 1 && tempState . value [ 1 ] >= 1 ) { addState ( tempState , [ tempState . value [ 0 ] - 1 , tempState . value [ 1 ] - 1 , 0 ] ) ; } if ( tempState . value [ 1 ] >= 1 ) { addState ( tempState , [ tempState . value [ 0 ] - 0 , tempState . value [ 1 ] - 1 , 0 ] ) ; } } else if ( boatPosition === 0 ) { if ( initialState [ 0 ] - tempState . value [ 0 ] > 0 ) { addState ( tempState , [ tempState . value [ 0 ] + 1 , tempState . value [ 1 ] + 0 , 1 ] ) ; } if ( initialState [ 1 ] - tempState . value [ 1 ] > 0 ) { addState ( tempState , [ tempState . value [ 0 ] + 0 , tempState . value [ 1 ] + 1 , 1 ] ) ; } if ( initialState [ 0 ] - tempState . value [ 0 ] > 2 ) { addState ( tempState , [ tempState . value [ 0 ] + 2 , tempState . value [ 1 ] + 0 , 1 ] ) ; } if ( initialState [ 1 ] - tempState . value [ 1 ] > 2 ) { addState ( tempState , [ tempState . value [ 0 ] + 0 , tempState . value [ 1 ] + 2 , 1 ] ) ; } if ( ( initialState [ 0 ] - tempState . value [ 0 ] > 0 ) && ( initialState [ 1 ] - tempState . value [ 1 ] > 0 ) ) { addState ( tempState , [ tempState . value [ 0 ] + 1 , tempState . value [ 1 ] + 1 , 1 ] ) ; } } } }
Generate new states from parent state.
Generate new states from parent state .
// If Boat is at the left bank // console.log("boat is going from Left to Right"); // 2 Missionaries // 1 Missionary // 2 Cannibals // 1 Missionary and 1 Cannibal // 1 Cannibal // If Boat is at the right bank. // 1 Missionary and 1 Cannibal // 1 Cannibal // 2 Missionary // 2 Cannibals // 1 Missionary and 1 Cannibal