Example #1
0
        private TreeNode MiniMax(Board board, Double depth, Double alpha, Double beta, Boolean maximizer)
        {
            if(depth == 0)
            {
                return GetLeafNode(board, maximizer);
            }
            var pieces = board.GetPiecesForPlayer(board.PlayerTurn);
            var bestScore = maximizer ? _minScore : _maxScore;
            var bestMove = default(Move);

            var moves = pieces.SelectMany(x => x.GetPossibleMoves(board));
            if (!moves.Any())
            {
                if (board.PlayerHasCheck(board.PlayerTurn))
                {
                    return new TreeNode(null, maximizer ? _maxScore : _minScore);
                }
            }

            foreach (var mv in moves)
            {
                var newBoard = board.Copy();
                newBoard.MakeMove(mv);
                var newNode = MiniMax(newBoard, depth - 1, alpha, beta, !maximizer);
                if (maximizer)
                {
                    if(newNode.Score > bestScore)
                    {
                        bestScore = newNode.Score;
                        bestMove = mv;
                    }
                    alpha = Math.Max(alpha, bestScore);
                    if(beta <= alpha)
                    {
                        return new TreeNode(bestMove, bestScore);
                    }
                }
                else
                {
                    if(newNode.Score < bestScore)
                    {
                        bestScore = newNode.Score;
                        bestMove = mv;
                    }
                    beta = Math.Min(beta, bestScore);
                    if(beta <= alpha)
                    {
                        return new TreeNode(bestMove, bestScore);
                    }
                }
            }
            return new TreeNode(bestMove, bestScore);
        }
Example #2
0
        public Move GetNextMove(Board board)
        {
            var allMoves = new List<Move>();
            var pcs = board.GetPiecesForPlayer(board.PlayerTurn);
            foreach(var pc in pcs)
            {
                var moves = pc.GetPossibleMoves(board);
                allMoves.AddRange(moves);
            }

            var randomIndex = new Random().Next(allMoves.Count);
            return allMoves[randomIndex];
        }
        public double Evaluate(Board board)
        {
            Double score = 0;

            var playerPieces = board.GetPiecesForPlayer(board.PlayerTurn);
            var opposingPieces = board.GetPiecesForPlayer(board.OpposingPlayer());

            score = GetScore(playerPieces, board);
            score -= GetScore(opposingPieces, board);

            //score = 200 * (CountPiecesByType(playerPieces, PieceType.King) - CountPiecesByType(opposingPieces, PieceType.King));
            //score += 9 * (CountPiecesByType(playerPieces, PieceType.Queen) - CountPiecesByType(opposingPieces, PieceType.Queen));
            //score += 5 * (CountPiecesByType(playerPieces, PieceType.Rook) - CountPiecesByType(opposingPieces, PieceType.Rook));
            //score += 3 * ((CountPiecesByType(playerPieces, PieceType.Bishop) - CountPiecesByType(opposingPieces, PieceType.Bishop)) +
            //                (CountPiecesByType(playerPieces, PieceType.Knight) - CountPiecesByType(opposingPieces, PieceType.Knight)));
            //score += 1 * (CountPiecesByType(playerPieces, PieceType.Pawn) - (CountPiecesByType(opposingPieces, PieceType.Pawn)));

            //score -= .5 * ((DoubledPawns(playerPieces) - DoubledPawns(opposingPieces)) +
            //                (BlockedPawns(playerPieces) - BlockedPawns(opposingPieces)) +
            //                (IsolatedPawns(playerPieces) - IsolatedPawns(opposingPieces)));
            //score += .1 * (CountMoves(board, playerPieces) - CountMoves(board, opposingPieces));
            return score;
        }