Esempio n. 1
0
        private static double NNsimpleBasicStrategy(Net.Net net, 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 policy = new NNBasicStrategy(net, eps);

            //do each hand
            for (totalHandsPlayed = 0; totalHandsPlayed < numOfHands; totalHandsPlayed++)
            {
                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());

                playHand(ref deck, playerHand, ref dealerHand, ref policy, ref winLoss, isTraining);
            }

            var x = winLoss / (1.0 * numOfHands);

            return(x);
        }
Esempio n. 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);
        }
Esempio n. 3
0
        private static void playHit(ref Deck deck, ref Hand playerHand, Hand dealerHand, ref NNBasicStrategy policy, bool isTraining)
        {
            var actionTaken = 1;
            while (actionTaken == 1) // hit
            {
                playerHand.addCards(deck.getCard());

                if (playerHand.getValue() > 21)
                {
                    break;
                }
                else
                {
                    if (isTraining)
                    {
                        policy.runBackwardsHit(playerHand, dealerHand, deck);
                    }
                }

                //need to do delayed reward.
                actionTaken = policy.choosePlayerAction(playerHand, dealerHand, deck);
            }
        }
Esempio n. 4
0
        private static float playHand(ref Deck deck, Hand playerHand, ref Hand dealerHand, ref NNBasicStrategy policy, ref double winLoss, bool isTraining)
        {
            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, deck);

                if (actionTaken == 1)//hit
                {
                    playHit(ref deck, ref playerHand, dealerHand, ref policy, isTraining);

                    if (playerHand.getValue() > 21)
                    {
                        winLoss -= 1.0;
                        reward = -1.0f;
                    }
                    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 = playHand(ref deck, pH1, ref dealerHand, ref policy, ref winLoss, isTraining);
                    reward += playHand(ref deck, pH2, ref dealerHand, ref policy, ref winLoss, isTraining);

                    winLoss += reward;
                    //policy.runBackwards(reward, actionTaken);
                }
                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
                    {
                        reward = -1.0f * mult;
                        winLoss -= 1.0 * mult;
                    }
                }

                if (isTraining)
                {
                    if (mult == 2.0f)
                        actionTaken = 2;
                    if (actionTaken != 3)
                        policy.runBackwards(reward, actionTaken);
                }

            }

            return reward;
        }
Esempio n. 5
0
        private static double NNsimpleBasicStrategy(Net.Net net, 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 policy = new NNBasicStrategy(net, eps);

            //do each hand
            for (totalHandsPlayed = 0; totalHandsPlayed < numOfHands; totalHandsPlayed++)
            {
                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());

                playHand(ref deck, playerHand, ref dealerHand, ref policy, ref winLoss, isTraining);
            }

            var x = winLoss / (1.0 * numOfHands);
            return x;
        }
Esempio n. 6
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;
        }
