public SquareNode(BoardNode board, Piece.Colour colour, Position position) { Board = board; Colour = colour; Position = position; }
private void OnCheckmate(Piece.Colour winner) { GetNode <Control>("Checkmate").Visible = true; var whoLabel = GetNode <Label>("Checkmate/Who"); whoLabel.Text = $"{winner} wins!"; }
/// <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); }
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>(); }; }
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"); }
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); }
/// <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); }
protected Player(BoardNode board, Piece.Colour colour) { Board = board; Colour = colour; }
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}"); } }
public MoveChooser(Piece.Colour player) { Player = player; History = new List <Move>(); }
public static float Score(Board board, Piece.Colour player, AIConstants constants) { return(new BoardScorer(board, player, constants).Score()); }
public BoardScorer(Board board, Piece.Colour player, AIConstants constants) { Board = board; Player = player; Constants = constants; }
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(); } }
public AIPlayer(BoardNode board, Piece.Colour colour) : base(board, colour) { MoveChooser = new MoveChooser(colour); Constants = AIConstants.Default; }
public IEnumerable <Move> _GetMoves(Piece.Colour player) { return(GetPositions(player) .Select(position => _GetMoves(position).Select(p => new Move(position, p))) .SelectMany(x => x)); }
public RandomPlayer(BoardNode board, Piece.Colour colour) : base(board, colour) { Random = new Random(); }
public IEnumerable <Position> GetPositions(Piece.Colour player) { return(GetPositions().Where(position => this[position].Value.Owner == player)); }
public static Piece.Colour Opposite(this Piece.Colour colour) { return(colour == Piece.Colour.White ? Piece.Colour.Black : Piece.Colour.White); }