Example #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);
        }
Example #2
0
        private static double NNCountingBasicStrategy(Net.Net playingNet, 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

            //input, playersVal (17), dealersVal(10), playerHasAce(1)
            var playingPolicy = new NNBasicStrategy(playingNet, 1.0);//fixed policy for playing
            var bettingPolicy = new NNBettingStrategy(bettingNet, eps);

            //do each hand
            for (totalHandsPlayed = 0; totalHandsPlayed < numOfHands; totalHandsPlayed++)
            {
                //figure out bet
                var bet         = 1.0f;
                var actionTaken = bettingPolicy.chooseBet(deck);

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

                if (deck.isDeckFinished())
                {
                    deck = new Deck(6);
                    deck.shuffleCards();
                }
                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 reward = playHand(ref deck, playerHand, ref dealerHand, ref playingPolicy, ref winLoss, isTraining);
                reward = reward * bet;
                bettingPolicy.runBackwards(reward, actionTaken);
            }

            var x = winLoss / (1.0 * numOfHands);

            return(x);
        }
Example #3
0
        private static double NNCountingBasicStrategy(Net.Net playingNet, 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

            //input, playersVal (17), dealersVal(10), playerHasAce(1)
            var playingPolicy = new NNBasicStrategy(playingNet, 1.0);//fixed policy for playing
            var bettingPolicy = new NNBettingStrategy(bettingNet, eps);
            //do each hand
            for (totalHandsPlayed = 0; totalHandsPlayed < numOfHands; totalHandsPlayed++)
            {
                //figure out bet
                var bet = 1.0f;
                var actionTaken = bettingPolicy.chooseBet(deck);

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

                if (deck.isDeckFinished())
                {
                    deck = new Deck(6);
                    deck.shuffleCards();
                }
                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 reward = playHand(ref deck, playerHand, ref dealerHand, ref playingPolicy, ref winLoss, isTraining);
                reward = reward * bet;
                bettingPolicy.runBackwards(reward, actionTaken);
            }

            var x = winLoss / (1.0 * numOfHands);
            return x;
        }
Example #4
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;
        }