Esempio n. 7
0
        private static void commandLineBJ()
        {
            Stream stream = File.Open("playingNet.xml", FileMode.Open);
            BinaryFormatter formatter = new BinaryFormatter();
            Net.Net playingNet = null;
            playingNet = (Net.Net)formatter.Deserialize(stream);
            stream.Close();
            /*
            Stream stream1 = File.Open("bettingNet.xml", FileMode.Open);
            BinaryFormatter formatter1 = new BinaryFormatter();
            Net.Net bettingNet = null;
            bettingNet = (Net.Net)formatter1.Deserialize(stream1);
            stream1.Close();
            */
            Random r = new Random();

            var playingPolicy = new NNBasicStrategy(playingNet, 1.0);//fixed policy for playing
            //var bettingPolicy = new NNBettingStrategy(bettingNet, eps);
            Deck deck = new Deck(6);
            deck.shuffleCards();
            bool exit = false;
            while(!exit)
            {
                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());
                bool handFinished = false;
                while (!handFinished)
                {
                    Console.WriteLine("Enter Q to quit");
                    Console.WriteLine();
                    Console.WriteLine();
                    Console.WriteLine("PLAYER HAND:    " + playerHand.handToString());
                    Console.WriteLine("DEALER SHOWING: " + dealerHand.getDealerShowing());
                    Console.WriteLine("Q Values: ");
                    var qVals = playingPolicy.getQScores(playerHand, dealerHand, deck);
                    var action = playingPolicy.choosePlayerAction(playerHand, dealerHand, deck);
                    foreach (var q in qVals)
                    {
                        Console.Write("  " + q);
                    }
                    if (action != 3)
                    {
                        var x = (float)(-1 * r.NextDouble());
                        Console.Write("  " + x);
                    }
                    else
                    {
                        var x = (float)(r.NextDouble());
                        while (x < .5f)
                            x = (float)(r.NextDouble());
                        Console.Write("  " + x);
                    }
                    Console.WriteLine();
                    Console.WriteLine("Choose action (0 = stay, 1 = hit, 2 = double, 3 = split)");
                    var line = Console.ReadLine();
                    if (line.Equals("Q") || line.Equals("q"))
                    {
                        exit = true;
                        continue;
                    }

                    var act = Int32.Parse(line);
                    if (act == 0) //stand
                        handFinished = true;
                    else if(act == 1) //hit
                    {
                        playerHand.addCards(deck.getCard());
                        if (playerHand.getValue() > 21)
                            handFinished = true;
                    }
                    else if(act == 2) //double
                    {
                        playerHand.addCards(deck.getCard());
                        handFinished = true;
                    }
                    else
                    {
                        Console.WriteLine("Split doesnt work with the command line interface right now");
                    }
                }

                if (playerHand.getValue() > 21)
                {
                    Console.WriteLine("LOST -- WE BUSTED");
                    continue;
                }

                //play dealer
                var dealerVal = playDealer(ref deck, ref dealerHand);

                Console.WriteLine("PLAYER HAND: " + playerHand.handToString());
                Console.WriteLine("DEALER HAND: " + dealerHand.handToString());

                if (dealerVal > 21) //dealer busts
                {
                    Console.WriteLine("WINNER -- DEALER BUSTED");
                }
                else if (dealerVal < playerHand.getValue()) //we beat dealer
                {
                    Console.WriteLine("WINNER");
                }
                else if (dealerVal == playerHand.getValue()) //draw
                {
                    Console.WriteLine("DRAW");
                }
                else //we lost to dealer
                {
                    Console.WriteLine("YOU LOST");
                }
                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine("ENTER TO CONTINUE");
                Console.ReadLine();
            }
        }
Esempio n. 8
0
        private static void playHit(ref Deck deck, ref Hand playerHand, Hand dealerHand, ref NNBasicStrategy policy, bool isTraining)
        {
            var actionTaken = 1;

            while (actionTaken == 1) // hit
            {
                playerHand.addCards(deck.getCard());

                if (playerHand.getValue() > 21)
                {
                    break;
                }
                else
                {
                    if (isTraining)
                    {
                        policy.runBackwardsHit(playerHand, dealerHand, deck);
                    }
                }

                //need to do delayed reward.
                actionTaken = policy.choosePlayerAction(playerHand, dealerHand, deck);
            }
        }
Esempio n. 9
0
        private static float playHand(ref Deck deck, Hand playerHand, ref Hand dealerHand, ref NNBasicStrategy policy, ref double winLoss, bool isTraining)
        {
            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, deck);

                if (actionTaken == 1)//hit
                {
                    playHit(ref deck, ref playerHand, dealerHand, ref policy, isTraining);

                    if (playerHand.getValue() > 21)
                    {
                        winLoss -= 1.0;
                        reward   = -1.0f;
                    }
                    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  = playHand(ref deck, pH1, ref dealerHand, ref policy, ref winLoss, isTraining);
                    reward += playHand(ref deck, pH2, ref dealerHand, ref policy, ref winLoss, isTraining);

                    winLoss += reward;
                    //policy.runBackwards(reward, actionTaken);
                }
                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
                    {
                        reward   = -1.0f * mult;
                        winLoss -= 1.0 * mult;
                    }
                }

                if (isTraining)
                {
                    if (mult == 2.0f)
                    {
                        actionTaken = 2;
                    }
                    if (actionTaken != 3)
                    {
                        policy.runBackwards(reward, actionTaken);
                    }
                }
            }

            return(reward);
        }
