Ejemplo n.º 1
0
            //エピソードセットから評価関数を計算
            public void Evaluation()
            {
                int                  dealersFaceUpCard;
                int                  usableAceFlag = 0;
                StateRewardSet       SRSet;
                StateRewardOccurence SRO;

                for (int i = 0; i < FinalResultSet.Count; i++)
                {
                    dealersFaceUpCard = DealerCardsListSet[i][0];
                    int playerCardNum = 0;
                    int playersSum    = 0;
                    foreach (var card in PlayerCardsListSet[i])
                    {
                        if (card == 1 && (playersSum + 11) <= 21)
                        {
                            playersSum   += 11;
                            usableAceFlag = 1;
                        }
                        else if (card == 1 && (playersSum + 11) > 21)
                        {
                            playersSum += 1;
                        }
                        else if (card > 1 && (playersSum + card) <= 21)
                        {
                            playersSum += card;
                        }
                        else if (card > 1 && (playersSum + card) > 21)
                        {
                            if (usableAceFlag == 1)
                            {
                                playersSum   += card - 10;
                                usableAceFlag = 0;
                            }
                            else
                            {
                                playersSum += card;
                            }
                        }

                        if (12 <= playersSum && playersSum <= 21)
                        {
                            Console.WriteLine("Start Evaluation!");
                            Console.WriteLine("dealersFaceUpCard: {0}, playersSum: {1}, usableAceFlag: {2}, result: {3}"
                                              , dealersFaceUpCard, playersSum, usableAceFlag, FinalResultSet[i]);
                            //現タイミングでヒットかスタンドのどちらを取っているかを判定。ヒットであれば報酬は0とする
                            //現タイミングがプレイヤーにとっての最後のカードではなく、かつバーストではなかったら
                            if (playerCardNum + 1 < PlayerCardsListSet[i].Count && playersSum + PlayerCardsListSet[i][playerCardNum + 1] <= 21)
                            {
                                SRSet = new StateRewardSet(dealersFaceUpCard, playersSum, usableAceFlag, 0);
                            }
                            else
                            {
                                SRSet = new StateRewardSet(dealersFaceUpCard, playersSum, usableAceFlag, FinalResultSet[i]);
                            }

                            if (StateRewardTable.Count == 0)
                            {
                                SRO = new StateRewardOccurence(SRSet, 1);
                                StateRewardTable.Add(SRO);
                                Console.WriteLine("This is a First visit!");
                            }
                            else
                            {
                                bool existflag = false;
                                foreach (var sr in StateRewardTable)
                                {
                                    //現在の状態がすでに登録されているかどうかをチェックし、登録されていれば上書き。そうでなければ新規登録
                                    if (sr.srset.dealerState == SRSet.dealerState && sr.srset.playerSumState == SRSet.playerSumState &&
                                        sr.srset.usableAceState == SRSet.usableAceState)
                                    {
                                        sr.occurence++;
                                        sr.srset.reward += SRSet.reward;
                                        existflag        = true;
                                    }
                                }
                                if (!existflag)
                                {
                                    SRO = new StateRewardOccurence(SRSet, 1);
                                    StateRewardTable.Add(SRO);
                                }
                            }
                        }
                        playerCardNum++;
                    }
                }

                //結果の出力ルーティン
                var OrderedResult = StateRewardTable.OrderBy(a => a.srset.usableAceState)
                                    .ThenBy(a => a.srset.dealerState).ThenBy(a => a.srset.playerSumState);
                int indentChecker = 0;

                foreach (var state in OrderedResult)
                {
                    if (indentChecker != 0 && state.srset.dealerState != indentChecker)
                    {
                        Console.WriteLine("");
                    }
                    Console.WriteLine("{0}, {1}, {2}, {3}, {4}, {5}", state.srset.dealerState
                                      , state.srset.playerSumState, state.srset.usableAceState
                                      , state.occurence, state.srset.reward, (double)state.srset.reward / (double)state.occurence);
                    indentChecker = state.srset.dealerState;
                }
            }
Ejemplo n.º 2
0
 public StateRewardOccurence(StateRewardSet SRSet, int occ)
 {
     this.srset     = SRSet;
     this.occurence = occ;
 }