private static void KingStandardMovement() { Debug.WriteLine("Test: KingStandardMovement, Begin"); var board = new Board(); var KW = new King(new Vector2(4, 7), Color.White); board.Pieces.Add(KW); board.WhiteKing = KW; //Check long straight slides Debug.Assert(!KW.IsMoveValid(board, KW.Position.AddVector(0, 0)), "Dont move"); Debug.Assert(!KW.IsMoveValid(board, KW.Position.AddVector(0, 1)), "Move down, out of bounds"); Debug.Assert(KW.IsMoveValid(board, KW.Position.AddVector(0, -1)), "Move up"); Debug.Assert(!KW.IsMoveValid(board, KW.Position.AddVector(1, 1)), "Move diagonal"); Debug.Assert(KW.IsMoveValid(board, KW.Position.AddVector(-1, -1)), "Move diagonal negative"); Debug.WriteLine("Test: KingStandardMovement, End"); }
private static void KingCheckChecking() { Debug.WriteLine("Test: KingCheckChecking, Begin"); var board = new Board(); var KW = new King(new Vector2(4, 7), Color.White); board.Pieces.Add(KW); board.WhiteKing = KW; //Move to the corner with no enemeies just fine KW.Position = new Vector2(0, 6); Debug.Assert(KW.IsMoveValid(board, new Vector2(0, 7)), "Moves into corner with no enemies gaurding"); //Create a bishop covering the corner at distance, king can no longer move to that space var sniperBishop = new Bishop(new Vector2(7, 0), Color.Black); board.Pieces.Add(sniperBishop); Debug.Assert(!KW.IsMoveValid(board, new Vector2(0, 7)), "Cannot move into path of distant rook"); //Add a pawn between the corner and the rook, King can now move into the corner freely var protectorPawn = new Pawn(new Vector2(6, 1), Color.White); board.Pieces.Add(protectorPawn); Debug.Assert(protectorPawn.IsMoveValid(board, protectorPawn.Position.AddVector(0, -1)), "move up"); Debug.Assert(KW.IsMoveValid(board, new Vector2(0, 7)), "Moves into corner with enemies gaurding because pawn is protecting path"); //Move king to corner covered by rook, but protected by pawn KW.Position = new Vector2(0, 7); //Move king freely Debug.Assert(!KW.IsMoveValid(board, KW.Position.AddVector(0, 1)), "Move down, out of bounds"); Debug.Assert(!KW.IsMoveValid(board, KW.Position.AddVector(-1, 0)), "Move left, out of bounds"); Debug.Assert(!KW.IsMoveValid(board, KW.Position.AddVector(-1, 1)), "Move diagonal, out of bounds"); Debug.Assert(KW.IsMoveValid(board, KW.Position.AddVector(0, -1)), "Move up"); Debug.Assert(!KW.IsMoveValid(board, KW.Position.AddVector(1, 1)), "Move diagonal twoards threat, still protected"); //Attempting to Move pawn expsoses king to check Debug.Assert(!protectorPawn.IsMoveValid(board, protectorPawn.Position.AddVector(0, -1)), "Can no longer move up because it would expose king to check"); //Killing rook with pawn protects king Debug.Assert(protectorPawn.IsMoveValid(board, protectorPawn.Position.AddVector(1, -1)), "Kill rook. King is safe"); //Put another blocking pawn, then move original pawn. King still protected var protectorPawn2 = new Pawn(new Vector2(5, 2), Color.White); board.Pieces.Add(protectorPawn2); Debug.Assert(protectorPawn.IsMoveValid(board, protectorPawn.Position.AddVector(0, -1)), "Move up now that a different pawn is blocking path"); Debug.Assert(protectorPawn2.IsMoveValid(board, protectorPawn2.Position.AddVector(0, -1)), "Move up now that a different pawn is blocking path"); Debug.Assert(!protectorPawn2.IsMoveValid(board, protectorPawn2.Position.AddVector(1, -1)), "Pawn can not attack teammate silly!"); Debug.WriteLine("Test: KingCheckChecking, End"); }
private static void KingBlockedMovementAndAttacks() { Debug.WriteLine("Test: KingBlockedMovementAndAttacks, Begin"); var board = new Board(); var KW = new King(new Vector2(4, 7), Color.White); board.Pieces.Add(KW); board.WhiteKing = KW; //Test the King movement getting blocked by friends and attacking enemies var blockingPawn = new Pawn(new Vector2(4, 6), Color.White); board.Pieces.Add(blockingPawn); Debug.Assert(!KW.IsMoveValid(board, KW.Position.AddVector(0, -1)), "Friendly blocking destination"); blockingPawn.Position = new Vector2(5, 6); Debug.Assert(!KW.IsMoveValid(board, KW.Position.AddVector(1, -1)), "Friendly blocking diagonal destination"); blockingPawn.Color = Color.Black; Debug.Assert(KW.IsMoveValid(board, KW.Position.AddVector(1, -1)), "Enemy attacked at diagonal destination"); blockingPawn.Position = new Vector2(4, 6); Debug.Assert(!KW.IsMoveValid(board, KW.Position.AddVector(1, 0)), "Move puts me in check"); Debug.Assert(!KW.IsMoveValid(board, KW.Position.AddVector(-1, 0)), "Move puts me in check"); //Check that the King can kill a enemy holding him in check Debug.Assert(KW.IsMoveValid(board, KW.Position.AddVector(0, -1)), "Attack enemy putting me in check"); //Check that the king cannot kill a piece holding it in check if that in turn puts it into further check var pawnReinforcements = new Pawn(new Vector2(3, 5), Color.Black); board.Pieces.Add(pawnReinforcements); Debug.Assert(!KW.IsMoveValid(board, KW.Position.AddVector(0, -1)), "Attack enemy putting me in check, blocked by enemy protecting him"); //However, killing that enemy totally works if the covering peice is your teammate pawnReinforcements.Color = Color.White; Debug.Assert(KW.IsMoveValid(board, KW.Position.AddVector(0, -1)), "Attack enemy putting me in check, protected by teammate"); //Knight sitting off in the distance, protcting a pawn Knight knightStealthy = new Knight(blockingPawn.Position.AddVector(1, -2), Color.Black); board.Pieces.Add(knightStealthy); Debug.Assert(!KW.IsMoveValid(board, KW.Position.AddVector(0, -1)), "Attack enemy putting me in check, enemy is protected by knight"); Debug.WriteLine("Test: KingBlockedMovementAndAttacks, End"); }
public static void KingCastling() { Debug.WriteLine("Test: KingCastling, Begin"); var board = new Board(); //Create King and 2 rooks in standard positions var KW = new King(new Vector2(4, 7), Color.White); var RW0 = new Rook(new Vector2(0, 7), Color.White); var RW1 = new Rook(new Vector2(7, 7), Color.White); board.Pieces.Add(KW); board.Pieces.Add(RW0); board.Pieces.Add(RW1); board.WhiteKing = KW; //Castle in both directions Debug.Assert(KW.IsMoveValid(board, KW.Position.AddVector(2, 0)), "Castled with the King Rook"); Debug.Assert(KW.IsMoveValid(board, KW.Position.AddVector(-2, 0)), "Castled with the Queen Rook"); //Castling doesnt work if path of king passes through a checked position var QB0 = new Queen(new Vector2(2, 2), Color.Black); board.Pieces.Add(QB0); Debug.Assert(!KW.IsMoveValid(board, KW.Position.AddVector(-2, 0)), "Castled with the Queen Rook, path gaurded by queen"); //Block the queens path and it works again var blockingPawn = new Pawn(new Vector2(2, 6), Color.White); board.Pieces.Add(blockingPawn); Debug.Assert(KW.IsMoveValid(board, KW.Position.AddVector(-2, 0)), "Castled with the Queen Rook, queens view blocked by hero pawn"); //Block the kings path with a teammate blockingPawn.Position = new Vector2(2, 7); Debug.Assert(!KW.IsMoveValid(board, KW.Position.AddVector(-2, 0)), "Castled with the Queen Rook, hero pawn is now in the way"); Debug.WriteLine("Test: KingCastling, End"); }