public Set(Color color) { int rank; this.color = color; pieces = new Piece[16]; rank = (color == Color.Black) ? 8 : 1; pieces[0] = rookA = new Rook(color, new Position(rank, 'a')); pieces[1] = knightA = new Knight(color, new Position(rank, 'b')); pieces[2] = bishopA = new Bishop(color, new Position(rank, 'c')); pieces[3] = king = new King(color, new Position(rank, 'd')); pieces[4] = queen = new Queen(color, new Position(rank, 'e')); pieces[5] = bishopB = new Bishop(color, new Position(rank, 'f')); pieces[6] = knightB = new Knight(color, new Position(rank, 'g')); pieces[7] = rookB = new Rook(color, new Position(rank, 'h')); rank = (color == Color.Black) ? 7 : 2; pieces[8] = pawnA = new Pawn(color, new Position(rank, 'a')); pieces[9] = pawnB = new Pawn(color, new Position(rank, 'b')); pieces[10] = pawnC = new Pawn(color, new Position(rank, 'c')); pieces[11] = pawnD = new Pawn(color, new Position(rank, 'd')); pieces[12] = pawnE = new Pawn(color, new Position(rank, 'e')); pieces[13] = pawnF = new Pawn(color, new Position(rank, 'f')); pieces[14] = pawnG = new Pawn(color, new Position(rank, 'g')); pieces[15] = pawnH = new Pawn(color, new Position(rank, 'h')); }
public List<Tuple<int, int>> getValidMoves(Piece currentPiece, Gameboard gameboard, bool doRecurse = true) { this.copyOfCurrentPiece = new Piece(currentPiece); this.gameboard = new Gameboard(gameboard); switch (currentPiece.type) { case (int)type.rock: // tower Rock rock = new Rock(); this.validDestinations = rock.Rules(currentPiece, gameboard); if (doRecurse) cleanUp(); return validDestinations; case (int)type.knight: // horsie Knight knight = new Knight(); this.validDestinations = knight.Rules(currentPiece, gameboard); if (doRecurse) cleanUp(); return validDestinations; case (int)type.bishop: // springare Bishop bishop = new Bishop(); this.validDestinations = bishop.Rules(currentPiece, gameboard); if (doRecurse) cleanUp(); return validDestinations; case (int)type.queen: //los quuenos Queen queen = new Queen(); this.validDestinations = queen.Rules(currentPiece, gameboard); if (doRecurse) cleanUp(); return validDestinations; case (int)type.king: // los kingos King king = new King(); this.validDestinations = king.Rules(currentPiece, gameboard); if (doRecurse) cleanUp(); return validDestinations; case (int)type.pawn: // los farmeros Pawn pawn = new Pawn(); this.validDestinations = pawn.Rules(currentPiece, gameboard); if (doRecurse) cleanUp(); return validDestinations; } return new List<Tuple<int,int>>(); }
/* * This constructor is used in the clone method */ public List(ArrayList l) { list = new ArrayList(); foreach (Piece p in l) { Position pos = new Position(p.getPosition().getRow(), p.getPosition().getColumn()); Color color = p.getColor(); Image image = p.getImage(); Piece piece = null; if (p is Bishop) { piece = new Bishop(pos, color); } else if (p is Knight) { piece = new Knight(pos, color); } else if (p is Pawn) { piece = new Pawn(pos, color); } else if (p is Queen) { piece = new Queen(pos, color); } else if (p is Rook) { piece = new Rook(pos, color); } else if (p is King) { piece = new King(pos, color); } list.Add(piece); } }
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 void PerformLegalMove(Move move) { move.PreviousMovesSinceLastCaptureOrPawnMove = MovesSinceLastCaptureOrPawnMove; MovesSinceLastCaptureOrPawnMove++; var fromSquare = move.FromSquare; MoveCount++; var piece = move.Piece; piece.MoveCount++; fromSquare.Piece = null; //use from square to remove piece var playColor = piece.Color; var capture = move.Capture; if (capture != null) { OtherPlayer.Material -= capture.Value; OtherPlayer.Pieces.Remove(capture); MovesSinceLastCaptureOrPawnMove = 0; } move.ToSquare.SetPiece(piece); if (move.IsPromotion) { piece.Square = null; move.PromotedPawn = (Pawn)piece; var queen = new Queen(playColor); AddPiece(move.ToSquare.File, move.ToSquare.Rank, queen); move.Piece = queen; //todo: test without it. CurrentPlayer.Material += 800; //add queen, remove pawn CurrentPlayer.Pieces.Remove(move.PromotedPawn); } else if (move.IsCastling) { Castle(move); } else if (move.IsEnpassant) { move.CapturedFrom.Piece = null; move.Capture.Square = null; } CurrentPlayer.Moves.Push(move); //If it is found later that this is an illegal move it is removed in the undo - function move.WhiteWasChecked = WhitePlayer.IsChecked; move.BlackWasChecked = BlackPlayer.IsChecked; if (move.ScoreInfo.HasFlag(ScoreInfo.InsufficientMaterial)) { Ended = true; } if (move.ScoreInfo.HasFlag(ScoreInfo.DrawByRepetion)) { Ended = true; } if (move.PreviousMovesSinceLastCaptureOrPawnMove > 50) { Ended = true; } move.PreviousEnPassant = EnPassantFile; EnPassantFile = null; if (piece is Pawn) { MovesSinceLastCaptureOrPawnMove = 0; if (piece.MoveCount == 1) { var dist = move.ToSquare.Rank - fromSquare.Rank; if (Math.Abs(dist) == 2) { EnPassantFile = fromSquare.File; } } } OtherPlayer.IsChecked = move.IsCheck; CurrentPlayer.IsChecked = false; SwitchPlayer(); move.PreviousHash = Hash; PositionsDatabase.Instance.UpdateHash(move); Hash ^= move.Hash; }
public void MakeMove(Position origin, Position destiny) { Piece takenPiece = ChessMove(origin, destiny); if (IsInCheck(CurrentPlayer)) { UndoChessMove(origin, destiny, takenPiece); throw new BoardExceptions("You can not put your self in check"); } Piece movedPiece = Board.GetPiece(destiny); //Special move: Pawn Promotion if (movedPiece is Pawn) { if ((movedPiece.Color == Color.White && destiny.Line == 0) || (movedPiece.Color == Color.Black && destiny.Line == 7)) { Board.RemovePiece(destiny); GamePieces.Remove(movedPiece); Console.WriteLine("Type the kind of piece you want to promote your pawn to"); Console.Write("(QUEEN / ROOK / KNIGHT / BISHOP): "); string playerChoice = Console.ReadLine(); if (!ScreenController.CheckPlayerChoice(playerChoice)) { throw new BoardExceptions("Not valid option"); } playerChoice = playerChoice.ToLower(); switch (playerChoice) { case "queen": { Piece newPiece = new Queen(movedPiece.Color, Board); Board.AddressPiece(newPiece, destiny); GamePieces.Add(newPiece); break; } case "rook": { Piece newPiece = new Rook(movedPiece.Color, Board); Board.AddressPiece(newPiece, destiny); GamePieces.Add(newPiece); break; } case "knight": { Piece newPiece = new Knight(movedPiece.Color, Board); Board.AddressPiece(newPiece, destiny); GamePieces.Add(newPiece); break; } case "bishop": { Piece newPiece = new Bishop(movedPiece.Color, Board); Board.AddressPiece(newPiece, destiny); GamePieces.Add(newPiece); break; } } } } //EndGame Pawn Promotion if (IsInCheck(EnemyIs(CurrentPlayer))) { PlayerInCheck = true; } else { PlayerInCheck = false; } if (IsInCheckMate(EnemyIs(CurrentPlayer))) { EndGame = true; } else { Turn++; MudaJogador(); } //Special move: en passant if (movedPiece is Pawn && (destiny.Line == origin.Line + 2 || destiny.Line == origin.Line - 2)) { VulnerableToEnPassant = movedPiece; } else { VulnerableToEnPassant = null; } }
/** * StartGame() * * Remplit le plateau et lance le premier tour * * @author Axel Floquet-Trillot * */ public void StartGame() { //Remplissage du plateau for (int i = 0; i < GameBoard.GetLength(0); i++) { switch (i) { case 0: for (int j = 0; j < GameBoard.GetLength(1); j++) { switch (j) { case 0: case 7: GameBoard[i, j] = new Rook(Piece.Color.Black); break; case 1: case 6: GameBoard[i, j] = new Knight(Piece.Color.Black); break; case 2: case 5: GameBoard[i, j] = new Bishop(Piece.Color.Black); break; case 3: GameBoard[i, j] = new Queen(Piece.Color.Black); break; case 4: GameBoard[i, j] = new King(Piece.Color.Black); break; } } break; case 1: for (int j = 0; j < GameBoard.GetLength(1); j++) { GameBoard[i, j] = new Pawn(Piece.Color.Black); } break; case 2: case 3: case 4: case 5: for (int j = 0; j < GameBoard.GetLength(1); j++) { GameBoard[i, j] = null; } break; case 6: for (int j = 0; j < GameBoard.GetLength(1); j++) { GameBoard[i, j] = new Pawn(Piece.Color.White); } break; case 7: for (int j = 0; j < GameBoard.GetLength(1); j++) { switch (j) { case 0: case 7: GameBoard[i, j] = new Rook(Piece.Color.White); break; case 1: case 6: GameBoard[i, j] = new Knight(Piece.Color.White); break; case 2: case 5: GameBoard[i, j] = new Bishop(Piece.Color.White); break; case 3: GameBoard[i, j] = new Queen(Piece.Color.White); break; case 4: GameBoard[i, j] = new King(Piece.Color.White); break; } } break; } } PlayTurn(); }
public void Run(GameBoard Game, Coordinate Current, Coordinate Next, Check Checker, Empty Emptyspace) { string typecheck; typecheck = Game.Chessboard[Current.X, Current.Y].GetType().ToString(); switch (typecheck) { case "Chess.Pawn": Pawn Pawnmover = new Pawn("WHITE"); Pawnmover = (Pawn)Game.Chessboard[Current.X, Current.Y]; if (Pawnmover.isMovable(Game, Next)) { Game.Chessboard[Current.X, Current.Y].Move(Game, Current, Next, Emptyspace); if (!(Checker.Checktest(Game, Next))) { Game.LastmoveReverser(Game, Current, Next, Emptyspace); } else { anyChanges = true; } } break; case "Chess.Bishop": Bishop Bishopmover = new Bishop("a"); Bishopmover = (Bishop)Game.Chessboard[Current.X, Current.Y]; if (Bishopmover.isMovable(Game, Next)) { Game.Chessboard[Current.X, Current.Y].Move(Game, Current, Next, Emptyspace); if (!(Checker.Checktest(Game, Next))) { Game.LastmoveReverser(Game, Current, Next, Emptyspace); } else { anyChanges = true; } } break; case "Chess.Queen": Queen Queenmover = new Queen("a"); Queenmover = (Queen)Game.Chessboard[Current.X, Current.Y]; if (Queenmover.isMovable(Game, Next)) { Game.Chessboard[Current.X, Current.Y].Move(Game, Current, Next, Emptyspace); if (!(Checker.Checktest(Game, Next))) { Game.LastmoveReverser(Game, Current, Next, Emptyspace); } else { anyChanges = true; } } break; case "Chess.Knight": Knight Knightmover = new Knight("a"); Knightmover = (Knight)Game.Chessboard[Current.X, Current.Y]; if (Knightmover.isMovable(Game, Next)) { Game.Chessboard[Current.X, Current.Y].Move(Game, Current, Next, Emptyspace); if (!(Checker.Checktest(Game, Next))) { Game.LastmoveReverser(Game, Current, Next, Emptyspace); } else { anyChanges = true; } } break; case "Chess.Castle": Castle Castlemover = new Castle("a"); Castlemover = (Castle)Game.Chessboard[Current.X, Current.Y]; if (Castlemover.isMovable(Game, Next)) { Game.Chessboard[Current.X, Current.Y].Move(Game, Current, Next, Emptyspace); if (!(Checker.Checktest(Game, Next))) { Game.LastmoveReverser(Game, Current, Next, Emptyspace); } else { anyChanges = true; } } break; case "Chess.King": King Kingmover = new King("a"); Kingmover = (King)Game.Chessboard[Current.X, Current.Y]; if (Kingmover.isMovable(Game, Next)) { Game.Chessboard[Current.X, Current.Y].Move(Game, Current, Next, Emptyspace); if (!(Checker.Checktest(Game, Next))) { Game.LastmoveReverser(Game, Current, Next, Emptyspace); } else { anyChanges = true; } } else if (Kingmover.Rooktest1(Game, Current, Next, Emptyspace) && Checker.Checktest(Game, Current)) { Game.Chessboard[Current.X, Current.Y].Rookmover1(Game, Current, Next, Emptyspace); anyChanges = true; } else if (Kingmover.Rooktest2(Game, Current, Next, Emptyspace) && Checker.Checktest(Game, Current)) { Game.Chessboard[Current.X, Current.Y].Rookmover2(Game, Current, Next, Emptyspace); anyChanges = true; } break; } if (CommonProperties.enPassantfinder + 1 > Game.White.Count || CommonProperties.enPassantfinder + 1 > Game.Black.Count) { CommonProperties.enPassantfinder = 0; } if (Game.White[CommonProperties.enPassantfinder].enPassantchecker == 2) { Game.White[CommonProperties.enPassantfinder].enPassantchecker -= 1; } else if (Game.White[CommonProperties.enPassantfinder].enPassantchecker == 1) { Game.White[CommonProperties.enPassantfinder].enPassantchecker -= 1; } if (Game.Black[CommonProperties.enPassantfinder].enPassantchecker == 2) { Game.Black[CommonProperties.enPassantfinder].enPassantchecker -= 1; } else if (Game.Black[CommonProperties.enPassantfinder].enPassantchecker == 1) { Game.Black[CommonProperties.enPassantfinder].enPassantchecker -= 1; } }
public bool MatetestforWhite(GameBoard Game, Empty Emptyspace, Check Checker) { Coordinate CurrentCoord = new Coordinate(); Coordinate NextCoord = new Coordinate(); for (int i = 0; i < Game.White.Count; i++) { string typecheck = Game.White[i].GetType().ToString(); CurrentCoord.X = Game.White[i].Current.X; CurrentCoord.Y = Game.White[i].Current.Y; for (byte j = 0; j < 8; j++) { NextCoord.X = j; for (byte k = 0; k < 8; k++) { NextCoord.Y = k; switch (typecheck) { case "Chess.Pawn": Pawn Pawnmover = new Pawn("WHITE"); Pawnmover = (Pawn)Game.Chessboard[CurrentCoord.X, CurrentCoord.Y]; if (Pawnmover.isMovable(Game, NextCoord)) { Game.Chessboard[CurrentCoord.X, CurrentCoord.Y].Move(Game, CurrentCoord, NextCoord, Emptyspace); if (!(Checker.Checktest(Game, NextCoord))) { Game.LastmoveReverser(Game, CurrentCoord, NextCoord, Emptyspace); } else { Game.LastmoveReverser(Game, CurrentCoord, NextCoord, Emptyspace); return(true); } } break; case "Chess.Bishop": Bishop Bishopmover = new Bishop("a"); Bishopmover = (Bishop)Game.Chessboard[CurrentCoord.X, CurrentCoord.Y]; if (Bishopmover.isMovable(Game, NextCoord)) { Game.Chessboard[CurrentCoord.X, CurrentCoord.Y].Move(Game, CurrentCoord, NextCoord, Emptyspace); if (!(Checker.Checktest(Game, NextCoord))) { Game.LastmoveReverser(Game, CurrentCoord, NextCoord, Emptyspace); } else { Game.LastmoveReverser(Game, CurrentCoord, NextCoord, Emptyspace); return(true); } } break; case "Chess.Queen": Queen Queenmover = new Queen("a"); Queenmover = (Queen)Game.Chessboard[CurrentCoord.X, CurrentCoord.Y]; if (Queenmover.isMovable(Game, NextCoord)) { Game.Chessboard[CurrentCoord.X, CurrentCoord.Y].Move(Game, CurrentCoord, NextCoord, Emptyspace); if (!(Checker.Checktest(Game, NextCoord))) { Game.LastmoveReverser(Game, CurrentCoord, NextCoord, Emptyspace); } else { Game.LastmoveReverser(Game, CurrentCoord, NextCoord, Emptyspace); return(true); } } break; case "Chess.Knight": Knight Knightmover = new Knight("a"); Knightmover = (Knight)Game.Chessboard[CurrentCoord.X, CurrentCoord.Y]; if (Knightmover.isMovable(Game, NextCoord)) { Game.Chessboard[CurrentCoord.X, CurrentCoord.Y].Move(Game, CurrentCoord, NextCoord, Emptyspace); if (!(Checker.Checktest(Game, NextCoord))) { Game.LastmoveReverser(Game, CurrentCoord, NextCoord, Emptyspace); } else { Game.LastmoveReverser(Game, CurrentCoord, NextCoord, Emptyspace); return(true); } } break; case "Chess.Castle": Castle Castlemover = new Castle("a"); Castlemover = (Castle)Game.Chessboard[CurrentCoord.X, CurrentCoord.Y]; if (Castlemover.isMovable(Game, NextCoord)) { Game.Chessboard[CurrentCoord.X, CurrentCoord.Y].Move(Game, CurrentCoord, NextCoord, Emptyspace); if (!(Checker.Checktest(Game, NextCoord))) { Game.LastmoveReverser(Game, CurrentCoord, NextCoord, Emptyspace); } else { Game.LastmoveReverser(Game, CurrentCoord, NextCoord, Emptyspace); return(true); } } break; case "Chess.King": King Kingmover = new King("a"); Kingmover = (King)Game.Chessboard[CurrentCoord.X, CurrentCoord.Y]; if (Kingmover.isMovable(Game, NextCoord)) { Game.Chessboard[CurrentCoord.X, CurrentCoord.Y].Move(Game, CurrentCoord, NextCoord, Emptyspace); if (!(Checker.Checktest(Game, NextCoord))) { Game.LastmoveReverser(Game, CurrentCoord, NextCoord, Emptyspace); } else { Game.LastmoveReverser(Game, CurrentCoord, NextCoord, Emptyspace); return(true); } } break; } } } } return(false); }
void INIT() { for (int r = 0; r < Dimension; r++) { for (int c = 0; c < Dimension; c++) { if (r == 1) { Ps[r, c] = new Pawn(MYCOLOR.BLACK, "Black_Pawn.png"); } else if (r == 6) { Ps[r, c] = new Pawn(MYCOLOR.WHITE, "White_Pawn.png"); } else if (r == 0 && (c == 0 || c == 7)) { Ps[r, c] = new Rook(MYCOLOR.BLACK, "Black_Rook.png"); } else if (r == 7 && (c == 0 || c == 7)) { Ps[r, c] = new Rook(MYCOLOR.WHITE, "White_Rook.png"); } else if (r == 0 && (c == 1 || c == 6)) { Ps[r, c] = new Horse(MYCOLOR.BLACK, "Black_Knight.png"); } else if (r == 7 && (c == 1 || c == 6)) { Ps[r, c] = new Horse(MYCOLOR.WHITE, "White_Knight.png"); } else if (r == 0 && (c == 2 || c == 5)) { Ps[r, c] = new Bishop(MYCOLOR.BLACK, "Black_Bishop.png"); } else if (r == 7 && (c == 2 || c == 5)) { Ps[r, c] = new Bishop(MYCOLOR.WHITE, "White_Bishop.png"); } else if (r == 0 && c == 3) { Ps[r, c] = new Queen(MYCOLOR.BLACK, "Black_Queen.png"); } else if (r == 7 && c == 3) { Ps[r, c] = new Queen(MYCOLOR.WHITE, "White_Queen.png"); } else if (r == 0 && c == 4) { Ps[r, c] = new King(MYCOLOR.BLACK, "Black_King.png"); } else if (r == 7 && c == 4) { Ps[r, c] = new King(MYCOLOR.WHITE, "White_King.png"); } else { Ps[r, c] = null; } MYCOLOR cc = (((r + c) % 2 == 0) ? MYCOLOR.WHITE : MYCOLOR.BLACK); Cs[r, c] = new Cell(r, c, cc, Ps[r, c], ChessPanel.Width, ChessPanel.Height, Dimension); Cs[r, c].Click += new System.EventHandler(CellSelected); ChessPanel.Controls.Add(Cs[r, c]); } } }
public int canDefend(int color) { Board b = new Board(); Castle castle = new Castle(color, 0, 0); Pawn pawn = new Pawn(color, 0, 0); Knight knight = new Knight(color, 0, 0); Bishop bishop = new Bishop(color, 0, 0); Queen queen = new Queen(color, 0, 0); int i, j, m, n, f = 0; for (i = 0; i < 8; i++) { for (j = 0; j < 8; j++) { b.setSquare(this.getInfo(i, j), i, j); } } Point p = b.searchKing(color); King king = new King(color, p.x, p.y); for (i = 0; i < 8; i++) { for (j = 0; j < 8; j++) { if (b.getInfo(i, j) == 0) { } else if (b.getInfo(i, j) == (color == 1?1:7)) { castle.x = i; castle.y = j; for (m = 0; m < 8; m++) { for (n = 0; n < 8; n++) { if (castle.move(b, m, n) == 1) { f = b.getInfo(m, n); b.setSquare(0, i, j); b.setSquare((color == 1?1:7), m, n); if (king.isChecked(b) == 0) { return(1); } b.setSquare((color == 1?1:7), i, j); b.setSquare(f, m, n); } } } } else if (b.getInfo(i, j) == (color == 1?2:8)) { knight.x = i; knight.y = j; for (m = 0; m < 8; m++) { for (n = 0; n < 8; n++) { if (knight.move(b, m, n) == 1) { f = b.getInfo(m, n); b.setSquare(0, i, j); b.setSquare((color == 1?2:8), m, n); if (king.isChecked(b) == 0) { return(1); } b.setSquare((color == 1?2:8), i, j); b.setSquare(f, m, n); } } } } else if (b.getInfo(i, j) == (color == 1?3:9)) { bishop.x = i; bishop.y = j; for (m = 0; m < 8; m++) { for (n = 0; n < 8; n++) { if (bishop.move(b, m, n) == 1) { f = b.getInfo(m, n); b.setSquare(0, i, j); b.setSquare((color == 1?3:9), m, n); if (king.isChecked(b) == 0) { return(1); } b.setSquare((color == 1?3:9), i, j); b.setSquare(f, m, n); } } } } else if (b.getInfo(i, j) == (color == 1?4:10)) { queen.x = i; queen.y = j; for (m = 0; m < 8; m++) { for (n = 0; n < 8; n++) { if (queen.move(b, m, n) == 1) { f = b.getInfo(m, n); b.setSquare(0, i, j); b.setSquare((color == 1?4:10), m, n); if (king.isChecked(b) == 0) { return(1); } b.setSquare((color == 1?4:10), i, j); b.setSquare(f, m, n); } } } } else if (b.getInfo(i, j) == (color == 1?5:11)) { for (m = 0; m < 8; m++) { for (n = 0; n < 8; n++) { if (king.move(b, m, n) == 1) { f = b.getInfo(m, n); king.x = m; king.y = n; b.setSquare(0, i, j); b.setSquare((color == 1?5:11), m, n); if (king.isChecked(b) == 0) { return(1); } b.setSquare((color == 1?5:11), i, j); b.setSquare(f, m, n); king.x = p.x; king.y = p.y; } } } } else if (b.getInfo(i, j) == (color == 1?6:12)) { pawn.x = i; pawn.y = j; for (m = 0; m < 8; m++) { for (n = 0; n < 8; n++) { if (pawn.move(b, m, n) == 1) { f = b.getInfo(m, n); b.setSquare(0, i, j); b.setSquare((color == 1?6:12), m, n); if (king.isChecked(b) == 0) { return(1); } b.setSquare((color == 1?6:12), i, j); b.setSquare(f, m, n); } } } } } } return(0); }
public static void AddPiece(this Game game, Square square, PieceType type) { Piece piece = null; switch (type) { case PieceType.NoPiece: break; case PieceType.WhiteKing: piece = new King(Color.White); break; case PieceType.WhiteQueen: piece = new Queen(Color.White); break; case PieceType.WhiteRook: piece = new Rook(Color.White); break; case PieceType.WhiteBishop: piece = new Bishop(Color.White); break; case PieceType.WhiteNight: piece = new Knight(Color.White); break; case PieceType.WhitePawn: piece = new Pawn(Color.White); break; case PieceType.BlackKing: piece = new King(Color.Black); break; case PieceType.BlackQueen: piece = new Queen(Color.Black); break; case PieceType.BlackRook: piece = new Rook(Color.Black); break; case PieceType.BlackBishop: piece = new Bishop(Color.Black); break; case PieceType.BlackKnight: piece = new Knight(Color.Black); break; case PieceType.BlackPawn: piece = new Pawn(Color.Black); break; default: throw new NotImplementedException(); } game.AddPiece(square.File, square.Rank, piece); }
public void BeforeEachTest() { Target = new Queen(); MovesFrom11 = Target.GetMovesFrom(new BoardCoordinate(1, 1)); }
public void CreateDefaultPosition() { Figure blackCastle1 = new Castle(false, 0, 0); Figure blackCastle2 = new Castle(false, 7, 0); Figure blackKnight1 = new Knight(false, 1, 0); Figure blackKnight2 = new Knight(false, 6, 0); Figure blackBishop1 = new Bishop(false, 2, 0); Figure blackBishop2 = new Bishop(false, 5, 0); Figure blackKing = new King(false, 4, 0); Figure blackQueen = new Queen(false, 3, 0); Figure blackPawn1 = new Pawn(false, 0, 1); Figure blackPawn2 = new Pawn(false, 1, 1); Figure blackPawn3 = new Pawn(false, 2, 1); Figure blackPawn4 = new Pawn(false, 3, 1); Figure blackPawn5 = new Pawn(false, 4, 1); Figure blackPawn6 = new Pawn(false, 5, 1); Figure blackPawn7 = new Pawn(false, 6, 1); Figure blackPawn8 = new Pawn(false, 7, 1); Figure whiteCastle1 = new Castle(true, 0, 7); Figure whiteCastle2 = new Castle(true, 7, 7); Figure whiteKnight1 = new Knight(true, 1, 7); Figure whiteKnight2 = new Knight(true, 6, 7); Figure whiteBishop1 = new Bishop(true, 2, 7); Figure whiteBishop2 = new Bishop(true, 5, 7); Figure whiteKing = new King(true, 4, 7); Figure whiteQueen = new Queen(true, 3, 7); Figure whitePawn1 = new Pawn(true, 0, 6); Figure whitePawn2 = new Pawn(true, 1, 6); Figure whitePawn3 = new Pawn(true, 2, 6); Figure whitePawn4 = new Pawn(true, 3, 6); Figure whitePawn5 = new Pawn(true, 4, 6); Figure whitePawn6 = new Pawn(true, 5, 6); Figure whitePawn7 = new Pawn(true, 6, 6); Figure whitePawn8 = new Pawn(true, 7, 6); _table[0, 0].PutFigure(blackCastle1); _table[1, 0].PutFigure(blackKnight1); _table[2, 0].PutFigure(blackBishop1); _table[3, 0].PutFigure(blackQueen); _table[4, 0].PutFigure(blackKing); _table[5, 0].PutFigure(blackBishop2); _table[6, 0].PutFigure(blackKnight2); _table[7, 0].PutFigure(blackCastle2); _table[0, 1].PutFigure(blackPawn1); _table[1, 1].PutFigure(blackPawn2); _table[2, 1].PutFigure(blackPawn3); _table[3, 1].PutFigure(blackPawn4); _table[4, 1].PutFigure(blackPawn5); _table[5, 1].PutFigure(blackPawn6); _table[6, 1].PutFigure(blackPawn7); _table[7, 1].PutFigure(blackPawn8); _table[0, 7].PutFigure(whiteCastle1); _table[1, 7].PutFigure(whiteKnight1); _table[2, 7].PutFigure(whiteBishop1); _table[3, 7].PutFigure(whiteQueen); _table[4, 7].PutFigure(whiteKing); _table[5, 7].PutFigure(whiteBishop2); _table[6, 7].PutFigure(whiteKnight2); _table[7, 7].PutFigure(whiteCastle2); _table[0, 6].PutFigure(whitePawn1); _table[1, 6].PutFigure(whitePawn2); _table[2, 6].PutFigure(whitePawn3); _table[3, 6].PutFigure(whitePawn4); _table[4, 6].PutFigure(whitePawn5); _table[5, 6].PutFigure(whitePawn6); _table[6, 6].PutFigure(whitePawn7); _table[7, 6].PutFigure(whitePawn8); }
/* Executes a chess move * @param Position from, Position to */ public void ExecuteMove(Position from, Position to) { Piece captured = Move(from, to); if (IsInCheck(CurrentPlayer)) { UndoMove(from, to, captured); throw new GameBoardException("You cannot put yourself in check."); } Piece p = this.Board.GetPiece(to); /*** Promotion ***/ if (p is Pawn) { if ((p.Color == Color.White && to.Row == 0) || (p.Color == Color.Black && to.Row == 7)) { p = this.Board.RemovePiece(to); this._pieces.Remove(p); bool wrong = true; Piece np = null; do { Console.Clear(); View.PrintMatch(this, null); Console.WriteLine("PROMOTION\n" + "Knight [N/n]\n" + "Rook [R/r]\n" + "Bishop [B/b]\n" + "Queen [Q/q]\n" + "Choose a piece: "); string c = Console.ReadLine(); switch (c.ToUpper()) { case "N": np = new Knight(p.Color, this.Board); wrong = false; break; case "R": np = new Rook(p.Color, this.Board); wrong = false; break; case "B": np = new Bishop(p.Color, this.Board); wrong = false; break; case "Q": np = new Queen(p.Color, this.Board); wrong = false; break; default: Console.WriteLine("Invalid Piece!" + "\nPress enter to continue..."); Console.ReadLine(); wrong = true; break; } } while (wrong); this.Board.InsertPiece(np, to); this._pieces.Add(np); } } if (IsInCheck(Opponent(CurrentPlayer))) { this.Check = true; } else { this.Check = false; } if (IsInCheckmate(Opponent(CurrentPlayer))) { this.Finished = true; } else { this.Turn++; SwitchPlayers(); } /*** En Passant ***/ if (p is Pawn && (to.Row == from.Row - 2 || to.Row == from.Row + 2)) { this.EnPassant = p; } else { this.EnPassant = null; } }
public GameBoard() { King BlackKing = new King("BLACK"); Queen BlackQueen = new Queen("BLACK", 1); Castle BlackCastle1 = new Castle("BLACK", 1); Castle BlackCastle2 = new Castle("BLACK", 2); Bishop BlackBishop1 = new Bishop("BLACK", 1); Bishop BlackBishop2 = new Bishop("BLACK", 2); Knight BlackKnight1 = new Knight("BLACK", 1); Knight BlackKnight2 = new Knight("BLACK", 2); Pawn BlackPawn1 = new Pawn("BLACK", 1); Pawn BlackPawn2 = new Pawn("BLACK", 2); Pawn BlackPawn3 = new Pawn("BLACK", 3); Pawn BlackPawn4 = new Pawn("BLACK", 4); Pawn BlackPawn5 = new Pawn("BLACK", 5); Pawn BlackPawn6 = new Pawn("BLACK", 6); Pawn BlackPawn7 = new Pawn("BLACK", 7); Pawn BlackPawn8 = new Pawn("BLACK", 8); Empty Emptyspace = new Empty(); for (byte i = 2; i < 6; i++) { for (byte j = 0; j < 8; j++) { Chessboard[i, j] = Emptyspace; } } Black.Add(BlackKing); Black.Add(BlackQueen); Black.Add(BlackCastle1); Black.Add(BlackCastle2); Black.Add(BlackBishop1); Black.Add(BlackBishop2); Black.Add(BlackKnight1); Black.Add(BlackKnight2); Black.Add(BlackPawn1); Black.Add(BlackPawn2); Black.Add(BlackPawn3); Black.Add(BlackPawn4); Black.Add(BlackPawn5); Black.Add(BlackPawn6); Black.Add(BlackPawn7); Black.Add(BlackPawn8); King WhiteKing = new King("WHITE"); Queen WhiteQueen = new Queen("WHITE", 1); Castle WhiteCastle1 = new Castle("WHITE", 1); Castle WhiteCastle2 = new Castle("WHITE", 2); Bishop WhiteBishop1 = new Bishop("WHITE", 1); Bishop WhiteBishop2 = new Bishop("WHITE", 2); Knight WhiteKnight1 = new Knight("WHITE", 1); Knight WhiteKnight2 = new Knight("WHITE", 2); Pawn WhitePawn1 = new Pawn("WHITE", 1); Pawn WhitePawn2 = new Pawn("WHITE", 2); Pawn WhitePawn3 = new Pawn("WHITE", 3); Pawn WhitePawn4 = new Pawn("WHITE", 4); Pawn WhitePawn5 = new Pawn("WHITE", 5); Pawn WhitePawn6 = new Pawn("WHITE", 6); Pawn WhitePawn7 = new Pawn("WHITE", 7); Pawn WhitePawn8 = new Pawn("WHITE", 8); White.Add(WhiteKing); White.Add(WhiteQueen); White.Add(WhiteCastle1); White.Add(WhiteCastle2); White.Add(WhiteBishop1); White.Add(WhiteBishop2); White.Add(WhiteKnight1); White.Add(WhiteKnight2); White.Add(WhitePawn1); White.Add(WhitePawn2); White.Add(WhitePawn3); White.Add(WhitePawn4); White.Add(WhitePawn5); White.Add(WhitePawn6); White.Add(WhitePawn7); White.Add(WhitePawn8); Chessboard[0, 0] = WhiteCastle1; Chessboard[0, 1] = WhiteKnight1; Chessboard[0, 2] = WhiteBishop1; Chessboard[0, 3] = WhiteQueen; Chessboard[0, 4] = WhiteKing; Chessboard[0, 5] = WhiteBishop2; Chessboard[0, 6] = WhiteKnight2; Chessboard[0, 7] = WhiteCastle2; Chessboard[1, 0] = WhitePawn1; Chessboard[1, 1] = WhitePawn2; Chessboard[1, 2] = WhitePawn3; Chessboard[1, 3] = WhitePawn4; Chessboard[1, 4] = WhitePawn5; Chessboard[1, 5] = WhitePawn6; Chessboard[1, 6] = WhitePawn7; Chessboard[1, 7] = WhitePawn8; Chessboard[7, 0] = BlackCastle1; Chessboard[7, 1] = BlackKnight1; Chessboard[7, 2] = BlackBishop1; Chessboard[7, 3] = BlackQueen; Chessboard[7, 4] = BlackKing; Chessboard[7, 5] = BlackBishop2; Chessboard[7, 6] = BlackKnight2; Chessboard[7, 7] = BlackCastle2; Chessboard[6, 0] = BlackPawn1; Chessboard[6, 1] = BlackPawn2; Chessboard[6, 2] = BlackPawn3; Chessboard[6, 3] = BlackPawn4; Chessboard[6, 4] = BlackPawn5; Chessboard[6, 5] = BlackPawn6; Chessboard[6, 6] = BlackPawn7; Chessboard[6, 7] = BlackPawn8; for (byte i = 0; i < 8; i++) { for (byte j = 0; j < 8; j++) { if (!(Chessboard[i, j].GetType().ToString().Contains("Empty"))) { Chessboard[i, j].Current.X = i; Chessboard[i, j].Current.Y = j; } } } }
private void buttonAccept_Click(object sender, System.EventArgs e) { //Button "Accept" clicked, carry out the promotion if (comboBoxFigures.SelectedItem == null) { MessageBox.Show(this, "You must select a figure", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { promotionRealized = true; //Delete the pawn Board.getInstance().killPiece(pawn.getPosition(), true); //Create the new figure string fig = comboBoxFigures.SelectedItem.ToString(); Position pos = pawn.getPosition(); Color color = pawn.getColor(); Piece piece = null; if (fig == "Bishop") { piece = new Bishop(pos, color); } else if (fig == "Knight") { piece = new Knight(pos, color); } else if (fig == "Queen") { piece = new Queen(pos, color); } else if (fig == "Rook") { piece = new Rook(pos, color); } if (color == Color.White) { Board.getInstance().getWhitePieces().insert(piece); } else { Board.getInstance().getBlackPieces().insert(piece); } Board.getInstance().deletePiece(pawn); Board.getInstance().drawPiece(piece); this.Close(); } }
/********************************************************************* * SetPieces * Set up all of the pieces on the board *********************************************************************/ void SetPieces() { Pawn w; Pawn b; for (int i = 0; i < 8; i++) { w = new Pawn("white"); board[i, 1].SetPiece(w); whiteTeam.Add(w); b = new Pawn("black"); board[i, 6].SetPiece(b); blackTeam.Add(b); } Rook rook = new Rook("white"); board[0, 0].SetPiece(rook); whiteTeam.Add(rook); rook = new Rook("white"); board[7, 0].SetPiece(rook); whiteTeam.Add(rook); Rook rookB = new Rook("black"); board[0, 7].SetPiece(rookB); blackTeam.Add(rookB); rookB = new Rook("black"); board[7, 7].SetPiece(rookB); blackTeam.Add(rookB); Knight knight = new Knight("white"); board[1, 0].SetPiece(knight); whiteTeam.Add(knight); knight = new Knight("white"); board[6, 0].SetPiece(knight); whiteTeam.Add(knight); Knight knightB = new Knight("black"); board[1, 7].SetPiece(knightB); blackTeam.Add(knightB); knightB = new Knight("black"); board[6, 7].SetPiece(knightB); blackTeam.Add(knightB); Bishop bishop = new Bishop("white"); board[2, 0].SetPiece(bishop); whiteTeam.Add(bishop); bishop = new Bishop("white"); board[5, 0].SetPiece(bishop); whiteTeam.Add(bishop); Bishop bishopB = new Bishop("black"); board[2, 7].SetPiece(bishopB); blackTeam.Add(bishopB); bishopB = new Bishop("black"); board[5, 7].SetPiece(bishopB); blackTeam.Add(bishopB); Queen queen = new Queen("white"); board[3, 0].SetPiece(queen); whiteTeam.Add(queen); queen = new Queen("black"); board[3, 7].SetPiece(queen); blackTeam.Add(queen); King king = new King("white"); board[4, 0].SetPiece(king); whiteTeam.Add(king); king = new King("black"); board[4, 7].SetPiece(king); blackTeam.Add(king); }
private void MoveFigure(int x, int y, int xNew, int yNew, Figure[,] table = null) { if (table == null) { table = figureTable; } else { table = tempTable; } Figure oldFigure = table[x, y]; oldFigure.Move(xNew, yNew); if (table == figureTable) { previousMove = new Tuple <PlayerType, FigureType, int, int, int, int>(onMove, oldFigure.type, x, y, xNew, yNew); } if (table[x, y] != null && table[x, y] is Pawn) { Pawn pawn = table[x, y] as Pawn; // Check if en passant if (xNew == x + pawn.Direction && (yNew == y - 1 || yNew == y + 1) && table[xNew, yNew] == null) { table[x, yNew] = null; } table[xNew, yNew] = oldFigure; table[x, y] = null; // Check for promotion (Upgrade this code!!!) - only in real move mode if (table == figureTable && (xNew == 0 || xNew == 7)) { new Promotion(onMove).ShowDialog(); FigureType promotionFigure = Promotion.promotedFigure; switch (promotionFigure) { case FigureType.KNIGHT: table[xNew, yNew] = new Knight(xNew, yNew, onMove, promotionFigure, this); break; case FigureType.BISHOP: table[xNew, yNew] = new Bishop(xNew, yNew, onMove, promotionFigure, this); break; case FigureType.ROOK: table[xNew, yNew] = new Rook(xNew, yNew, onMove, promotionFigure, this); break; case FigureType.QUEEN: table[xNew, yNew] = new Queen(xNew, yNew, onMove, promotionFigure, this); break; default: break; } } } // Rokada (Castling) else if (table == figureTable && table[x, y] != null && table[x, y] is King && Math.Abs(yNew - y) == 2) { // Move king table[xNew, yNew] = oldFigure; table[x, y] = null; // Small - move rook if (yNew > y) { table[x, yNew + 1].Move(x, yNew - 1); table[x, yNew - 1] = table[x, yNew + 1]; table[x, yNew + 1] = null; } // Big - move rook else { table[x, yNew - 2].Move(x, yNew + 1); table[x, yNew + 1] = table[x, yNew - 2]; table[x, yNew - 2] = null; } } else { table[xNew, yNew] = oldFigure; table[x, y] = null; } }
//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; } } }