예제 #1
0
        public SquareNode(BoardNode board, Piece.Colour colour, Position position)
        {
            Board = board;

            Colour   = colour;
            Position = position;
        }
예제 #2
0
        private void OnCheckmate(Piece.Colour winner)
        {
            GetNode <Control>("Checkmate").Visible = true;

            var whoLabel = GetNode <Label>("Checkmate/Who");

            whoLabel.Text = $"{winner} wins!";
        }
예제 #3
0
        /// <summary>
        /// Returns true if a piece at "position" can be taken by a piece owned by "from"
        /// </summary>
        /// <param name="position"></param>
        /// <param name="from"></param>
        /// <returns></returns>
        public bool UnderAttack(Position position, Piece.Colour from)
        {
            foreach (var move in _GetMoves(from))
            {
                if (move.To == position)
                {
                    return(true);
                }
            }

            return(false);
        }
예제 #4
0
        public HumanPlayer(BoardNode board, Piece.Colour colour) : base(board, colour)
        {
            SquareClickedTcs     = new TaskCompletionSource <Position>();
            board.SquareClicked += p =>
            {
                if (Board.CurrentPlayer != Colour)
                {
                    return;
                }

                SquareClickedTcs.SetResult(p);
                SquareClickedTcs = new TaskCompletionSource <Position>();
            };
        }
예제 #5
0
        public Position KingPosition(Piece.Colour player)
        {
            for (int i = 0; i < Size; i++)
            {
                for (int j = 0; j < Size; j++)
                {
                    var position = new Position(j, i);
                    if (this[position] is Piece piece)
                    {
                        if (piece.Type == Piece.PieceType.King && piece.Owner == player)
                        {
                            return(position);
                        }
                    }
                }
            }

            throw new ApplicationException($"{player} does not have a king");
        }
예제 #6
0
        public IEnumerable <Move> GetMoves(Piece.Colour player)
        {
            var moves = GetPositions(player)
                        .Select(position => GetMoves(position))
                        .SelectMany(x => x);

            if (!moves.Any())
            {
                var kingpos = KingPosition(player);
                if (UnderAttack(kingpos, player.Opposite()))
                {
                    Checkmate?.Invoke(player.Opposite());
                }
                else
                {
                    Stalemate?.Invoke();
                }
            }

            return(moves);
        }
예제 #7
0
        /// <summary>
        /// The more pieces "player" has, the more points they have.
        /// </summary>
        /// <param name="player"></param>
        /// <returns></returns>
        private float ScoreMaterial(Piece.Colour player)
        {
            var material = Board.GetPositions(player);

            float score = 0;

            foreach (var position in material)
            {
                switch (Board[position].Value.Type)
                {
                case Piece.PieceType.Pawn:
                    score += Constants.PawnScore;
                    break;

                case Piece.PieceType.Knight:
                    score += Constants.KnightScore;
                    break;

                case Piece.PieceType.Bishop:
                    score += Constants.BishopScore;
                    break;

                case Piece.PieceType.Rook:
                    score += Constants.RookScore;
                    break;

                case Piece.PieceType.Queen:
                    score += Constants.QueenScore;
                    break;

                default:
                    break;
                }
            }

            return(score);
        }
예제 #8
0
 protected Player(BoardNode board, Piece.Colour colour)
 {
     Board  = board;
     Colour = colour;
 }
예제 #9
0
파일: Menu.cs 프로젝트: Leowbattle/Chess
        private Player CreatePlayer(PlayerTypeSelector.PlayerType type, BoardNode board, Piece.Colour colour)
        {
            switch (type)
            {
            case PlayerTypeSelector.PlayerType.Human:
                return(new HumanPlayer(board, colour));

            case PlayerTypeSelector.PlayerType.Computer:
                return(new AIPlayer(board, colour));

            case PlayerTypeSelector.PlayerType.Random:
                return(new RandomPlayer(board, colour));

            default:
                throw new ApplicationException($"Invalid player type {type}");
            }
        }
예제 #10
0
 public MoveChooser(Piece.Colour player)
 {
     Player  = player;
     History = new List <Move>();
 }
예제 #11
0
 public static float Score(Board board, Piece.Colour player, AIConstants constants)
 {
     return(new BoardScorer(board, player, constants).Score());
 }
예제 #12
0
 public BoardScorer(Board board, Piece.Colour player, AIConstants constants)
 {
     Board     = board;
     Player    = player;
     Constants = constants;
 }
예제 #13
0
        public async override void _Ready()
        {
            bool gameRunning = true;

            Board            = new Board();
            Board.Checkmate += winner =>
            {
                gameRunning = false;

                GD.Print($"Checkmate! {winner} wins.");
                Checkmate?.Invoke(winner);
            };
            Board.Stalemate += () =>
            {
                gameRunning = false;

                GD.Print("Stalemate!");
                Stalemate?.Invoke();
            };

            UI = GetNode <UI>("UI");

            var squares = GetNode <Control>("Squares");
            var colour  = Piece.Colour.Black;

            for (int i = 0; i < Board.Size; i++)
            {
                for (int j = 0; j < Board.Size; j++)
                {
                    var square = new SquareNode(this, colour, new Position(j, i));
                    square.Clicked += position =>
                    {
                        SquareClicked?.Invoke(position);
                    };
                    Squares[j, i] = square;
                    squares.AddChild(square);

                    colour = colour.Opposite();
                }
                colour = colour.Opposite();
            }

            // WhitePlayer = new HumanPlayer(this, Piece.Colour.White);
            //BlackPlayer = new HumanPlayer(this, Piece.Colour.Black);

            //WhitePlayer = new RandomPlayer(this, Piece.Colour.White);
            //BlackPlayer = new RandomPlayer(this, Piece.Colour.Black);

            //WhitePlayer = new AIPlayer(this, Piece.Colour.White);
            // BlackPlayer = new AIPlayer(this, Piece.Colour.Black);

            var drawer = new PieceDrawer(this);

            squares.AddChild(drawer);

            CurrentPlayer = Piece.Colour.White;
            while (true)
            {
                LegalMoves = Board.GetMoves(CurrentPlayer).ToHashSet();
                if (!gameRunning)
                {
                    break;
                }

                var move = await Current.GetMoveAsync();

                await Task.Delay(10);

                GD.Print($"{CurrentPlayer} wants to move {move}");
                await Task.Run(() => Board.DoMove(move, p => Current.Promote(p).Result));

                drawer.Update();

                CurrentPlayer = CurrentPlayer.Opposite();
            }
        }
예제 #14
0
 public AIPlayer(BoardNode board, Piece.Colour colour) : base(board, colour)
 {
     MoveChooser = new MoveChooser(colour);
     Constants   = AIConstants.Default;
 }
예제 #15
0
 public IEnumerable <Move> _GetMoves(Piece.Colour player)
 {
     return(GetPositions(player)
            .Select(position => _GetMoves(position).Select(p => new Move(position, p)))
            .SelectMany(x => x));
 }
예제 #16
0
 public RandomPlayer(BoardNode board, Piece.Colour colour) : base(board, colour)
 {
     Random = new Random();
 }
예제 #17
0
 public IEnumerable <Position> GetPositions(Piece.Colour player)
 {
     return(GetPositions().Where(position => this[position].Value.Owner == player));
 }
예제 #18
0
 public static Piece.Colour Opposite(this Piece.Colour colour)
 {
     return(colour == Piece.Colour.White ? Piece.Colour.Black : Piece.Colour.White);
 }