public override int Run(Board board, Player player)
        {
            Player enemy = BackendHelper.SwapPlayer(player);

            Player winner = board.Winner;

            if (winner == player)
            {
                return(inf);
            }
            else if (winner == enemy)
            {
                return(-inf);
            }

            int result = 0;

            for (int i = 0; i < board.Dimension; ++i)
            {
                result += MatchLine(board, player, new Place(i, 0), new Place(0, 1));
                result += MatchLine(board, player, new Place(0, i), new Place(1, 0));
            }

            //if (board.GetPlayer(new Place(3, 3)) == player)
            //    result++;
            //else if (board.GetPlayer(new Place(3, 3)) == enemy)
            //    result--;

            return(result);
        }
Beispiel #2
0
 public override string ToString()
 {
     return(String.Format("{0}: {1} -> {2}",
                          BackendHelper.PlayerToString(player),
                          source,
                          destination));
 }
Beispiel #3
0
        public override int GetHashCode()
        {
            int result = 0;

            foreach (Place place in BackendHelper.GetAllPlaces(dimension))
            {
                result ^= hashValues[place.Column, place.Row, (int)GetPlayer(place)];
            }
            return(result);
        }
        private int MatchLine(Board board, Player player, Place place, Place vector)
        {
            Dictionary <Player, int> places = new Dictionary <Player, int>();

            places[Player.None]  = 0;
            places[Player.White] = 0;
            places[Player.Black] = 0;

            Player enemy  = BackendHelper.SwapPlayer(player);
            Place  place1 = place;

            double resultOffensive = 0;
            double resultDefensive = 0;
            int    count           = 0;

            //while (BackendHelper.IsPlaceValid(place1, board.Dimension))
            for (int i = 0; i < board.Dimension; ++i)
            {
                places[board.GetPlayer(place1)]++;
                count++;

                if (count > 4)
                {
                    places[board.GetPlayer(place1 + (-4 * vector))]--;
                    count--;
                }

                if (count == 4)
                {
                    if (places[enemy] == 0)
                    {
                        resultOffensive += lineBonus[places[Player.None]];
                    }
                    else if (places[player] == 0)
                    {
                        resultDefensive += lineBonus[places[Player.None]];
                    }
                }

                place1 += vector;
            }

            switch (player)
            {
            case Player.Black:
                resultOffensive *= 1.1;
                break;

            case Player.White:
                resultDefensive *= 0.9;
                break;
            }

            return((int)Math.Round(resultOffensive - resultDefensive));
        }
Beispiel #5
0
        static Board()
        {
            hashValues = new int[maxBoardSize, maxBoardSize, 3];
            Random rand = new Random();

            foreach (Place place in BackendHelper.GetAllPlaces(maxBoardSize))
            {
                hashValues[place.Column, place.Row, (int)Player.None]  = 0;
                hashValues[place.Column, place.Row, (int)Player.White] = rand.Next();
                hashValues[place.Column, place.Row, (int)Player.Black] = rand.Next();
            }
        }
Beispiel #6
0
        public Transition PerformMove(Move move)
        {
            Transition transition = new Transition();

            PerformAtomicMove(move);
            transition.Moves.Add(move);

            if (move.Source != null)
            {
                //faza II - zbijanie

                foreach (Place vector1 in BackendHelper.Vectors.Values)
                {
                    Place place1 = move.Destination + vector1;

                    if (!BackendHelper.IsPlaceValid(place1, dimension))
                    {
                        continue;
                    }

                    Player enemy = GetPlayer(place1);

                    //sąsiad musi istnieć i być przeciwnikiem
                    if (enemy == Player.None || enemy == move.Player)
                    {
                        continue;
                    }

                    Place place2 = place1 + vector1;

                    if (!BackendHelper.IsPlaceValid(place2, dimension))
                    {
                        continue;
                    }

                    Player friend = GetPlayer(place2);

                    //pionek musi istnieć i nie być przeciwnikiem
                    if (friend == Player.None || friend != move.Player)
                    {
                        continue;
                    }

                    Move capture = new Move(enemy, place1, null);
                    PerformAtomicMove(capture);
                    transition.Moves.Add(capture);
                }
            }

            return(transition);
        }
Beispiel #7
0
        private Result Run(Evaluator evaluator, Board board, Player player, int depth, int alpha, int beta)
        {
            hits++;

            if (depth == 0 || board.Winner != Player.None)
            {
                return(new Result(null, evaluator.Run(board, player)));
            }

            Result best = new Result(null, -inf);

            List <Move> moves = board.GetPossibleMovesSorted(player);

            if (depth == start)
            {
                total = moves.Count;
            }

            foreach (Move move in moves)
            {
                Transition transition = board.PerformMove(move);
                Result     candidate  = Run(evaluator, board, BackendHelper.SwapPlayer(player), depth - 1, -beta, -alpha);

                board.ReverseTransition(transition);

                if (-candidate.Value > best.Value)
                {
                    best = new Result(move, -candidate.Value);
                }

                if (best.Value >= beta)
                {
                    break;
                }

                if (best.Value > alpha)
                {
                    alpha = best.Value;
                }

                if (depth == start)
                {
                    done++;
                }
            }

            return(best);
        }
