public BoardSquare(int row, int col, int width, int height) { this.row = row; this.col = col; this.Location = new Point(col * width, row * height); this.Size = new Size(width, height); squareColor = (row + col) % 2 == 0 ? ChessColors.Black : ChessColors.White; this.BackColor = squareColor == ChessColors.Black ? Color.Black : Color.White; this.SizeMode = PictureBoxSizeMode.CenterImage; chessPiece = null; }
protected abstract void OnMoving(ChessPiece SelectedPiece, Position MoveTo);
protected abstract void OnPick(ChessPiece SelectedPiece);
protected abstract void OnError(ChessPiece SelectedPiece, string message);
//Moves a piece from one location to another on the chess board public static void Move(int oldX, int oldY, int newX, int newY, ChessPiece[,] chessBoard) { //You can't move a piece if it doesn't exist if (chessBoard[oldX, oldY].Type() != chessPieces.CLEAR) { chessColour pieceColor = chessBoard[oldX, oldY].Colour; //Special cases for pawns if (chessBoard[oldX, oldY].Type() == chessPieces.PAWN) { //We must remove the enemy pawn killed via enPassent if applicable if (isEnPassant(oldX, oldY, newX, newY, chessBoard)) { chessBoard[newX, oldY] = new ChessPiece(newX, newY - 1); } //When a white pawn moves to the bottom (8) or a black pawn moves to the top (0) it is promoted if ((pieceColor == chessColour.BLACK && newY == MIN_CHESS_VALUE) || (pieceColor == chessColour.WHITE && newY == MAX_CHESS_VALUE)) { //replace the pawn with a queen King oldKing = chessBoard[oldX, oldY]._king; chessBoard[oldX, oldY] = new Queen(oldX, oldY); chessBoard[oldX, oldY].Colour = pieceColor; chessBoard[oldX, oldY]._king = oldKing; } //Whenever a pawn moves two spaces, we record this for processing later if (Math.Abs(newY - oldY) > 1) { ChessPiece.DblMovePawn = (Pawn)chessBoard[oldX, oldY]; } else { //if the pawn didn't move two spaces, then clear DblMove ChessPiece.DblMovePawn = null; } } //Special case - If castling, we move the pieces to a different location if (isCastling(chessBoard[oldX, oldY], chessBoard[newX, newY], chessBoard)) { //Find the king and rook ChessPiece king; ChessPiece rook; if (chessBoard[oldX, oldY].Type() == chessPieces.KING) { king = chessBoard[oldX, oldY]; rook = chessBoard[newX, newY]; } else { rook = chessBoard[oldX, oldY]; king = chessBoard[newX, newY]; } //Move the king two spaces X towards the rook //Move the rook next to the king, on the opposite side if (king.PositionX < rook.PositionX) { king.PositionX = king.PositionX + 2; rook.PositionX = king.PositionX - 1; } else { king.PositionX = king.PositionX - 2; rook.PositionX = king.PositionX + 1; } //Update positions on chessboard - put pieces in new spots, clear old spots chessBoard[king.PositionX, king.PositionY] = king; chessBoard[rook.PositionX, rook.PositionY] = rook; chessBoard[oldX, oldY] = new ChessPiece(oldX, oldY); chessBoard[newX, newY] = new ChessPiece(newX, newY); //Once moved, the king and rook can never castle again king.CanCastle = false; rook.CanCastle = false; } else // regular move takes place { //move the piece to the new location chessBoard[newX, newY] = chessBoard[oldX, oldY]; //record the new location in the piece chessBoard[newX, newY].PositionX = newX; chessBoard[newX, newY].PositionY = newY; //replace the piece you moved chessBoard[oldX, oldY] = new ChessPiece(oldX, oldY); } //If the king or roook have moved, they can never castle again if (chessBoard[newX, newY].Type() == chessPieces.KING || chessBoard[newX, newY].Type() == chessPieces.ROOK) { chessBoard[newX, newY].CanCastle = false; } } }
public bool isOccupied(ChessPiece.PieceType? type = null, Chess.Game.Player? player = null) { return Piece != null && (type == null || Piece.GetType() == type) && (player == null || Piece.GetPlayer() == player); }
/// <summary> /// Calculate the actual actions available for a Chess Piece at a set of coordinates. /// </summary> /// <param name="x">The number of squares right of the bottom left square</param> /// <param name="y">The number of squares above the bottom left square</param> /// <param name="ignoreCheck">Do not check for threats to the king</param> /// <param name="attackActions">Calculate attacks</param> /// <param name="moveActions">Calculate movement</param> /// <param name="boardArray">An optional substitute board</param> /// <returns>A list of points that can be moved to</returns> public IEnumerable <Point> PieceActions(int x, int y, bool ignoreCheck = false, bool attackActions = true, bool moveActions = true, ChessPiece[,] boardArray = null) { if (boardArray == null) { boardArray = this.boardArray; } bool[,] legalActions = new bool[boardArray.GetLength(0), boardArray.GetLength(1)]; List <Point> availableActions = new List <Point>(); ChessPiece movingPeice = boardArray[x, y]; if (attackActions) { foreach (Point[] direction in movingPeice.AvailableAttacks) { foreach (Point attackPoint in direction) { Point adjustedPoint = new Point(attackPoint.x + x, attackPoint.y + y); if (ValidatePoint(adjustedPoint)) { if (boardArray[adjustedPoint.x, adjustedPoint.y] != null && boardArray[adjustedPoint.x, adjustedPoint.y].Player == movingPeice.Player) { break; } if (boardArray[adjustedPoint.x, adjustedPoint.y] != null) { AddMove(availableActions, new Point(x, y), adjustedPoint, ignoreCheck); break; } } } } } if (moveActions) { foreach (Point[] direction in movingPeice.AvailableMoves) { foreach (Point movePoint in direction) { Point adjustedPoint = new Point(movePoint.x + x, movePoint.y + y); if (ValidatePoint(adjustedPoint)) { if (boardArray[adjustedPoint.x, adjustedPoint.y] != null) { break; } AddMove(availableActions, new Point(x, y), adjustedPoint, ignoreCheck); } } } } if (movingPeice is King && ((King)movingPeice).CanCastle) { int rookX = 0; if (boardArray[rookX, y] is Rook && ((Rook)boardArray[rookX, y]).CanCastle) { bool missedCondition = false; foreach (int rangeX in Enumerable.Range(rookX + 1, Math.Abs(rookX - x) - 1)) { if (boardArray[rangeX, y] != null) { missedCondition = true; } // TODO: Validate that the king won't move through check } // TODO: Validate that king isn't currently in check missedCondition = missedCondition || KingInCheck(movingPeice.Player); if (!missedCondition) { AddMove(availableActions, new Point(x, y), new Point(x - 2, y), ignoreCheck); } } rookX = COLUMNS - 1; if (boardArray[rookX, y] is Rook && ((Rook)boardArray[rookX, y]).CanCastle) { bool missedCondition = false; foreach (int rangeX in Enumerable.Range(x + 1, Math.Abs(rookX - x) - 1)) { if (boardArray[rangeX, y] != null) { missedCondition = true; } // TODO: Validate that the king won't move through check } // TODO: Validate that king isn't currently in check missedCondition = missedCondition || KingInCheck(movingPeice.Player); if (!missedCondition) { AddMove(availableActions, new Point(x, y), new Point(x + 2, y), ignoreCheck); } } } if (movingPeice is Pawn) { Pawn pawn = (Pawn)movingPeice; int flipDirection = 1; if (pawn.Player == 1) { flipDirection = -1; } if (pawn.CanEnPassantLeft) { Point attackPoint; attackPoint = ChessPiece.GetDiagnalMovementArray(1, DiagnalDirection.FORWARD_LEFT)[0]; attackPoint.y *= flipDirection; attackPoint.y += y; attackPoint.x += x; if (ValidatePoint(attackPoint)) { AddMove(availableActions, new Point(x, y), attackPoint, ignoreCheck); } } if (pawn.CanEnPassantRight) { Point attackPoint; attackPoint = ChessPiece.GetDiagnalMovementArray(1, DiagnalDirection.FORWARD_RIGHT)[0]; attackPoint.y *= flipDirection; attackPoint.y += y; attackPoint.x += x; if (ValidatePoint(attackPoint)) { AddMove(availableActions, new Point(x, y), attackPoint, ignoreCheck); } } } return(availableActions); }
public bool PossibleMove(Position SelectedPiece, Position PositionToMove) { ChessPiece currentPiece = BoardMatrix[SelectedPiece.Row, SelectedPiece.Column]; OnPick(currentPiece); int row = PositionToMove.Row; int column = PositionToMove.Column; //If piece is not blank space if (currentPiece == null) { OnError(null, "Select a piece"); return(false); // If piece belongs to player } else if (!DoesPieceBelongToPlayer(currentPiece)) { OnError(currentPiece, "Piece doesn't belong to this player"); return(false); } else { // TODO: Collision Position[] possibleMoves = PossibleMovesWithCollission(currentPiece); if (possibleMoves.Any(move => move.Row == row && move.Column == column)) { BoardMatrix.UpdateBoard(currentPiece, currentPiece.Position, PositionToMove, OnMoving, OnDrop); TogglePlayers(); OnPlayersToggled(); ChessPiece DetectsKing; switch (CurrentPlayer) { case ChessColor.White: DetectsKing = CheckDetection(BoardMatrix.WhiteKing); break; default: DetectsKing = CheckDetection(BoardMatrix.BlackKing); break; } if (DetectsKing != null && DetectsKing.Color == CurrentPlayer) { BoardMatrix.UpdateBoard(currentPiece, currentPiece.Position, SelectedPiece, OnMoving, OnDrop); return(false); } ; return(true); } else { OnError(currentPiece, "You can't move there"); return(false); } } }
//Event driven logic - user clicking on a chess piece drives the whole program private void dgv_chessBoard_CellContentClick(object sender, DataGridViewCellEventArgs e) { //We can only move a piece if we've selected one to move if (selectionMade) { ChessPiece selectedPiece = chessBoard[selectedX, selectedY]; chessColour selectedColour = chessBoard[selectedX, selectedY].Colour; // check that the move is legal for the piece selected, // and that the piece is the correct colour to move, if (currentGame.Turn == selectedColour && selectedPiece.isValidMove(e.ColumnIndex, e.RowIndex, chessBoard)) { //If we're playing single player with no networking we can move black or white pieces if (!isNetworked) { movePiece(selectedX, selectedY, e.ColumnIndex, e.RowIndex); } else if (thisPlayerColour == currentGame.Turn) //If we're playing a networked game, we can only move our own pieces { movePiece(selectedX, selectedY, e.ColumnIndex, e.RowIndex); // Use an event to call the networkingWindow to record and send the move to other player if (isNetworked) { //"MOVE|oldCol,oldRow|newCol,newRow\n" chessMoveArgs args = new chessMoveArgs(); args.oldX = selectedX; args.oldY = selectedY; args.newX = e.ColumnIndex; args.newY = e.RowIndex; OnChessMoveEvent(args); } } } //if the move wasn't valid, clear selection for next use selectionMade = false; //report selection to other player if (isNetworked) { selectionMadeArgs args = new selectionMadeArgs(); args.selection = selectionMade; args.intX = selectedX; args.intY = selectedY; OnSelectionMadeEvent(args); } } else { selectionMade = true; //saves the selection selectedX = e.ColumnIndex; selectedY = e.RowIndex; //report selection to other player if (isNetworked) { selectionMadeArgs args = new selectionMadeArgs(); args.selection = selectionMade; args.intX = selectedX; args.intY = selectedY; OnSelectionMadeEvent(args); } } updateSelectionSquare(); //updates the gameboard if a square has been selected/released }
private void PlaceNewPiece(char column, int row, ChessPiece piece) { _board.PlacePiece(piece, new ChessPosition(column, row).ToPosition()); _piecesOnTheBoard.Add(piece); }
protected bool IsThereOpponentPiece(Position position) { ChessPiece p = (ChessPiece)Board.Piece(position); return(p != null && p.Color != Color); }
private void CellClicked(MouseButtonEventArgs m) { int ym = (int)(m.GetPosition(this).X / 64); int xm = (int)(m.GetPosition(this).Y / 64); for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if ((i + j) % 2 == 0) { borderControls[i, j].Background = new SolidColorBrush(Color.FromRgb(100, 100, 100)); } else { borderControls[i, j].Background = new SolidColorBrush(Color.FromRgb(220, 220, 220)); } } } if (avaliableMoves != null) { foreach (ChessPoint cpt in avaliableMoves) { if (ym == cpt.X && xm == cpt.Y) { selectedPiece.position = cpt; DrawChessBoard(); isBlackMove = !isBlackMove; break; } } } if (avaliableHits != null) { foreach (ChessPoint cpt in avaliableHits) { if (ym == cpt.X && xm == cpt.Y) { ChessPiece pieceToRemove = null; foreach (ChessPiece cpth in chessPieces) { if (cpth.position.X == cpt.X && cpth.position.Y == cpt.Y) { pieceToRemove = cpth; } } chessPieces.Remove(pieceToRemove); selectedPiece.position = cpt; DrawChessBoard(); isBlackMove = !isBlackMove; break; } } } bool isValid = false; selectedPiece = null; avaliableMoves = null; avaliableHits = null; foreach (ChessPiece cp in chessPieces) { if (ym == cp.position.X && xm == cp.position.Y && cp.isBlack == isBlackMove) { isValid = true; selectedPiece = cp; } } if (isValid) { selectedPoint = new ChessPoint(ym, xm); Console.WriteLine("Selected : " + selectedPiece.position.X + " , " + selectedPiece.position.Y); borderControls[xm, ym].Background = Brushes.LimeGreen; avaliableMoves = selectedPiece.getPossibleMoves(chessPieces); foreach (ChessPoint cpt in avaliableMoves) { borderControls[cpt.Y, cpt.X].Background = Brushes.SkyBlue; } avaliableHits = selectedPiece.getPossibleHits(chessPieces); Console.WriteLine("Hits : " + avaliableHits.Count); foreach (ChessPoint cpt in avaliableHits) { borderControls[cpt.Y, cpt.X].Background = Brushes.Red; } } }
/// <summary> /// Takes piece and returns it's cell color /// </summary> /// <param name="piece">ChessPiece parameter to process</param> /// <returns>Black or White cell</returns> public string GetColor(ChessPiece piece) { return(piece.HorizontalCoordinate % 2 == piece.VerticalCoordinate % 2 ? "Black" : "white"); }
/// <summary> /// This constructor takes 2 ChessPiece objects as parameters /// </summary> /// <param name="first">First Piece</param> /// <param name="second">Second Piece</param> public PositionAnalyzer(ChessPiece first, ChessPiece second) { Pieces.Add(first); Pieces.Add(second); }
protected abstract void OnDrop(ChessPiece SelectedPiece);
protected abstract void OnCheck(King King, ChessPiece PieceWhichTriggersCheck);
public Tile(ChessPiece piece) { PieceOnTile = piece; }
/// <summary> /// Move a peice from one location on the board to another. /// </summary> /// <param name="from">The location of the piece that is moving.</param> /// <param name="to">The location to move to.</param> /// <returns>Returns true on success or false on failure.</returns> public bool ActionPiece(Point from, Point to, bool bypassValidaiton = false) { if (bypassValidaiton || PieceActions(from).Contains(to)) { ChessPiece movingPeice = boardArray[from.x, from.y]; if (movingPeice is Pawn) { Pawn pawn = (Pawn)movingPeice; // If this was a double jump, check enpassant if (Math.Abs(from.y - to.y) == 2) { int adjasentX = to.x - 1; if (adjasentX > -1 && boardArray[adjasentX, to.y] != null && boardArray[adjasentX, to.y].Player != movingPeice.Player && boardArray[adjasentX, to.y] is Pawn) { if (!bypassValidaiton) { ((Pawn)boardArray[adjasentX, to.y]).CanEnPassantRight = true; } } adjasentX += 2; if (adjasentX < COLUMNS && boardArray[adjasentX, to.y] != null && boardArray[adjasentX, to.y].Player != movingPeice.Player && boardArray[adjasentX, to.y] is Pawn) { if (!bypassValidaiton) { ((Pawn)boardArray[adjasentX, to.y]).CanEnPassantLeft = true; } } } // If this was a sideways jump to null, it was enpassant! if (from.x != to.x && boardArray[to.x, to.y] == null) { boardArray[to.x, from.y] = null; } if (!bypassValidaiton) // Pawns can't double jump after they move. { pawn.CanDoubleJump = false; } } if (movingPeice is CastlePiece) { CastlePiece rookOrKing = (CastlePiece)movingPeice; if (!bypassValidaiton) // Castling can't be done after moving { rookOrKing.CanCastle = false; } } if (movingPeice is King) { King king = (King)movingPeice; if (from.x - to.x == 2) { // Move rook for Queenside castle boardArray[to.x + 1, from.y] = boardArray[0, from.y]; boardArray[0, from.y] = null; } if (from.x - to.x == -2) { // Move rook for Kingside castle boardArray[to.x - 1, from.y] = boardArray[COLUMNS - 1, from.y]; boardArray[COLUMNS - 1, from.y] = null; } } movingPeice.CalculateMoves(); boardArray[from.x, from.y] = null; boardArray[to.x, to.y] = movingPeice; return(true); } return(false); }
public void moveFromeSquare() { piece = null; }
public ChessBoard(Player p1, Player p2) { //all white pieces whiteKing = new King(p1, 3, 0); ChessPiece whiteQueen = new Queen(p1, 4, 0); ChessPiece whiteRunner1 = new Runner(p1, 2, 0); ChessPiece whiteRunner2 = new Runner(p1, 5, 0); ChessPiece whiteHorse1 = new Horse(p1, 1, 0); ChessPiece whiteHorse2 = new Horse(p1, 6, 0); ChessPiece whiteTower1 = new Tower(p1, 0, 0); ChessPiece whiteTower2 = new Tower(p1, 7, 0); ChessPiece whiteFarmer1 = new Farmer(p1, 0, 1); ChessPiece whiteFarmer2 = new Farmer(p1, 1, 1); ChessPiece whiteFarmer3 = new Farmer(p1, 2, 1); ChessPiece whiteFarmer4 = new Farmer(p1, 3, 1); ChessPiece whiteFarmer5 = new Farmer(p1, 4, 1); ChessPiece whiteFarmer6 = new Farmer(p1, 5, 1); ChessPiece whiteFarmer7 = new Farmer(p1, 6, 1); ChessPiece whiteFarmer8 = new Farmer(p1, 7, 1); //all black pieces blackKing = new King(p2, 4, 7); ChessPiece blackQueen = new Queen(p2, 3, 7); ChessPiece blackRunner1 = new Runner(p2, 2, 7); ChessPiece blackRunner2 = new Runner(p2, 5, 7); ChessPiece blackHorse1 = new Horse(p2, 1, 7); ChessPiece blackHorse2 = new Horse(p2, 6, 7); ChessPiece blackTower1 = new Tower(p2, 0, 7); ChessPiece blackTower2 = new Tower(p2, 7, 7); ChessPiece blackFarmer1 = new Farmer(p2, 0, 6); ChessPiece blackFarmer2 = new Farmer(p2, 1, 6); ChessPiece blackFarmer3 = new Farmer(p2, 2, 6); ChessPiece blackFarmer4 = new Farmer(p2, 3, 6); ChessPiece blackFarmer5 = new Farmer(p2, 4, 6); ChessPiece blackFarmer6 = new Farmer(p2, 5, 6); ChessPiece blackFarmer7 = new Farmer(p2, 6, 6); ChessPiece blackFarmer8 = new Farmer(p2, 7, 6); //add all chesspieces to twodimensional array board[3, 0] = whiteKing; board[4, 0] = whiteQueen; board[2, 0] = whiteRunner1; board[5, 0] = whiteRunner2; board[1, 0] = whiteHorse1; board[6, 0] = whiteHorse2; board[0, 0] = whiteTower1; board[7, 0] = whiteTower2; board[0, 1] = whiteFarmer1; board[1, 1] = whiteFarmer2; board[2, 1] = whiteFarmer3; board[3, 1] = whiteFarmer4; board[4, 1] = whiteFarmer5; board[5, 1] = whiteFarmer6; board[6, 1] = whiteFarmer7; board[7, 1] = whiteFarmer8; board[4, 7] = blackKing; board[3, 7] = blackQueen; board[2, 7] = blackRunner1; board[5, 7] = blackRunner2; board[1, 7] = blackHorse1; board[6, 7] = blackHorse2; board[0, 7] = blackTower1; board[7, 7] = blackTower2; board[0, 6] = blackFarmer1; board[1, 6] = blackFarmer2; board[2, 6] = blackFarmer3; board[3, 6] = blackFarmer4; board[4, 6] = blackFarmer5; board[5, 6] = blackFarmer6; board[6, 6] = blackFarmer7; board[7, 6] = blackFarmer8; }
public LogicBoard() { BoardMatrix = new ChessPiece[8, 8]; Init(); }
private bool DoesPieceBelongToPlayer(ChessPiece piece) { return(piece.Color == CurrentPlayer); }
public bool IsMoveAllowed(ChessPiece piece, Point destination) { return(true); }