Ejemplo n.º 1
0
        /// <summary>
        /// find next move by direct score
        /// </summary>
        /// <param name="x">current board</param>
        /// <returns>next move</returns>
        static public int DirectScoreMove(Board x)
        {
            double maxScore = 0;
            int    maxi     = -1;

            for (int k = 0; k < 4; k++)
            {
                if (BoardControl.ExecuteMove(x, k) == x)
                {
                    continue;
                }
                if (PreScore.DirectScore(BoardControl.ExecuteMove(x, k)) > maxScore)
                {
                    maxScore = PreScore.DirectScore(BoardControl.ExecuteMove(x, k));
                    maxi     = k;
                }
            }
            if (maxi == -1)
            {
                return(new Random(DateTime.Now.Millisecond).Next() % 4);
            }
            else
            {
                return(maxi);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// play game for monte carlo method
        /// </summary>
        /// <param name="Str">play strategy</param>
        /// <param name="board">current board</param>
        /// <param name="nmove">monte carlo steps</param>
        /// <param name="move">pre-pointed next step</param>
        /// <returns>score by monte carlo</returns>
        public static double MontePlay(Strategy Str, Board board, int nmove, int move = -1)
        {
            bool first = true;

            while (true)
            {
                UInt64 newboard;
                if (first && (move != -1))
                {
                    if (BoardControl.ExecuteMove(board, move) == board)
                    {
                        return(0);
                    }
                    first = false;
                }
                else
                {
                    for (move = 0; move < 4; move++)
                    {
                        if (BoardControl.ExecuteMove(board, move) != board)
                        {
                            break;
                        }
                    }
                    if (move == 4)
                    {
                        break;
                    }
                    move = Str(board);
                    if (move < 0)
                    {
                        break;
                    }
                }

                newboard = BoardControl.ExecuteMove(board, move);
                if (newboard == board)
                {
                    continue;
                }
                UInt64 tile = BoardControl.RandomTile();
                board = BoardControl.InsertTileRand(newboard, tile);
                nmove--;
                if (nmove <= 0)
                {
                    return(PreScore.DirectScore(board));
                }
            }
            return(0);
        }
Ejemplo n.º 3
0
 static void Main(string[] args)
 {
     BoardControl.Initialize();
     PreScore.Initialize();
     PlayGame.Play(AI.SearchMove);
 }