Beispiel #8
0
        public List <Move> GetPossibleMoves(Player player)
        {
            List <Move> moves = new List <Move>();

            if (befores[player] > 0)
            {
                //faza I - dostawianie

                foreach (Place destination in BackendHelper.GetAllPlaces(dimension))
                {
                    if (GetPlayer(destination) == Player.None)
                    {
                        moves.Add(new Move(player, null, destination));
                    }
                }
            }
            else
            {
                //faza II - przesuwanie

                foreach (Place source in BackendHelper.GetAllPlaces(dimension))
                {
                    if (GetPlayer(source) == player)
                    {
                        foreach (Place vector in BackendHelper.Vectors.Values)
                        {
                            Place destination = source + vector;

                            if (BackendHelper.IsPlaceValid(destination, dimension))
                            {
                                if (GetPlayer(destination) == Player.None)
                                {
                                    moves.Add(new Move(player, source, destination));
                                }
                            }
                        }
                    }
                }
            }

            return(moves);
        }
Beispiel #9
0
        public override int Run(Board board, Player player)
        {
            int result = 0;

            foreach (Place place1 in BackendHelper.GetAllPlaces(board.Dimension))
            {
                int    k;
                Player currentPlayer;
                if (player == board.GetPlayer(place1))
                {
                    currentPlayer = player;
                    k             = 1;
                }
                else if (BackendHelper.SwapPlayer(player) == board.GetPlayer(place1))
                {
                    currentPlayer = BackendHelper.SwapPlayer(player);
                    k             = -1;
                }
                else
                {
                    continue;
                }

                foreach (Place vector in BackendHelper.Vectors.Values)
                {
                    Place place2 = place1 + vector;

                    result += k;

                    while (BackendHelper.IsPlaceValid(place2, board.Dimension) &&
                           board.GetPlayer(place2) == currentPlayer)
                    {
                        result += k;
                        place2  = place2 + vector;
                    }
                }
            }

            return(result);
        }
        private Result RunInternal(Evaluator evaluator, Board board, Player player, int depth)
        {
            hits++;

            if (depth == 0 || board.Winner != Player.None)
            {
                return(new Result(null, evaluator.Run(board, player)));
            }

            Result winner = new Result(null, int.MinValue);

            List <Move> moves = board.GetPossibleMovesSorted(player);

            if (depth == start)
            {
                total = moves.Count;
            }

            foreach (Move move in moves)
            {
                Transition transition = board.PerformMove(move);

                Result candidate = RunInternal(evaluator, board, BackendHelper.SwapPlayer(player), depth - 1);

                if (-candidate.Value > winner.Value)
                {
                    winner = new Result(move, -candidate.Value);
                }

                board.ReverseTransition(transition);

                if (depth == start)
                {
                    done++;
                }
            }

            return(winner);
        }
Beispiel #11
0
        public override string ToString()
        {
            string result = "";

            foreach (Place place in BackendHelper.GetAllPlaces(dimension))
            {
                switch (GetPlayer(place))
                {
                case Player.None:
                    result += ' ';
                    break;

                case Player.Black:
                    result += 'B';
                    break;

                case Player.White:
                    result += 'W';
                    break;
                }
            }

            return(result);
        }
Beispiel #12
0
        public override bool Equals(object other)
        {
            Board board = other as Board;

            if (board == null)
            {
                return(base.Equals(other));
            }

            if (dimension != board.dimension)
            {
                return(false);
            }

            foreach (Place place in BackendHelper.GetAllPlaces(dimension))
            {
                if (GetPlayer(place) != board.GetPlayer(place))
                {
                    return(false);
                }
            }

            return(true);
        }
        private Result Run(Evaluator evaluator, Board board, Player player, int depth, int alpha, int beta)
        {
            hits++;

            if (depth == 0 || board.Winner != Player.None)
            {
                return(new Result(null, evaluator.Run(board, player)));
            }

            int prevalpha = alpha;

            Transposition transposition = transTable.Lookup(board, player);

            if (transposition != null && transposition.Depth >= depth)
            {
                switch (transposition.Bound)
                {
                case EvaluationBound.Lower:
                    alpha = Math.Max(alpha, transposition.BestMove.Value);
                    break;

                case EvaluationBound.Upper:
                    beta = Math.Min(beta, transposition.BestMove.Value);
                    break;

                case EvaluationBound.Accurate:
                    alpha = beta = transposition.BestMove.Value;
                    break;
                }

                if (alpha >= beta)
                {
                    return(transposition.BestMove);
                }
            }

            List <Move> possibleMoves = board.GetPossibleMovesSorted(player);

            if (depth == start)
            {
                total = possibleMoves.Count;
            }

            if (transposition != null)
            {
                int index = possibleMoves.IndexOf(transposition.BestMove.Move);
                if (index == -1)
                {
                    throw new Exception(String.Format("Move \"{0}\" not found.", transposition.BestMove.Move));
                }
                possibleMoves[index] = possibleMoves[0];
                possibleMoves[0]     = transposition.BestMove.Move;
            }

            Result best = new Result(null, -inf);

            foreach (Move move in possibleMoves)
            {
                Transition transition = board.PerformMove(move);

                Result candidate = Run(evaluator, board, BackendHelper.SwapPlayer(player), depth - 1, -beta, -alpha);

                board.ReverseTransition(transition);

                if (-candidate.Value > best.Value)
                {
                    best = new Result(move, -candidate.Value);
                }

                if (best.Value >= beta)
                {
                    break;
                }

                if (best.Value > alpha)
                {
                    alpha = best.Value;
                }

                if (depth == start)
                {
                    done++;
                }
            }

            transTable.Save(board, best, prevalpha, beta, depth);

            return(best);
        }
Beispiel #14
0
 public override string ToString()
 {
     return(String.Format("{0}{1}",
                          BackendHelper.ColumnToString(column),
                          BackendHelper.RowToString(row)));
 }