program 3/blackjack.js
/**
  * Simluates the calculation of the probability of taking action on each turn of a blackjack-like simulation
  * @author Jason Seminara, N12517906
  * @since 2015-04-14
  * 

  *
  * @INSTRUCTIONS
  * This is a node.js executable. 
  * Execute by passing the three required args in the command line nCards, lTarget, uTarget
  * e.g.
  * node blackjack.js 10 21 23
  *
  
  *
  * @returns 3 matrices: 
  * (1) the optimal move for player X in the case where player Y did not pass on the previous move, where XT and YT are the current totals for X and Y: Play[XT,YT]=1 if X should draw, 0 if he should pass
  * (2) the probability of winning if X passes in state XT,YT;
  * (3) the probability of winning if X draws in state XT,YT;
  *

*/
Simluates the calculation of the probability of taking action on each turn of a blackjack-like simulation
@author Jason Seminara, N12517906
@since 2015-04-14



@INSTRUCTIONS
This is a node.js executable.
Execute by passing the three required args in the command line nCards, lTarget, uTarget
e.g.
node blackjack.js 10 21 23



@returns 3 matrices:
(1) the optimal move for player X in the case where player Y did not pass on the previous move, where XT and YT are the current totals for X and Y: Play[XT,YT]=1 if X should draw, 0 if he should pass
(2) the probability of winning if X passes in state XT,YT;
(3) the probability of winning if X draws in state XT,YT.
Simluates the calculation of the probability of taking action on each turn of a blackjack - like simulation @author Jason Seminara N12517906 @since 2015 - 04 - 14 @INSTRUCTIONS This is a node . js executable . Execute by passing the three required args in the command line nCards lTarget uTarget e . g . node blackjack . js 10 21 23 @returns 3 matrices : ( 1 ) the optimal move for player X in the case where player Y did not pass on the previous move where XT and YT are the current totals for X and Y : Play [ XT YT ] = 1 if X should draw 0 if he should pass ( 2 ) the probability of winning if X passes in state XT YT ; ( 3 ) the probability of winning if X draws in state XT YT .
function Blackjack(nCards, lTarget, uTarget){
  var xT= 0; 
  var yT= 0; 
  var play = new Array(lTarget);
  var prob = new Array(lTarget);
  for(var i=0; i<lTarget; i++){
    play[i] = [0];
    prob[i] = [0];
    for(var j=0; j<lTarget; j++){
      prob[i][j] = [0,0];
    }
  }
  function computeProbOfDraw(xT,yT){
    var probWinning = 0;
    var probYwins   = 0;
    for(var card = 1; card<=nCards; card++){
      if(xT+card > uTarget){
        probYwins=1;
      }
      else if(xT+card >= lTarget){
        probYwins=0;
      }else{
        probYwins = prob[yT][xT+card][play[yT][xT+card]]
      }
      probWinning = probWinning+(1-probYwins)/nCards;
    }
    return probWinning;
  }
  for(var sum=2*lTarget-2; sum>=0; sum--){
    for(xT = sum+1-lTarget; xT<lTarget; xT++){
      yT = sum-xT;
      if( yT<0 || xT<0 ) continue;
      prob[xT][yT][0] = (xT<=yT) ? 0 : 1-prob[yT][xT][1];
      prob[xT][yT][1] = computeProbOfDraw(xT,yT);
      play[xT][yT] = (prob[xT][yT][1] > prob[xT][yT][0])?1:0;
    }
  }
  return {play:play,prob:prob};
}
function Blackjack ( nCards , lTarget , uTarget ) { var xT = 0 ; var yT = 0 ; var play = new Array ( lTarget ) ; var prob = new Array ( lTarget ) ; for ( var i = 0 ; i < lTarget ; i ++ ) { play [ i ] = [ 0 ] ; prob [ i ] = [ 0 ] ; for ( var j = 0 ; j < lTarget ; j ++ ) { prob [ i ] [ j ] = [ 0 , 0 ] ; } } function computeProbOfDraw ( xT , yT ) { var probWinning = 0 ; var probYwins = 0 ; for ( var card = 1 ; card <= nCards ; card ++ ) { if ( xT + card > uTarget ) { probYwins = 1 ; } else if ( xT + card >= lTarget ) { probYwins = 0 ; } else { probYwins = prob [ yT ] [ xT + card ] [ play [ yT ] [ xT + card ] ] } probWinning = probWinning + ( 1 - probYwins ) / nCards ; } return probWinning ; } for ( var sum = 2 * lTarget - 2 ; sum >= 0 ; sum -- ) { for ( xT = sum + 1 - lTarget ; xT < lTarget ; xT ++ ) { yT = sum - xT ; if ( yT < 0 || xT < 0 ) continue ; prob [ xT ] [ yT ] [ 0 ] = ( xT <= yT ) ? 0 : 1 - prob [ yT ] [ xT ] [ 1 ] ; prob [ xT ] [ yT ] [ 1 ] = computeProbOfDraw ( xT , yT ) ; play [ xT ] [ yT ] = ( prob [ xT ] [ yT ] [ 1 ] > prob [ xT ] [ yT ] [ 0 ] ) ? 1 : 0 ; } } return { play : play , prob : prob } ; }
Simluates the calculation of the probability of taking action on each turn of a blackjack-like simulation
 @author Jason Seminara, N12517906
 @since 2015-04-14
Simluates the calculation of the probability of taking action on each turn of a blackjack - like simulation @author Jason Seminara N12517906 @since 2015 - 04 - 14
// Current Totals for X. // Current Totals for Y. // Play[xT][yT]: Boolean[lTarget][lTarget] // Optimal move for X when Y did not pass on previous move. // 1 if X should draw, 0 if pass.   // prob[xT][yT][2]: Boolean[lTarget][lTarget][2] // probability that X will win from state xT,yT if he makes move P // init all arrays with 0 //console.log("probWinning",probWinning); // walk over the arrays, fill out the 2d grid from the [lTarget,lTarget] to [0,0] // we want to fill up the upper-left diagonal of the table,  // but the math used to do so creates lots of negatives.  // skip over them to grab the valid positive ones. //console.log(xT,yT); // the prob of winning if X passes in state xT,yT // the prob of winning if X draws in state xT,yT // we have to cast to integer here (not Boolean) b/c we need to use this value as a key on prob[x][y][bool]   