Street Dice Pt 6

Today our goal is to update the function for the final value in our game debug menu which is resolveBet(). Last video we ran into an issue where we tried to nest a question in our debug interface. So what happened was when we selected to set the bet, we needed to then input a bet value to use to set the bet. When we added our question, it would take our bet but wouldn’t loop so we would just hang on the input stream. We needed to add a call to our recursive menu function after taking that value.

With the bet set. Today’s task will be to resolve the bet. We need to know the current player, the game result, and the bet amount. The other challenge was to get the opposite value of the current player. so if it was 0 then we needed 1 for the other player because we needed to add or subtract from the other player’s bank depending on the result.

We found a neat solution on stack overflow that operates like a button being on or off and gives the desired result.

v = +!v;

The final code for our resolveBet() function was:

function resolveBet(game) {
  console.log("Did the player win? ", (game.currentGameResult=='win'));
  //currentShooter, currentGameResult, bet
  if (game.currentGameResult == 'win') {
    // Define loser as opposite of winner if winner = currentShooter
    let loser = +!game.currentShooter;
    console.log("Loser: ", loser);
    // subtract money from loser bank v = +!v;
    console.log(game.setPlayerBank(loser, -game.bet)); 
    // add money to currentShooter bank
    console.log(game.setPlayerBank(game.currentShooter, game.bet));
  } else {
    // subtract money from currentShooter bank
    game.setPlayerBank(game.currentShooter, -game.bet);
    // add money to winner bank
    let winner = +!game.currentShooter;
    game.setPlayerBank(winner, game.bet); 
  }
  return console.log(game.playersBanks);
}

Please watch the coding session on youTube to follow along.