public void GameTest()
        {
            Decks playingDecks = new Decks();

            playingDecks.FillCards();
            byte[] gameCards = new byte[416];
            gameCards = playingDecks.Cards;
            byte          bjFlag;
            int           testResult;
            LibraryLoader libraryLoader = new LibraryLoader("../../../../BotsLibrary/BotsLibrary.dll");
            Assembly      asm           = libraryLoader.LoadLibrary();
            PluginHelper  pluginHelper  = new PluginHelper(asm);

            // Dealer has blackjack and player in both hands has blackjack too
            gameCards[0] = 10; gameCards[1] = 1; gameCards[2] = 1; gameCards[3] = 10;
            gameCards[4] = 10; gameCards[5] = 1; bjFlag = 3; testResult = 32;
            SomeGameTest(pluginHelper, playingDecks, gameCards, testResult, bjFlag);

            gameCards[0] = 10; gameCards[1] = 10; gameCards[2] = 10; gameCards[3] = 1;
            bjFlag       = 0; testResult = -16; // Dealer has blackjack, player's score is 20.
            SomeGameTest(pluginHelper, playingDecks, gameCards, testResult, bjFlag);

            gameCards[0] = 10; gameCards[1] = 10; gameCards[2] = 10; gameCards[3] = 7;
            bjFlag       = 0; testResult = 32; // Dealer has 17, player's score is 20.
            SomeGameTest(pluginHelper, playingDecks, gameCards, testResult, bjFlag);

            gameCards[0] = 10; gameCards[1] = 10; gameCards[2] = 5; gameCards[3] = 7;
            bjFlag       = 0; testResult = 8; // Player surrendered
            SomeGameTest(pluginHelper, playingDecks, gameCards, testResult, bjFlag);

            gameCards[0] = 9; gameCards[1] = 8; gameCards[2] = 8; gameCards[3] = 9; gameCards[4] = 10;
            gameCards[5] = 6; gameCards[6] = 10; bjFlag = 0; testResult = 64; // Player won with both hands
            SomeGameTest(pluginHelper, playingDecks, gameCards, testResult, bjFlag);
            // Now we test two games successively
            playingDecks.FillCards();
            gameCards    = playingDecks.Cards;
            gameCards[0] = 10; gameCards[1] = 10; gameCards[2] = 5; gameCards[3] = 7;
            bjFlag       = 0; testResult = 8; // Player surrendered
            uint          initialMoney = 1600;
            BlackjackGame testGame     = new BlackjackGame(playingDecks, initialMoney);

            object[] parameters = new object[] { testGame.PlayingDealer.DealerCards[0], (uint)testGame.InitialMoney, testGame.PlayingCards, (uint)16 };
            pluginHelper.CreatePlayer(parameters, 1);
            testGame.Player = pluginHelper.players[1];
            Assert.AreEqual(testGame.Game(), testResult);
            for (int i = 0; i < 416; i++)
            {
                Assert.AreEqual(testGame.PlayingCards.Cards[i], gameCards[i]);
            }
            // Second game
            gameCards[0] = 9; gameCards[1] = 8; gameCards[2] = 8; gameCards[3] = 9; gameCards[4] = 10;
            gameCards[5] = 6; gameCards[6] = 10; bjFlag = 0; testResult = 64; // Player won with both hands
            Assert.AreEqual(testGame.Game(), testResult);
            for (int i = 0; i < 416; i++)
            {
                Assert.AreEqual(testGame.PlayingCards.Cards[i], gameCards[i]);
            }
        }
        static void Main(string[] args)
        {
            Console.Write("This programm represents the blackjack game with opportunity to split");
            Console.Write(" cards, double wagers and surrender.\n");
            Console.WriteLine("You can choose one of 3 strategies to play:");
            Console.WriteLine("0 - basic strategy");
            Console.WriteLine("1 - cards counting strategy, based on basic strategy and cards count");
            Console.WriteLine("2 - simple strategy, based on basic strategy and players score at the moment");
            Console.WriteLine("The programm will print the approximate sum of your wagers when the game ends");
            Console.WriteLine("Please enter the correct number of strategy you want to play");
            int strategy = Convert.ToInt32(Console.ReadLine()); // in ascii table "0" has 48 number

            if (strategy < 0 || strategy > 2)
            {
                Console.WriteLine("You number isn't right.");
                return;
            }
            Bots  botsStrategy = (Bots)strategy;
            Decks playingDecks = new Decks();

            playingDecks.FillCards();
            double        win  = 0;
            BlackjackGame game = new BlackjackGame(playingDecks, 1600, botsStrategy); // 1600 is initial players money

            for (int i = 0; i < 40; i++)
            {
                win += game.Game();
            }
            Console.WriteLine("Approximate sum with 40 played rounds is {0}", win / 40.0);
            Console.WriteLine("Press enter button to exit");
            Console.ReadLine();
        }
