示例#1
0
        public override int Play(Board Current)
        {
            MCEvaluation.SetStrength(Strength);
            List<int> CandidateMoves = Utile.PreprocessMoves(Current, Current.GetValidMoves());
            int ColumnToPlay = -1;
            float BestValue = float.MinValue;

            TProcess.Maximum = CandidateMoves.Count;
            TProcess.Value = 0;

            if (CandidateMoves.Count==0)
            {
                List<int> C = Current.GetValidMoves();
                return C[0];
            }

            if (CandidateMoves.Count==1)
            {
                return CandidateMoves[0];
            }

            foreach (int Move in CandidateMoves)
            {
                TProcess.PerformStep();
                float CurrentValue = MCEvaluation.Evaluate(Current, Move);
                if (CurrentValue > BestValue)
                {
                    BestValue = CurrentValue;
                    ColumnToPlay = Move;
                }
            }
            return ColumnToPlay;
        }
示例#2
0
 public static int GetPlayoutsNumber(Board Current, int ms)
 {
     int playouts_to_think = 0;
     MCScore MCEvaluation = new MCScore();
     // test speed
     MCEvaluation.SetStrength(1000);
     Stopwatch sw = new Stopwatch();
     sw.Start();
     int Move = Current.GetValidMoves()[0];
     float f = MCEvaluation.Evaluate(Current, Move);
     sw.Stop();
     playouts_to_think = Convert.ToInt32(1000 / (float)sw.Elapsed.TotalMilliseconds * (ms/(float)Current.GetValidMoves().Count));
     return playouts_to_think;
 }
示例#3
0
 protected State Expand(Board Game)
 {
     // maybe the next move
     int MoveId;
     // until the game ends play it
     while (Game.TestVictory() == State.empty)
     {
         List<int> ValidMoves = Game.GetValidMoves();
         MoveId = Rnd.Next(0, ValidMoves.Count - 1);
         Game.Move(ValidMoves[MoveId]);
     }
     // return the winner
     return Game.TestVictory();
 }
示例#4
0
 /// <summary>
 /// returns a move by minimax and deep cuts
 /// </summary>
 /// <param name="CurrentSituation">current game situation</param>
 /// <returns>move to play</returns>
 public override int Play(Board CurrentSituation)
 {
     List<int> CandidateMoves = CurrentSituation.GetValidMoves();    // get a possible Moves
     TProcess.Maximum = CandidateMoves.Count;                        // display the current thinking progress
     TProcess.Value = 0;
     float alpha = float.MinValue;                                   // current best valuation alpha
     int ColumnToPlay = -1;                                          // column to play
     foreach (int M in CandidateMoves)                               // test a canditate moves
     {
         TProcess.PerformStep();                                     // next move => update progressbar
         float eval = ScoreMove(CurrentSituation, M, 0);             // Score the move
         if (eval > alpha)                                           // is this move better?
         {
             alpha = eval;                                           // then we take this move
             ColumnToPlay = M;
         }
     }
     return ColumnToPlay;
 }
示例#5
0
        /// <summary>
        /// returns a move by minimax and deep cuts
        /// </summary>
        /// <remarks>
        /// THIS IS STILL BUGGY!!!
        /// </remarks>
        /// <param name="CurrentSituation">current game situation</param>
        /// <returns>move to play</returns>
        public override int Play(Board CurrentSituation)
        {
            Scores.SetStrength(100);
            CP = CurrentSituation.CurrentPlayer;
            List<int> CandidateMoves = CurrentSituation.GetValidMoves();
            TProcess.Maximum = CandidateMoves.Count;
            TProcess.Value = 0;
            float value = float.MinValue;
            int ColumnToPlay = -1;
            WorkingBoard = CurrentSituation.Clone();
            foreach (int M in CandidateMoves)
            {
                TProcess.PerformStep();
                float eval = ScoreMove(M, 0, float.MinValue, float.MaxValue);
                //MessageBox.Show(value.ToString());
                if (eval > value)
                {
                    value = eval;
                    ColumnToPlay = M;
                }
            }

            return ColumnToPlay;
        }
示例#6
0
 public override int Play(Board Situation)
 {
     List<int> ValidMoves = Situation.GetValidMoves();
     int MoveId = r.Next(0, ValidMoves.Count - 1);
     return ValidMoves[MoveId];
 }
示例#7
0
        /// <summary>
        /// internal score of move
        /// </summary>
        /// <param name="Situation">current game situation</param>
        /// <param name="Move">move to do</param>
        /// <param name="deep">current deep</param>
        /// <returns>value of move</returns>
        protected float ScoreMove(Board Situation, int Move, int deep)
        {
            // game finished?
            State Result = Situation.TestVictory();
            // if game is finished ...
            if (Result != State.empty)
            {
                // is draw?
                if (Result == State.draw)
                    return 0.0f;
                // else return score 1.0f for winning and -1.0f for loosing
                return (Result == Situation.CurrentPlayer) ? 1f : -1f;
            }
            // cut deep
            if (deep == MAX_DEEP)
            {
                // adjust strength dynamically
                int S = Convert.ToInt32(Strength / ((double)Math.Pow(7, MAX_DEEP)));
                Scores.SetStrength(S);
                // Score Board due monte carlo
                return Scores.Evaluate(Situation, Move);

            }

            Situation.Move(Move);                                   // do move
            List<int> CandidateMoves = Situation.GetValidMoves();   // new situation, so new possible moves

            float val = float.MinValue;                             // temp alpha value

            foreach (int M in CandidateMoves)
            {
                val = Math.Max(val, -1f * ScoreMove(Situation, M, deep + 1)); // evaluate each move by recursion
            }

            Situation.UnMove(Move);                               // redo move
            return val;                                           // return value
        }