/// <summary>
        /// Use this method to return the value You want to bet
        /// </summary>
        /// <param name="gameState"></param>
        /// <returns></returns>
        public static int BetRequest(JObject gameState)
        {
            try
            {
                //var des = gameState.ToObject<RequestStructure.GameState>();
                var des = new RequestStructure.GameState(gameState);

                Hand hand = CheckCardsOnHand(des);

                int? rank = CreateRank(des, hand);
                int bet = CalculateBet(gameState, rank);

                return bet;
            }
            catch (Exception e)
            {
                Console.WriteLine("Error occured: " + e.Message + "\n\t" + e.StackTrace);
                Console.WriteLine("Gamestate = " + gameState.ToString());

                // try to calculate bet without rank
                try
                {
                    int bet = CalculateBet(gameState, null);
                    return bet;
                }
                catch (Exception e2)
                {
                    Console.WriteLine("Could not calculate bet without rank: " + e2.Message + "\n\t" + e2.StackTrace);
                    return 200;
                }
            }
        }
 public void IsTwoAces315PointsRound()
 {
     var content = File.ReadAllText("..\\..\\gamestateTwoAces.json");
     gamestate = JObject.Parse(content);
     var state = new RequestStructure.GameState(gamestate);
     var hand = PokerPlayer.CheckCardsOnHand(state);
     var result = PokerPlayer.CreateRank(state, hand);
     Assert.AreEqual(result.Value, (int)StartingHandRanking.AA);
 }
 public void IsNullForSecondRound()
 {
     var content = File.ReadAllText("..\\..\\gamestate.json");
     gamestate = JObject.Parse(content);
     var state = new RequestStructure.GameState(gamestate);
     var hand = PokerPlayer.CheckCardsOnHand(state);
     var result = PokerPlayer.CreateRank(state,hand);
     Assert.IsNotNull(result);
 }
        public void IsTwoCardsNothingTest()
        {
            var content = File.ReadAllText("..\\..\\gamestate.json");
            gamestate = JObject.Parse(content);
            var state = new RequestStructure.GameState(gamestate);

            var result = PokerPlayer.CheckCardsForFlush(state.CommunityCards.Concat(state.OurPlayer.HoleCards).ToList());
            Assert.AreEqual(result, Hand.Nothing);
        }
        public void TestCreateRankDoesNotCrrash()
        {
            var content = File.ReadAllText("..\\..\\gamestateKingAceOther.json");
            gamestate = JObject.Parse(content);
            var state = new RequestStructure.GameState(gamestate);

            state.OurCards.Clear();
            state.OurCards.Add(new RequestStructure.Card("2", RequestStructure.Suit.Hearts));
            state.OurCards.Add(new RequestStructure.Card("10", RequestStructure.Suit.Clubs));
            var hand = PokerPlayer.CheckCardsOnHand(state);
            var result = PokerPlayer.CreateRank(state, hand);
        }
        public void test_deserialization()
        {
            //var s = gamestate;
            var content = File.ReadAllText("..\\..\\gamestate.json");
            gamestate = JObject.Parse(content);
            var state = new RequestStructure.GameState(gamestate);

            Assert.IsTrue(state.TournamentId == "550d1d68cd7bd10003000003");
            Assert.IsTrue(state.GameId == "550da1cb2d909006e90004b1");
            Assert.IsTrue(state.Round == 0);
            Assert.IsTrue(state.BetIndex == 0);
            Assert.IsTrue(state.SmallBlind == 10);
            Assert.IsTrue(state.CurrentBuyIn == 320);
            Assert.IsTrue(state.Pot == 400);
            Assert.IsTrue(state.MinimumRaise == 240);
            Assert.IsTrue(state.Dealer== "1");
            Assert.IsTrue(state.Orbits == 7);
            Assert.IsTrue(state.InAction == 1);

            var cards = state.CommunityCards;
            Assert.IsNotNull(cards);
            Assert.IsTrue(cards.Count ==3);
            var card = cards.First();
            Assert.IsTrue(card.Rank == "4");
            Assert.IsTrue(card.Suit== RequestStructure.Suit.Spades);

            var players= state.Players;
            Assert.IsNotNull(players);
            Assert.IsTrue(players.Count == 3);
            var player = players[1];
            Assert.IsTrue(player.Id== 1);
            Assert.IsTrue(player.Name== "Bob");
            Assert.IsTrue(player.Status== RequestStructure.PlayerStatus.Active);
            Assert.IsTrue(player.Version == "Default random player");
            Assert.IsTrue(player.Stack== 1590);
            Assert.IsTrue(player.Bet== 80);

            var holeCards = player.HoleCards;
            Assert.AreEqual(holeCards.Count, 2);
            var fCard = holeCards.First();
            Assert.AreEqual(fCard.Rank, "6");
            Assert.AreEqual(fCard.Suit, RequestStructure.Suit.Hearts);

            var ourPlayer = state.OurPlayer;
            Assert.AreEqual(ourPlayer.Name,"Bob");

            Assert.IsNotNull(state);
        }
        public static dynamic CalculateBet(JObject gameState, int? rank)
        {
            int rankValue = 0;
            if (rank.HasValue)
            {
                rankValue = rank.Value;
            }
            if (rank.HasValue && rank < 50)
            {
                return 0;
            }

            var game = new RequestStructure.GameState(gameState);
            var currentPot = Convert.ToInt32(game.Pot);
            try
            {
                var funnyPokerBot = game.Players.Single(p => p.Name == "FunnyPokerBot");
                if (funnyPokerBot.Bet > currentPot*5 && funnyPokerBot.Status == RequestStructure.PlayerStatus.Active && !game.CommunityCards.Any())
                {
                    return game.OurPlayer.Stack;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            var allInPlayersCount = GetAllInPlayersCount(game);

            if(allInPlayersCount == 1)
            {
                return GetMaxBet(game);
            }
            if(allInPlayersCount > 2 && !game.CommunityCards.Any() && rankValue < 200)
            {
                return 0;
            }

            var maxBet = GetMaxBet(game);

            double expectedGainChance = maxBet/currentPot;
            double positionFactor = 1 + 1/(GetActivePlayerCount(game) - GetCurrentPosition(game) + 1);
            expectedGainChance = expectedGainChance*positionFactor;

            var activePlayerCount = GetActivePlayerCount(game);

            int actualBet = 0;
            if (currentPot <= 3*game.CurrentBuyIn || expectedGainChance >= (1/activePlayerCount))
            {
                if (rankValue > 200)
                {
                    actualBet = currentPot;
                }
                else
                {
                    if (rankValue < 130 && game.CurrentBuyIn > game.OurPlayer.Stack * 0.15)
                    {
                        actualBet = 0;
                    }
                    else
                    {
                        actualBet = game.CurrentBuyIn;
                    }
                }
            }
            return actualBet;
        }