Пример #3
0
        static void Main(string[] args)
        {
            Console.Write("This programm represents the blackjack game with opportunity to split");
            Console.Write(" cards, double wagers and surrender.\n");
            Console.WriteLine("There are 3 strategies to play:");
            Console.WriteLine("Basic strategy");
            Console.WriteLine("Cards counting strategy, based on basic strategy and cards count");
            Console.WriteLine("Simple strategy, based on basic strategy and players score at the moment");
            Console.WriteLine("There are 3 bots with different strategies");
            Console.WriteLine("The programm will print the approximate sum of their wagers when the game ends");
            Decks playingDecks = new Decks();

            playingDecks.FillCards();
            double win = 0;

            try
            {
                BlackjackGame game = new BlackjackGame(playingDecks, 1600); // 1600 is initial players money
                for (int botsCount = 0; botsCount < 3; botsCount++)
                {
                    Console.WriteLine("The turn of " + (botsCount + 1).ToString() + " bot.");
                    LibraryLoader libraryLoader = new LibraryLoader("../../../../BotsLibrary/BotsLibrary.dll");
                    Assembly      asm           = libraryLoader.LoadLibrary();
                    PluginHelper  pluginHelper  = new PluginHelper(asm);
                    object[]      parameters    = new object[] { game.PlayingDealer.DealerCards[0], game.InitialMoney, game.PlayingCards, (uint)16 };
                    pluginHelper.CreatePlayer(parameters, botsCount);
                    game.Player = pluginHelper.players[botsCount];
                    for (int i = 0; i < 40; i++)
                    {
                        win += game.Game();
                    }
                    Console.WriteLine("Approximate sum with 40 played rounds is {0}", win / 40.0);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                Console.WriteLine("Press enter button to exit");
                Console.ReadLine();
            }
        }
Пример #4
0
        public void FillCardsTest()
        {
            uint[] cardsArr     = new uint[10];
            byte[] playingCards = new byte[416];
            Decks  playingDecks = new Decks();

            playingDecks.FillCards();
            playingCards = playingDecks.Cards;
            for (int i = 0; i < 416; i++)
            {
                cardsArr[playingCards[i] - 1] += 1;
            }
            for (int i = 0; i < 9; i++)
            {
                Assert.AreEqual(cardsArr[i], 32); // number of cards whose score is 2...9 and number of aces
            }
            Assert.AreEqual(cardsArr[9], 128);    // number of cards whose score is 10
            Assert.Pass();
        }
        public void DealersTurnTest()
        {
            Decks playingDecks = new Decks();

            playingDecks.FillCards();
            byte[] gameCards   = new byte[416];
            byte[] dealerCards = new byte[21];
            byte   testResult;
            uint   playerScore;

            for (int i = 0; i < 21; i++)
            {
                dealerCards[i] = 0;
            }
            gameCards      = playingDecks.Cards;
            gameCards[0]   = 5; gameCards[1] = 6; gameCards[2] = 7; playerScore = 10;
            dealerCards[0] = 5; dealerCards[1] = 6; dealerCards[2] = 7; testResult = 1;
            SomeDealersTurnTest(playingDecks, playerScore, dealerCards, testResult);

            playerScore = 18; testResult = 2; gameCards[0] = 5; gameCards[1] = 6; gameCards[2] = 7;
            SomeDealersTurnTest(playingDecks, playerScore, dealerCards, testResult);

            playerScore = 19; testResult = 0; gameCards[0] = 5; gameCards[1] = 6; gameCards[2] = 7;
            SomeDealersTurnTest(playingDecks, playerScore, dealerCards, testResult);

            gameCards[0]   = 10; gameCards[1] = 1; playerScore = 21;
            dealerCards[0] = 10; dealerCards[1] = 1; dealerCards[2] = 0; testResult = 2;
            SomeDealersTurnTest(playingDecks, playerScore, dealerCards, testResult, 1);

            gameCards[0]   = 5; gameCards[1] = 5; gameCards[2] = 1; playerScore = 21;
            dealerCards[0] = 5; dealerCards[1] = 5; dealerCards[2] = 1; testResult = 2;
            SomeDealersTurnTest(playingDecks, playerScore, dealerCards, testResult);

            gameCards[0]   = 8; gameCards[1] = 1; gameCards[2] = 0; playerScore = 17;
            dealerCards[0] = 8; dealerCards[1] = 1; dealerCards[2] = 0; testResult = 1;
            SomeDealersTurnTest(playingDecks, playerScore, dealerCards, testResult);
        }
Пример #6
0
        public void GetCardTest()
        {
            Decks playingDecks = new Decks();

            byte[] playingCards   = new byte[416];
            byte[] cardsAfterTest = new byte[416];
            playingDecks.FillCards();
            for (int i = 0; i < 416; i++)
            {
                playingCards[i] = playingDecks.Cards[i];
            }
            byte firstCard = 0, secondCard = 0, thirdCard = 0;

            firstCard  = playingCards[0];
            secondCard = playingCards[1];
            thirdCard  = playingCards[2];
            for (int i = 0; i < 3; i++)
            {
                playingCards[i] = 0;
            }
            byte functionFirstValue  = playingDecks.GetCard();
            byte functionSecondValue = playingDecks.GetCard();
            byte functionThirdValue  = playingDecks.GetCard();

            Assert.AreEqual(firstCard, functionFirstValue);
            Assert.AreEqual(secondCard, functionSecondValue);
            Assert.AreEqual(thirdCard, functionThirdValue);
            for (int i = 0; i < 416; i++)
            {
                cardsAfterTest[i] = playingDecks.Cards[i];
            }
            for (int i = 0; i < 416; i++)
            {
                Assert.AreEqual(playingCards[i], cardsAfterTest[i]);
            }
        }
        public void GameTest()
        {
            Decks playingDecks = new Decks();

            playingDecks.FillCards();
            byte[] gameCards = new byte[416];
            gameCards = playingDecks.Cards;
            byte bjFlag;
            int  testResult;

            // Dealer has blackjack and player in both hands has blackjack too
            gameCards[0] = 10; gameCards[1] = 1; gameCards[2] = 1; gameCards[3] = 10;
            gameCards[4] = 10; gameCards[5] = 1; bjFlag = 3; testResult = 32;
            SomeGameTest(playingDecks, gameCards, testResult, bjFlag);

            gameCards[0] = 10; gameCards[1] = 10; gameCards[2] = 10; gameCards[3] = 1;
            bjFlag       = 0; testResult = -16; // Dealer has blackjack, player's score is 20.
            SomeGameTest(playingDecks, gameCards, testResult, bjFlag);

            gameCards[0] = 10; gameCards[1] = 10; gameCards[2] = 10; gameCards[3] = 7;
            bjFlag       = 0; testResult = 32; // Dealer has 17, player's score is 20.
            SomeGameTest(playingDecks, gameCards, testResult, bjFlag);

            gameCards[0] = 10; gameCards[1] = 10; gameCards[2] = 5; gameCards[3] = 7;
            bjFlag       = 0; testResult = 8; // Player surrendered
            SomeGameTest(playingDecks, gameCards, testResult, bjFlag);

            gameCards[0] = 9; gameCards[1] = 8; gameCards[2] = 8; gameCards[3] = 9; gameCards[4] = 10;
            gameCards[5] = 6; gameCards[6] = 10; bjFlag = 0; testResult = 64; // Player won with both hands
            SomeGameTest(playingDecks, gameCards, testResult, bjFlag);

            // With cards counter strategy player doubles and wins, dealer has 5, 10, 10
            playingDecks.FillCards();
            gameCards = playingDecks.Cards;
            Bots strategy = Bots.CardsCounterStrategy;

            gameCards[0] = 5; gameCards[1] = 3; gameCards[2] = 5; gameCards[3] = 10; gameCards[4] = 10;
            gameCards[5] = 10; bjFlag = 0; testResult = 64;
            SomeGameTest(playingDecks, gameCards, testResult, bjFlag, strategy);
            // But with basic strategy he just hits
            strategy     = Bots.BasicStrategy;
            gameCards[0] = 5; gameCards[1] = 3; gameCards[2] = 5; gameCards[3] = 10; gameCards[4] = 10;
            gameCards[5] = 10; bjFlag = 0; testResult = 32;
            SomeGameTest(playingDecks, gameCards, testResult, bjFlag, strategy);
            // With simple strategy player doubles and wins, dealer has 10, 6, 10
            strategy     = Bots.SimpleStrategy;
            gameCards[0] = 10; gameCards[1] = 5; gameCards[2] = 5; gameCards[3] = 10; gameCards[4] = 6;
            gameCards[5] = 10; bjFlag = 0; testResult = 64;
            SomeGameTest(playingDecks, gameCards, testResult, bjFlag, strategy);
            // But with basic strategy he just hits, dealer has 10, 6, 10
            strategy     = Bots.BasicStrategy;
            gameCards[0] = 10; gameCards[1] = 5; gameCards[2] = 5; gameCards[3] = 10; gameCards[4] = 6;
            gameCards[5] = 10; bjFlag = 0; testResult = 32;
            SomeGameTest(playingDecks, gameCards, testResult, bjFlag, strategy);
            // Now we test two games successively
            playingDecks.FillCards();
            gameCards    = playingDecks.Cards;
            gameCards[0] = 10; gameCards[1] = 10; gameCards[2] = 5; gameCards[3] = 7;
            bjFlag       = 0; testResult = 8; // Player surrendered
            uint          initialMoney = 1600;
            BlackjackGame testGame     = new BlackjackGame(playingDecks, initialMoney, strategy);

            Assert.AreEqual(testGame.Game(), testResult);
            for (int i = 0; i < 416; i++)
            {
                Assert.AreEqual(testGame.PlayingCards.Cards[i], gameCards[i]);
            }
            // Second game
            gameCards[0] = 9; gameCards[1] = 8; gameCards[2] = 8; gameCards[3] = 9; gameCards[4] = 10;
            gameCards[5] = 6; gameCards[6] = 10; bjFlag = 0; testResult = 64; // Player won with both hands
            Assert.AreEqual(testGame.Game(), testResult);
            for (int i = 0; i < 416; i++)
            {
                Assert.AreEqual(testGame.PlayingCards.Cards[i], gameCards[i]);
            }
        }
        public void PlayersTurnTest()
        {
            Decks playingDecks = new Decks();

            playingDecks.FillCards();
            byte[]      gameCards = new byte[416];
            Decisions[] playersDecisions = new Decisions[21];
            byte        bjFlag = 0, decisionsCounter, dealersCard;
            bool        surrenderFlag = false;
            uint        balance, initialMoney, firstWager, secondWager, firstSum, secondSum, initialWager;

            for (int i = 0; i < 21; i++)
            {
                playersDecisions[i] = Decisions.Nothing;
            }
            gameCards = playingDecks.Cards;
            // Player score is 20 and he don't do anything
            gameCards[0] = 10; gameCards[1] = 10; dealersCard = 5; decisionsCounter = 0; initialMoney = 60;
            balance      = 54; firstWager = 6; secondWager = 0; firstSum = 20; secondSum = 0; initialWager = 6;
            SomePlayersTurnTest(dealersCard, initialMoney, initialWager, playingDecks, decisionsCounter, firstSum, firstWager,
                                secondSum, secondWager, playersDecisions, balance);
            // Player has a blackjack
            gameCards[0] = 10; gameCards[1] = 1; firstSum = 21; bjFlag = 1;
            SomePlayersTurnTest(dealersCard, initialMoney, initialWager, playingDecks, decisionsCounter, firstSum, firstWager,
                                secondSum, secondWager, playersDecisions, balance, bjFlag);
            // Player doubles and ace has 1 score in this case
            gameCards[0] = 1; gameCards[1] = 8; gameCards[2] = 9; dealersCard = 6; firstSum = 18; firstWager = 12;
            balance      = 48; decisionsCounter = 1; playersDecisions[0] = Decisions.Doubling;
            SomePlayersTurnTest(dealersCard, initialMoney, initialWager, playingDecks, decisionsCounter, firstSum, firstWager,
                                secondSum, secondWager, playersDecisions, balance);
            // In this case ace is 11 score and player just hits
            gameCards[0] = 1; gameCards[1] = 7; gameCards[2] = 3; dealersCard = 9; firstSum = 21; firstWager = 6;
            balance      = 54; decisionsCounter = 1; playersDecisions[0] = Decisions.Hitting; bjFlag = 0;
            SomePlayersTurnTest(dealersCard, initialMoney, initialWager, playingDecks, decisionsCounter, firstSum, firstWager,
                                secondSum, secondWager, playersDecisions, balance, bjFlag);
            // In this case player first cards is 2 aces and he splits them and hits three times in first hand
            // and he has a blackjack in the second hand
            gameCards[0]        = 1; gameCards[1] = 1; gameCards[2] = 5; gameCards[3] = 6; gameCards[4] = 10; gameCards[5] = 10;
            dealersCard         = 9; playersDecisions[0] = Decisions.Splitting; playersDecisions[1] = Decisions.Hitting; // after 3rd playersDecision he plays second hand
            playersDecisions[2] = Decisions.Hitting; playersDecisions[3] = Decisions.Hitting; playersDecisions[4] = Decisions.Hitting;
            bjFlag = 2; firstSum = 0; secondSum = 21; firstWager = 6; secondWager = 6; balance = 48; decisionsCounter = 5;
            SomePlayersTurnTest(dealersCard, initialMoney, initialWager, playingDecks, decisionsCounter, firstSum, firstWager,
                                secondSum, secondWager, playersDecisions, balance, bjFlag);

            for (int i = 0; i < 5; i++)
            {
                playersDecisions[i] = Decisions.Nothing;
            }
            // Player surrenders
            gameCards[0]     = 10; gameCards[1] = 6; dealersCard = 9; playersDecisions[0] = Decisions.Surrendering;
            firstWager       = 3; balance = 57; secondWager = 0; firstSum = 16; secondSum = 0; surrenderFlag = true;
            decisionsCounter = 1; bjFlag = 0;
            SomePlayersTurnTest(dealersCard, initialMoney, initialWager, playingDecks, decisionsCounter, firstSum, firstWager,
                                secondSum, secondWager, playersDecisions, balance, bjFlag, surrenderFlag);

            // Now we test Cards Counter strategy
            // Instead of hitting we make doubling
            playingDecks.FillCards();
            gameCards = playingDecks.Cards;
            bool CardsCounterStrategyFlag = true;

            gameCards[0]        = 3; gameCards[1] = 5; gameCards[2] = 10; dealersCard = 9;
            firstWager          = 12; balance = 48; secondWager = 0; firstSum = 18; secondSum = 0; surrenderFlag = false;
            playersDecisions[0] = Decisions.Doubling; decisionsCounter = 1; bjFlag = 0;
            SomePlayersTurnTest(dealersCard, initialMoney, initialWager, playingDecks, decisionsCounter, firstSum, firstWager,
                                secondSum, secondWager, playersDecisions, balance, bjFlag, surrenderFlag, CardsCounterStrategyFlag);

            // Simple test of doubling function
            playingDecks.FillCards();
            gameCards    = playingDecks.Cards;
            gameCards[0] = 8; gameCards[1] = 3; gameCards[2] = 9; dealersCard = 6; firstSum = 20; firstWager = 12;
            balance      = 48; decisionsCounter = 1; playersDecisions[0] = Decisions.Doubling;
            SomePlayersTurnTest(dealersCard, initialMoney, initialWager, playingDecks, decisionsCounter, firstSum, firstWager,
                                secondSum, secondWager, playersDecisions, balance, bjFlag, surrenderFlag, CardsCounterStrategyFlag);

            // Instead of hitting we make doubling because our Cards count equals 2
            playingDecks.FillCards();
            gameCards           = playingDecks.Cards;
            gameCards[0]        = 3; gameCards[1] = 3; gameCards[2] = 6; gameCards[3] = 10; gameCards[4] = 6; gameCards[5] = 10;
            firstSum            = 19; secondSum = 19; dealersCard = 5; firstWager = 24; secondWager = 24; balance = 12; decisionsCounter = 5;
            playersDecisions[0] = Decisions.Splitting; playersDecisions[1] = Decisions.Doubling; playersDecisions[2] = Decisions.Doubling;
            playersDecisions[3] = Decisions.Doubling; playersDecisions[4] = Decisions.Doubling;
            SomePlayersTurnTest(dealersCard, initialMoney, initialWager, playingDecks, decisionsCounter, firstSum, firstWager,
                                secondSum, secondWager, playersDecisions, balance, bjFlag, surrenderFlag, CardsCounterStrategyFlag);

            // Now we use simple strategy
            // Player initially has two fives but instead of hitting he doubles because his score is 10
            for (int i = 0; i < 5; i++)
            {
                playersDecisions[i] = Decisions.Nothing;
            }
            bool simpleStrategyFlag = true;

            CardsCounterStrategyFlag = false;
            playingDecks.FillCards();
            gameCards    = playingDecks.Cards;
            gameCards[0] = 5; gameCards[1] = 5; gameCards[2] = 10; dealersCard = 10; firstSum = 20; firstWager = 12;
            balance      = 48; decisionsCounter = 1; secondWager = 0; secondSum = 0; playersDecisions[0] = Decisions.Doubling;
            SomePlayersTurnTest(dealersCard, initialMoney, initialWager, playingDecks, decisionsCounter, firstSum, firstWager,
                                secondSum, secondWager, playersDecisions, balance, bjFlag, surrenderFlag, CardsCounterStrategyFlag, simpleStrategyFlag);

            // Player initially has 2 aces and after his turn he has in both hands 21 score.
            playingDecks.FillCards();
            gameCards           = playingDecks.Cards;
            gameCards[0]        = 1; gameCards[1] = 1; gameCards[2] = 2; gameCards[3] = 8; gameCards[4] = 2; gameCards[5] = 8;
            firstSum            = 21; secondSum = 21; dealersCard = 5; firstWager = 12; secondWager = 12; balance = 36; decisionsCounter = 5;
            playersDecisions[0] = Decisions.Splitting; playersDecisions[1] = Decisions.Hitting; playersDecisions[2] = Decisions.Doubling;
            playersDecisions[3] = Decisions.Hitting; playersDecisions[4] = Decisions.Doubling;
            SomePlayersTurnTest(dealersCard, initialMoney, initialWager, playingDecks, decisionsCounter, firstSum, firstWager,
                                secondSum, secondWager, playersDecisions, balance, bjFlag, surrenderFlag, CardsCounterStrategyFlag, simpleStrategyFlag);
        }