Ejemplo n.º 1
0
        public Tuple <decimal, MoveType> AlphaBeta(Field.Field field, int depth, decimal alpha, decimal beta, string player, MoveType bestMovetype = MoveType.None)
        {
            var moves = field.Moves(player).Select(i => i.Key).ToList();

            //Console.Error.WriteLine($"update Curent-depth-{depth}");

            if (depth == 0 || moves.Count == 0)
            {
                alpha = Evaluate(field, field.GetByPlayerId(player));
                return(Tuple.Create(alpha, MoveType.None));
            }

            moves = OrderByClosness(field, new Point(field.Height / 2, field.Width / 2), moves);
            moves = OrderByClosness(field, field.EnemyPosition, moves);

            if (bestMovetype != MoveType.None)
            {
                moves.Remove(bestMovetype);
                moves.Insert(0, bestMovetype);
            }

            var bestMove = moves.Count > 0 ? moves[0] : MoveType.None;

            foreach (var move in moves)
            {
                CheckTime();

                field.MoveForth(move, player);
                var val = -AlphaBeta(field, depth - 1, -beta, -alpha, field.EnId).Item1;
                field.MoveBack(move, player);

                if (val > alpha)
                {
                    bestMove = move;
                    alpha    = val;
                    if (alpha >= beta)
                    {
                        return(Tuple.Create(alpha, bestMove));
                    }
                }
            }

            decimal suicideVal = -11 * (ComputePlayerValue(field, player) + 1);

            if (suicideVal > alpha)
            {
                foreach (var direction in field.Directions)
                {
                    if (player == field.MyId || player == field.EnId)
                    {
                        return(Tuple.Create(suicideVal, direction));
                    }
                }
            }

            return(Tuple.Create(alpha, bestMove));
        }
Ejemplo n.º 2
0
        private int ComputePlayerValue(Field.Field field, string playerId)
        {
            var player = field.GetByPlayerId(playerId);

            return(ComputePlayerValue(field, player));
        }