internal virtual MoveCheckResult CanMoveUp(ChessBoard board, BoardTile currentTile, BoardTile destinationTile, int count = ChessBoard.BoardSize) { if (currentTile.X != destinationTile.X) { return(MoveCheckResult.ContinueCheck); } if (destinationTile.Y < currentTile.Y) { for (int t = 1; t <= count; t++) { var enemy = board.PieceAt(currentTile.X, currentTile.Y - t); if (currentTile.Y - t == destinationTile.Y && (enemy == null)) { return(MoveCheckResult.CanMove); } if (currentTile.Y - t == destinationTile.Y && (enemy != null && enemy.IsWhite != this.IsWhite)) { return(MoveCheckResult.CanMove); } if (enemy != null && enemy != this) { return(MoveCheckResult.ContinueCheck); } } } return(MoveCheckResult.ContinueCheck); }
public Board() { _board = new BoardTile[RowColLen, RowColLen]; for (int i = 0; i < RowColLen; i++) { for (int j = 0; j < RowColLen; j++) { _board[i, j] = new BoardTile(new PiecePosition(i, j)); } } }
/// <summary> /// Generate the chess board. /// </summary> public void GenerateBoard() { Tiles = new BoardTile[BoardSize, BoardSize]; for (int y = 0; y < BoardSize; y++) { for (int x = 0; x < BoardSize; x++) { Tiles[y, x] = new BoardTile(x, y); } } }
/// <summary> /// Select a tile. /// </summary> /// <param name="point"></param> /// <returns>Returns true if the turn should be ended for current player.</returns> public bool SelectTile(Point point, Player player) { bool result = false; var clickedTile = Tiles[point.Y, point.X]; if (SelectedTile != clickedTile) { if (SelectedTile != null) { if (MoveSelectedPieceTo(clickedTile, player)) { SelectedTile = null; result = true; } else { // Only allow selection if player's own pieces. if (player.IsWhite == clickedTile.OccupyingPiece?.IsWhite) { SelectedTile = clickedTile; } else { SelectedTile = null; } } } else { // Only allow selection if player's own pieces. if (player.IsWhite == clickedTile.OccupyingPiece?.IsWhite) { SelectedTile = clickedTile; } else { SelectedTile = null; } } } else { SelectedTile = null; } DrawGame(); return(result); }
/// <summary> /// Checks whether the given piece can move to any spot. /// </summary> /// <param name="tile"></param> /// <returns></returns> private bool CanMoveToAnything(BoardTile tile) { for (int y = 0; y < BoardSize; y++) { for (int x = 0; x < BoardSize; x++) { if (tile.OccupyingPiece != null && tile.CanMoveTo(this, Tiles[y, x])) { return(true); } } } return(false); }
public bool CanMoveTo(ChessBoard board, BoardTile tile) { // By putting this code here we check whether or not the king will be attacked if we move, // and also checks what spots we CAN move to when the king is under attack. if (OccupyingPiece.WillKingBeAttackedIfMovedTo(board, this, tile)) { return(false); } if (OccupyingPiece != null) { return(OccupyingPiece.CanMoveTo(board, this, tile)); } return(false); }
public ChessBoard() { grid = new BoardTile[8, 8]; bool flag = true; for (int i = 0; i < grid.GetLength(0); i++) { for (int j = 0; j < grid.GetLength(1); j++) { Label temp; temp = new Label(); temp.AutoSize = false; Color tempColor; if (flag) { tempColor = Color.PeachPuff; } else { tempColor = Color.Peru; } temp.Size = new Size(50, 50); temp.Location = new Point((i * 50) + 120, ((j * 50) + 70)); temp.Visible = true; temp.BorderStyle = BorderStyle.Fixed3D; temp.Name = "Square"; grid[i, j] = new BoardTile(temp, tempColor); //GridArea(grid[i, j]); flag = !flag; } flag = !flag; } }
internal bool WillKingBeAttackedIfMovedTo(ChessBoard board, BoardTile currentTile, BoardTile destinationTile) { bool result = false; var friendlyKingTile = board.TilesByPieceType(typeof(Chess.Pieces.King), this.IsWhite).FirstOrDefault(); // Temporarely move piece to destination to check if king will be attacked. ChessPiece tempHolder = destinationTile.OccupyingPiece; destinationTile.OccupyingPiece = currentTile.OccupyingPiece; if (currentTile != destinationTile) { currentTile.OccupyingPiece = null; } if (this.GetType() == typeof(Chess.Pieces.King)) { friendlyKingTile = destinationTile; } for (int y = 0; y < ChessBoard.BoardSize; y++) { for (int x = 0; x < ChessBoard.BoardSize; x++) { var tileToCheck = board.Tiles[y, x]; if (tileToCheck.OccupyingPiece != null && tileToCheck.OccupyingPiece != this && tileToCheck.OccupyingPiece.CanMoveTo(board, tileToCheck, friendlyKingTile)) { result = true; break; } } } // Move pieces back. currentTile.OccupyingPiece = destinationTile.OccupyingPiece; destinationTile.OccupyingPiece = tempHolder; return(result); }
internal virtual MoveCheckResult CanMoveDownRight(ChessBoard board, BoardTile currentTile, BoardTile destinationTile, int count = ChessBoard.BoardSize) { for (int t = 1; t <= count; t++) { Point p1 = new Point(currentTile.X + t, currentTile.Y + t); var enemy = board.PieceAt(p1.X, p1.Y); if ((p1.X == destinationTile.X && p1.Y == destinationTile.Y) && (enemy == null)) { return(MoveCheckResult.CanMove); } if ((p1.X == destinationTile.X && p1.Y == destinationTile.Y) && (enemy != null && enemy.IsWhite != this.IsWhite)) { return(MoveCheckResult.CanMove); } if (enemy != null && enemy != this) { return(MoveCheckResult.ContinueCheck); } } return(MoveCheckResult.ContinueCheck); }
public PlayerMove(Player player, BoardTile from, BoardTile to) { Player = player; From = from; To = to; }
/// <summary> /// Try to move the currently selected piece to the given board tile. /// </summary> /// <param name="tile"></param> /// <returns>Returns true if movement was successfull.</returns> private bool MoveSelectedPieceTo(BoardTile tile, Player player) { if (SelectedTile.CanMoveTo(this, tile)) { // Check if current move is a castling move if (SelectedTile.OccupyingPiece != null && SelectedTile.OccupyingPiece.GetType() == typeof(King) && tile.OccupyingPiece != null && tile.OccupyingPiece.GetType() == typeof(Rook) && tile.OccupyingPiece.IsWhite == SelectedTile.OccupyingPiece.IsWhite) { var rookDestination = ((Rook)tile.OccupyingPiece).GetCastleLocation(this, tile); rookDestination.OccupyingPiece = tile.OccupyingPiece; tile.OccupyingPiece = null; tile = ((King)SelectedTile.OccupyingPiece).GetCastleLocation(this, SelectedTile, tile); } currentRountData.AddMove(new PlayerMove(player, SelectedTile, tile)); latestTiles.Clear(); latestTiles.Add(tile); latestTiles.Add(SelectedTile); if (tile.OccupyingPiece == null && SelectedTile.OccupyingPiece.GetType() != typeof(Pawn)) { movesMadeWithoutProgress++; } else { movesMadeWithoutProgress = 0; } tile.OccupyingPiece = SelectedTile.OccupyingPiece; SelectedTile.OccupyingPiece.PostMovementEvent(tile); SelectedTile.OccupyingPiece = null; if (IsCheckmate(player)) { OnGameFinished?.Invoke(this, new GameFinishedEvent(player, GameFinishedReason.CheckMate)); } else if (!EnoughMaterialOnBoard()) { OnGameFinished?.Invoke(this, new GameFinishedEvent(null, GameFinishedReason.InsufficientMaterial)); } else if (IsStalemate(player.IsWhite)) { OnGameFinished?.Invoke(this, new GameFinishedEvent(null, GameFinishedReason.Stalemate)); } else if (movesMadeWithoutProgress >= 75) { OnGameFinished?.Invoke(this, new GameFinishedEvent(null, GameFinishedReason.Move75)); } if (currentRountData.IsDone) { OnRoundFinished?.Invoke(this, currentRountData); currentRountData = new RoundFinishedEvent(currentRountData.Round + 1); } return(true); } return(false); }
public abstract bool CanMoveTo(ChessBoard board, BoardTile currentTile, BoardTile destinationTile);
public virtual void PostMovementEvent(BoardTile tile) { }