Esempio n. 10
0
        private static void commandLineBJ()
        {
            Stream          stream    = File.Open("playingNet.xml", FileMode.Open);
            BinaryFormatter formatter = new BinaryFormatter();

            Net.Net playingNet = null;
            playingNet = (Net.Net)formatter.Deserialize(stream);
            stream.Close();

            /*
             * Stream stream1 = File.Open("bettingNet.xml", FileMode.Open);
             * BinaryFormatter formatter1 = new BinaryFormatter();
             * Net.Net bettingNet = null;
             * bettingNet = (Net.Net)formatter1.Deserialize(stream1);
             * stream1.Close();
             */
            Random r = new Random();

            var playingPolicy = new NNBasicStrategy(playingNet, 1.0);//fixed policy for playing
            //var bettingPolicy = new NNBettingStrategy(bettingNet, eps);
            Deck deck = new Deck(6);

            deck.shuffleCards();
            bool exit = false;

            while (!exit)
            {
                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());
                bool handFinished = false;
                while (!handFinished)
                {
                    Console.WriteLine("Enter Q to quit");
                    Console.WriteLine();
                    Console.WriteLine();
                    Console.WriteLine("PLAYER HAND:    " + playerHand.handToString());
                    Console.WriteLine("DEALER SHOWING: " + dealerHand.getDealerShowing());
                    Console.WriteLine("Q Values: ");
                    var qVals  = playingPolicy.getQScores(playerHand, dealerHand, deck);
                    var action = playingPolicy.choosePlayerAction(playerHand, dealerHand, deck);
                    foreach (var q in qVals)
                    {
                        Console.Write("  " + q);
                    }
                    if (action != 3)
                    {
                        var x = (float)(-1 * r.NextDouble());
                        Console.Write("  " + x);
                    }
                    else
                    {
                        var x = (float)(r.NextDouble());
                        while (x < .5f)
                        {
                            x = (float)(r.NextDouble());
                        }
                        Console.Write("  " + x);
                    }
                    Console.WriteLine();
                    Console.WriteLine("Choose action (0 = stay, 1 = hit, 2 = double, 3 = split)");
                    var line = Console.ReadLine();
                    if (line.Equals("Q") || line.Equals("q"))
                    {
                        exit = true;
                        continue;
                    }

                    var act = Int32.Parse(line);
                    if (act == 0) //stand
                    {
                        handFinished = true;
                    }
                    else if (act == 1) //hit
                    {
                        playerHand.addCards(deck.getCard());
                        if (playerHand.getValue() > 21)
                        {
                            handFinished = true;
                        }
                    }
                    else if (act == 2) //double
                    {
                        playerHand.addCards(deck.getCard());
                        handFinished = true;
                    }
                    else
                    {
                        Console.WriteLine("Split doesnt work with the command line interface right now");
                    }
                }

                if (playerHand.getValue() > 21)
                {
                    Console.WriteLine("LOST -- WE BUSTED");
                    continue;
                }

                //play dealer
                var dealerVal = playDealer(ref deck, ref dealerHand);

                Console.WriteLine("PLAYER HAND: " + playerHand.handToString());
                Console.WriteLine("DEALER HAND: " + dealerHand.handToString());

                if (dealerVal > 21) //dealer busts
                {
                    Console.WriteLine("WINNER -- DEALER BUSTED");
                }
                else if (dealerVal < playerHand.getValue()) //we beat dealer
                {
                    Console.WriteLine("WINNER");
                }
                else if (dealerVal == playerHand.getValue()) //draw
                {
                    Console.WriteLine("DRAW");
                }
                else //we lost to dealer
                {
                    Console.WriteLine("YOU LOST");
                }
                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine("ENTER TO CONTINUE");
                Console.ReadLine();
            }
        }