public void TestPiecePlayer() { ChessBoard cb = new ChessBoard (); Bishop cpb = new Bishop (cb [4, 4], "B"); King cpk = new King (cb [3, 3], "W"); Assert.AreEqual ("B", cpb.Player); Assert.AreEqual ("W", cpk.Player); }
public void TestPieceVerticalPath() { ChessBoard cb = new ChessBoard (); Bishop cpb = new Bishop (cb [4, 4], "B"); King cpk = new King (cb [3, 3], "W"); Assert.IsTrue (cpb.CheckHorizontal(cb[3, 2], cb)); Assert.IsTrue (cpk.CheckVertical(cb[0, 0], cb)); }
public void TestPieceName() { ChessBoard cb = new ChessBoard (); Bishop cpb = new Bishop (cb [4, 4], "B"); King cpk = new King (cb [3, 3], "W"); Assert.AreEqual ("BB", cpb.Name); Assert.AreEqual ("WKG", cpk.Name); }
public void TestPieceGrid() { ChessBoard cb = new ChessBoard (); Bishop cpb = new Bishop (cb [4, 4], "B"); King cpk = new King (cb [3, 3], "W"); cpk.MovePiece(cb[3, 4], cb); cpk.MovePiece(cb[6, 6], cb); Assert.AreEqual (cb[4, 4], cpb.Grid); Assert.AreEqual (cb[3, 4], cpk.Grid); }
public void TestKingCaptureRule() { ChessBoard cb = new ChessBoard (); King kg = new King (cb [4, 4], "B"); cb [4, 4].PlacePiece (kg); //Create piece cases within range cb[5, 4].PlacePiece(new King(cb[5, 4], "B")); cb [5, 5].PlacePiece (new King (cb [5, 5], "W")); //Check whether the pieces can be captured or not. Assert.IsFalse(kg.CheckMovementRule(cb[5,4], cb)); Assert.IsTrue (kg.CheckMovementRule (cb [5, 5], cb)); }
public void TestKingMovement() { ChessBoard cb = new ChessBoard (); King kg = new King (cb [4, 4], "B"); cb [4, 4].PlacePiece (kg); //Test invalid movement Assert.IsFalse(kg.CheckMovementRule(cb[6,7], cb)); Assert.AreEqual(kg.MovePiece(cb[6, 7], cb), "Invalid move"); Assert.AreSame (kg.Grid, cb [4, 4]); //Test valid movement Assert.IsTrue (kg.CheckMovementRule (cb [3, 5], cb)); Assert.IsNull (kg.MovePiece (cb [3, 5], cb)); Assert.AreSame (kg.Grid, cb [3, 5]); }
public void TestKingMovementRuleOnEmptyGrids() { ChessBoard cb = new ChessBoard (); King kg = new King (cb [4, 4], "B"); cb [4, 4].PlacePiece (kg); //Check all possible movements Assert.IsTrue (kg.CheckMovementRule (cb [5, 4], cb)); Assert.IsTrue (kg.CheckMovementRule (cb [5, 5], cb)); Assert.IsTrue (kg.CheckMovementRule (cb [5, 3], cb)); Assert.IsTrue (kg.CheckMovementRule (cb [4, 3], cb)); Assert.IsTrue (kg.CheckMovementRule (cb [4, 5], cb)); Assert.IsTrue (kg.CheckMovementRule (cb [3, 3], cb)); Assert.IsTrue (kg.CheckMovementRule (cb [3, 4], cb)); Assert.IsTrue (kg.CheckMovementRule (cb [3, 5], cb)); //Check some invalid movements Assert.IsFalse (kg.CheckMovementRule (cb [6, 6], cb)); Assert.IsFalse (kg.CheckMovementRule (cb [4, 6], cb)); Assert.IsFalse (kg.CheckMovementRule (cb [6, 4], cb)); }
public void KillTheKing(King king) { //TODO }
public void UnMovePiece(Move move) { Position to = move.To; Position from = move.From; // Pawn demotion if (move.Promotion) { Board.DemotePawn(move); } if (move.Piece is King) { King king = move.Piece as King; if (move.Piece.MoveCount == 1) { if (king.Color == PieceColor.White) { Board.UpdatePieceEnum(king, PieceEnum.WhiteKing); } else if (king.Color == PieceColor.Black) { Board.UpdatePieceEnum(king, PieceEnum.BlackKing); } } if (move.Castle) { Board.MovePiece(move.CastleMove.To, move.CastleMove.From); move.CastleMove.Piece.MoveCount--; } } Board.MovePiece(to, from); move.Piece.MoveCount--; if (!(move.Capture is Empty)) { Board.AddPiece(move.Capture.Position.Row, move.Capture.Position.Col, move.Capture); } if (progress.Count > 0) { progress.Pop(); } if (moves.Count > 0) { moves.Pop(); } if (prevHashes.Count > 0) { prevHashes.Pop(); } if (CurrentTurn == PieceColor.White) { CurrentTurn = PieceColor.Black; if (moves.Count > 0 && moves.Peek().EnPassantMove) { Board.UpdatePieceEnum(moves.Peek().Piece, PieceEnum.WhitePawnEnPassant); } } else { CurrentTurn = PieceColor.White; if (moves.Count > 0 && moves.Peek().EnPassantMove) { Board.UpdatePieceEnum(moves.Peek().Piece, PieceEnum.BlackPawnEnPassant); } } }
internal void MovePiece(Move move) { Position to = move.To; Position from = move.From; if (move.Capture is King) { Debug.WriteLine("Not supposed to happen!"); } // Pawn promotion if (move.Promotion) { Board.PromotePawn(move); } if (move.Piece is Pawn) { Pawn pawn = move.Piece as Pawn; if (move.EnPassantMove && pawn.Color == PieceColor.White) { Board.UpdatePieceEnum(pawn, PieceEnum.WhitePawnEnPassant); } else if (move.EnPassantMove && pawn.Color == PieceColor.Black) { Board.UpdatePieceEnum(pawn, PieceEnum.BlackPawnEnPassant); } } if (move.Piece is King) { King king = move.Piece as King; if (move.Castle) { Board.MovePiece(move.CastleMove.From, move.CastleMove.To); move.CastleMove.Piece.MoveCount++; } if (king.Color == PieceColor.White) { Board.UpdatePieceEnum(king, PieceEnum.WhiteKingNoCastle); } else if (king.Color == PieceColor.Black) { Board.UpdatePieceEnum(king, PieceEnum.BlackKingNoCastle); } } if (!(move.Piece is Pawn) || move.Capture is Empty) { if (progress.Count > 0) { progress.Push(progress.Peek() + 1); } else { progress.Push(1); } } else { progress.Push(0); } if (!(move.Capture is Empty)) { Board.RemovePiece(move.Capture.Position.Row, move.Capture.Position.Col); } Board.MovePiece(from, to); move.Piece.MoveCount++; moves.Push(move); prevHashes.Push(Board.Hash); if (CurrentTurn == PieceColor.White) { CurrentTurn = PieceColor.Black; ClearPassant(PieceColor.Black); } else { CurrentTurn = PieceColor.White; ClearPassant(PieceColor.White); } if (IsThreatened(Board.GetKing(CurrentTurn))) { move.CheckBonus = true; } }
public ChessGame() { /* * 1 R N B K Q B N R * 2 P P P P P P P P * 3 * 4 * 5 * 6 * 7 P P P P P P P P * 8 R N B K Q B N R * A B C D E F G H * * { * {0,0 0,1 0,2 0,3 0,4 0,5 0,6 0,7} * {1,0 1,1 1,2 1,3 1,4 1,5 1,6 1,7} * {2,0 2,1 2,2 2,3 2,4 2,5 2,6 2,7} * {3,0 3,1 3,2 3,3 3,4 3,5 3,6 3,7} * {4,0 4,1 4,2 4,3 4,4 4,5 4,6 4,7} * {5,0 5,1 5,2 5,3 5,4 5,5 5,6 5,7} * {6,0 6,1 6,2 6,3 6,4 6,5 6,6 6,7} * {7,0 7,1 7,2 7,3 7,4 7,5 7,6 7,7} * } * * Movement patterns: * Rook: * The Rook can only move in rows and columns, and therefore * can either move to the same index in a lower or higher indexed array contained in the first dimension, * or it can move to any other position within its current second dimension. * * Bishop: * The Bishop can only move diagonally and therefore its second-dimensional index must be shifted by 1 * in either direction for every step it takes in the first dimension. * * Queen: * The Queen possesses the movement capabilities of both the Rook and the Bishop. * * King: * The King may only move 1 tile at a time, and therefore its indexed position * can only be shifted by 1 in any direction, in both the first and the second dimension. * * Pawn: * The Pawn can only move forward in a straight line, * except when there is a piece of the opposite color on a tile that is located * on either of the 2 frontal diagonal places. * The pawn may move 2 steps straight forward on its very first move. * If the pawn reaches the opposite side of the board, it may be converted into * a Rook, Bishop, Knight, or a Queen. * * Knight: * The Knight may move to any position that is 2 steps in any of the 4 cardinal directions, * and then displaced 1 step to either the right or left. * Unlike all other pieces, the Knight is not hindered by other pieces being in its way. * */ //Add pieces to board and set appropriate types and colors board[7, 0] = new Rook(PieceColor.black, board); board[7, 1] = new Knight(PieceColor.black, board); board[7, 2] = new Bishop(PieceColor.black, board); board[7, 3] = new King(PieceColor.black, board); board[7, 4] = new Queen(PieceColor.black, board); board[7, 5] = new Bishop(PieceColor.black, board); board[7, 6] = new Knight(PieceColor.black, board); board[7, 7] = new Rook(PieceColor.black, board); board[0, 0] = new Rook(PieceColor.white, board); board[0, 1] = new Knight(PieceColor.white, board); board[0, 2] = new Bishop(PieceColor.white, board); board[0, 3] = new King(PieceColor.white, board); board[0, 4] = new Queen(PieceColor.white, board); board[0, 5] = new Bishop(PieceColor.white, board); board[0, 6] = new Knight(PieceColor.white, board); board[0, 7] = new Rook(PieceColor.white, board); for (int i = 0; i < 8; i += 1) { board[1, i] = new Pawn(PieceColor.white, board); } for (int i = 0; i < 8; i += 1) { board[6, i] = new Pawn(PieceColor.black, board); } }