Exemplo n.º 1
0
        /*
         * static private void showPolicy(Net.Net n)
         * {
         *  Console.Write("    ");
         *  var pol = new NNBasicStrategy(n, 1.0);
         *  for (int d = 2; d <= 11; d++)
         *  {
         *      Console.Write(d + " ");
         *  }
         *  Console.WriteLine();
         *  //Try each possible input and get the output.
         *  for (int p = 21; p >= 4; p--)
         *  {
         *      if(p >= 10)
         *          Console.Write(p + "  ");
         *      else
         *          Console.Write(p + "   ");
         *      for (int d = 2; d <= 11; d++)
         *      {
         *          Hand pH = new Hand();
         *          Hand dH = new Hand();
         *          pH.addCards(p);
         *          dH.addCards(d);
         *          //var a = pol.choosePlayerAction(pH, dH, deck);
         *          Console.Write(a + " ");
         *      }
         *      Console.WriteLine();
         *  }
         *
         *  //do for soft totals
         *  Console.WriteLine();
         *  Console.WriteLine("------ SOFTS ------");
         *
         *  for (int p = 2; p <= 9; p++)
         *  {
         *      Console.Write(p + "   ");
         *      for (int d = 2; d <= 11; d++)
         *      {
         *          Hand pH = new Hand();
         *          Hand dH = new Hand();
         *          pH.addCards(11);
         *          pH.addCards(p);
         *          dH.addCards(d);
         *         // var a = pol.choosePlayerAction(pH, dH);
         *          Console.Write(a + " ");
         *      }
         *      Console.WriteLine();
         *  }
         *
         * }
         */

        /*
         * private static double simpleBasicStrategy(int numOfHands)
         * {
         * var ret = new List<double>();
         * int totalHandsPlayed;
         * double winLoss = 0.0; //-1 for loss, +1 for win. +1.5 for blackjack. 0 for draw
         * var policy = new BasicStrategy();
         * //do each hand
         * Deck deck = new Deck(6);
         * deck.shuffleCards();
         * for (totalHandsPlayed = 0; totalHandsPlayed < numOfHands; totalHandsPlayed++)
         * {
         *  if (deck.isDeckFinished())
         *  {
         *      deck = new Deck(6);
         *      deck.shuffleCards();
         *  }
         *
         *  //decide bet
         *  var trueCount = deck.getTrueCount();
         *  var bet = 1.0;
         *  if (trueCount > 2)
         *      bet = 10.0;
         *
         *  Hand playerHand = new Hand();
         *  Hand dealerHand = new Hand();
         *  //deal initial cards
         *  playerHand.addCards(deck.getCard());
         *  dealerHand.addCards(deck.getCard());
         *  playerHand.addCards(deck.getCard());
         *  dealerHand.addCards(deck.getCard());
         *  playHandBasic(ref deck, playerHand, ref dealerHand, ref policy, ref winLoss, bet);
         * }
         * var x = winLoss / (numOfHands);
         * return x;
         * }
         */
        private static double ccBasicStrategy(Net.Net bettingNet, int numOfHands, double eps, ref Deck deck, bool isTraining = false)
        {
            int    totalHandsPlayed;
            double winLoss       = 0.0; //-1 for loss, +1 for win. +1.5 for blackjack. 0 for draw
            var    policy        = new BasicStrategy();
            var    bettingPolicy = new NNBettingStrategy(bettingNet, eps);

            //do each hand
            for (totalHandsPlayed = 0; totalHandsPlayed < numOfHands; totalHandsPlayed++)
            {
                if (deck.isDeckFinished())
                {
                    deck = new Deck(6);
                    deck.shuffleCards();
                }

                //figure out bet
                var bet         = 1.0f;
                var actionTaken = bettingPolicy.chooseBet(deck);

                if (actionTaken == 1)
                {
                    bet = 5.0f;
                }
                if (actionTaken == 2)
                {
                    bet = 10.0f;
                }

                Hand playerHand = new Hand();
                Hand dealerHand = new Hand();
                //deal initial cards
                playerHand.addCards(deck.getCard());
                dealerHand.addCards(deck.getCard());
                playerHand.addCards(deck.getCard());
                dealerHand.addCards(deck.getCard());

                var winBefore = winLoss;
                var winAfter  = winLoss;
                var diff      = winAfter - winBefore;

                var reward = playHandBasic(ref deck, playerHand, ref dealerHand, ref policy, ref winLoss);
                reward = reward * bet;
                if (isTraining)
                {
                    bettingPolicy.runBackwards(reward, actionTaken);
                }

                diff     = (bet * diff) - diff;
                winLoss += diff;
            }
            var x = winLoss / (numOfHands);

            return(x);
        }
Exemplo n.º 2
0
        private static float playHandBasic(ref Deck deck, Hand playerHand, ref Hand dealerHand, ref BasicStrategy policy, ref double winLoss)
        {
            var reward = 0.0f;
            var mult   = 1.0f;

            //check for blackjack.
            if (playerHand.getValue() == 21 && dealerHand.getValue() != 21)
            {
                winLoss += 1.5;
                return(1.5f);
            }
            else if (dealerHand.getValue() == 21) //dealer got blackjack
            {
                winLoss -= 1.0;
                return(-1.0f);
            }
            else
            {
                //player decisions
                var actionTaken = policy.choosePlayerAction(playerHand, dealerHand);

                if (actionTaken == 1)//hit
                {
                    //player decisions
                    var action = policy.choosePlayerAction(playerHand, dealerHand);
                    while (action == 1)
                    {
                        playerHand.addCards(deck.getCard());
                        action = policy.choosePlayerAction(playerHand, dealerHand);
                    }
                    //see if we busted
                    if (playerHand.getValue() > 21)
                    {
                        winLoss -= 1.0;
                    }
                    else
                    {
                        actionTaken = 0;
                    }
                }
                else if (actionTaken == 2) //double
                {
                    playerHand.addCards(deck.getCard());
                    if (playerHand.getValue() > 21)
                    {
                        winLoss -= 2.0;
                        reward   = -1.0f;
                        mult     = 2.0f;
                    }
                    else
                    {
                        mult        = 2.0f;
                        actionTaken = 0;
                    }
                }
                else if (actionTaken == 3) //split
                {
                    Hand pH1 = new Hand();
                    Hand pH2 = new Hand();

                    var val = playerHand.getValue() / 2;
                    //split card and get an extra.
                    pH1.addCards(val);
                    pH2.addCards(val);
                    pH1.addCards(deck.getCard());
                    pH2.addCards(deck.getCard());

                    //win loss for the hands
                    reward  = playHandBasic(ref deck, pH1, ref dealerHand, ref policy, ref winLoss);
                    reward += playHandBasic(ref deck, pH2, ref dealerHand, ref policy, ref winLoss);

                    winLoss += reward;
                }
                if (actionTaken == 0) //stand
                {
                    //play dealer
                    var dealerVal = playDealer(ref deck, ref dealerHand);
                    if (dealerVal > 21) //dealer busts
                    {
                        winLoss += 1.0 * mult;
                        reward   = 1.0f * mult;
                    }
                    else if (dealerVal < playerHand.getValue()) //we beat dealer
                    {
                        winLoss += 1.0f * mult;
                        reward   = 1.0f * mult;
                    }
                    else if (dealerVal == playerHand.getValue()) //draw
                    {
                        reward = 0.0f;
                    }
                    else //we lost to dealer
                    {
                        winLoss -= 1.0 * mult;
                        reward   = -1.0f * mult;
                    }
                }
            }

            return(reward);
        }