private void InitializePieces() { board = new Piece[8, 8]; whites = new MarkableButtonPanel(); blacks = new MarkableButtonPanel(); int pieceSize = Constants.PIESESIZE; int markSize = Constants.MARKED_PIESESIZE; for (int i = 0; i < 8; i++) { Piece temp = new Pawn(new Sprite2D(ContentService.Instance.Textures["WhitePawn"], new Rectangle(0, 0, pieceSize, pieceSize)), 6, i, ChessColor.White, this); temp.Center(grid[6, i].Bounds); board[6, i] = temp; } Piece x = new Rook(new Sprite2D(ContentService.Instance.Textures["WhiteRook"], new Rectangle(0, 0, pieceSize, pieceSize)), 7, 0, ChessColor.White, this); x.Center(grid[7, 0].Bounds); board[7, 0] = x; x = new Rook(new Sprite2D(ContentService.Instance.Textures["WhiteRook"], new Rectangle(0, 0, pieceSize, pieceSize)), 7, 7, ChessColor.White, this); x.Center(grid[7, 7].Bounds); board[7, 7] = x; x = new Knight(new Sprite2D(ContentService.Instance.Textures["WhiteKnight"], new Rectangle(0, 0, pieceSize, pieceSize)), 7, 1, ChessColor.White, this); x.Center(grid[7, 1].Bounds); board[7, 1] = x; x = new Knight(new Sprite2D(ContentService.Instance.Textures["WhiteKnight"], new Rectangle(0, 0, pieceSize, pieceSize)), 7, 6, ChessColor.White, this); x.Center(grid[7, 6].Bounds); board[7, 6] = x; x = new Bishop(new Sprite2D(ContentService.Instance.Textures["WhiteBishop"], new Rectangle(0, 0, pieceSize, pieceSize)), 7, 2, ChessColor.White, this); x.Center(grid[7, 2].Bounds); board[7, 2] = x; x = new Bishop(new Sprite2D(ContentService.Instance.Textures["WhiteBishop"], new Rectangle(0, 0, pieceSize, pieceSize)), 7, 5, ChessColor.White, this); x.Center(grid[7, 5].Bounds); board[7, 5] = x; x = new Queen(new Sprite2D(ContentService.Instance.Textures["WhiteQueen"], new Rectangle(0, 0, pieceSize, pieceSize)), 7, 3, ChessColor.White, this); x.Center(grid[7, 3].Bounds); board[7, 3] = x; x = new King(new Sprite2D(ContentService.Instance.Textures["WhiteKing"], new Rectangle(0, 0, pieceSize, pieceSize)), 7, 4, ChessColor.White, this); x.Center(grid[7, 4].Bounds); board[7, 4] = x; for (int i = 0; i < 8; i++) { Piece temp = new Pawn(new Sprite2D(ContentService.Instance.Textures["BlackPawn"], new Rectangle(0, 0, pieceSize, pieceSize)), 1, i, ChessColor.Black, this); temp.Center(grid[1, i].Bounds); board[1, i] = temp; } x = new Rook(new Sprite2D(ContentService.Instance.Textures["BlackRook"], new Rectangle(0, 0, pieceSize, pieceSize)), 0, 0, ChessColor.Black, this); x.Center(grid[0, 0].Bounds); board[0, 0] = x; x = new Rook(new Sprite2D(ContentService.Instance.Textures["BlackRook"], new Rectangle(0, 0, pieceSize, pieceSize)), 0, 7, ChessColor.Black, this); x.Center(grid[0, 7].Bounds); board[0, 7] = x; x = new Knight(new Sprite2D(ContentService.Instance.Textures["BlackKnight"], new Rectangle(0, 0, pieceSize, pieceSize)), 0, 1, ChessColor.Black, this); x.Center(grid[0, 1].Bounds); board[0, 1] = x; x = new Knight(new Sprite2D(ContentService.Instance.Textures["BlackKnight"], new Rectangle(0, 0, pieceSize, pieceSize)), 0, 6, ChessColor.Black, this); x.Center(grid[0, 6].Bounds); board[0, 6] = x; x = new Bishop(new Sprite2D(ContentService.Instance.Textures["BlackBishop"], new Rectangle(0, 0, pieceSize, pieceSize)), 0, 2, ChessColor.Black, this); x.Center(grid[0, 2].Bounds); board[0, 2] = x; x = new Bishop(new Sprite2D(ContentService.Instance.Textures["BlackBishop"], new Rectangle(0, 0, pieceSize, pieceSize)), 0, 5, ChessColor.Black, this); x.Center(grid[0, 5].Bounds); board[0, 5] = x; x = new Queen(new Sprite2D(ContentService.Instance.Textures["BlackQueen"], new Rectangle(0, 0, pieceSize, pieceSize)), 0, 3, ChessColor.Black, this); x.Center(grid[0, 3].Bounds); board[0, 3] = x; x = new King(new Sprite2D(ContentService.Instance.Textures["BlackKing"], new Rectangle(0, 0, pieceSize, pieceSize)), 0, 4, ChessColor.Black, this); x.Center(grid[0, 4].Bounds); board[0, 4] = x; for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (board[i, j] == null) { continue; } board[i, j].MarkAnimation = new ButtonAnimation(null, new Rectangle(board[i, j].Bounds.Location, new Point(markSize, markSize)), null, true); board[i, j].UnMarkAnimation = new ButtonAnimation(null, new Rectangle(board[i, j].Bounds.Location, new Point(pieceSize, pieceSize)), null, true); if (board[i, j].ChessColor == ChessColor.Black) { blacks.Add(board[i, j]); } else { whites.Add(board[i, j]); } } } for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (board[i, j] != null) { board[i, j].CalculateLegalMoves(); } } } }
public void Move(Piece p, int tr, int tc) { int r = p.Row; int c = p.Col; LastPieceMoved = p; if (!IsEmpty(tr, tc)) { if (p.ChessColor == ChessColor.Black) { whites.Remove(board[tr, tc]); } else { blacks.Remove(board[tr, tc]); } } board[tr, tc] = board[r, c]; board[r, c] = null; board[tr, tc].Col = tc; board[tr, tc].Row = tr; if (board[tr, tc].ChessPiece == ChessPiece.Pawn && (board[tr, tc].Row == 0 || board[tr, tc].Row == 7)) { int col = tc; int row = tr; if (p.ChessColor == ChessColor.Black) { Queen piece = new Queen( new Sprite2D( ContentService.Instance.Textures["BlackQueen"], new Rectangle(col * Constants.TILESIZE, row * Constants.TILESIZE, Constants.PIESESIZE, Constants.PIESESIZE) ), row, col, p.ChessColor, this); blacks.Add(piece); blacks.Remove(board[row, col]); board[row, col] = piece; piece.MarkAnimation = new ButtonAnimation(null, new Rectangle(board[row, col].Bounds.Location, new Point(Constants.MARKED_PIESESIZE, Constants.MARKED_PIESESIZE)), null, true); piece.UnMarkAnimation = new ButtonAnimation(null, new Rectangle(board[row, col].Bounds.Location, new Point(Constants.PIESESIZE, Constants.PIESESIZE)), null, true); } else { Queen piece = new Queen( new Sprite2D( ContentService.Instance.Textures["WhiteQueen"], new Rectangle(col * Constants.TILESIZE, row * Constants.TILESIZE, Constants.PIESESIZE, Constants.PIESESIZE) ), row, col, p.ChessColor, this); whites.Add(piece); whites.Remove(board[row, col]); board[row, col] = piece; piece.MarkAnimation = new ButtonAnimation(null, new Rectangle(board[row, col].Bounds.Location, new Point(Constants.MARKED_PIESESIZE, Constants.MARKED_PIESESIZE)), null, true); piece.UnMarkAnimation = new ButtonAnimation(null, new Rectangle(board[row, col].Bounds.Location, new Point(Constants.PIESESIZE, Constants.PIESESIZE)), null, true); } } for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (board[i, j] != null) { board[i, j].CalculateLegalMoves(); } } } moveMade = true; }