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"); }
public static void TestBishops() { Debug.WriteLine("Test: TestBishops, Begin"); var board = new Board(); //Set them up in standard positions Bishop RW0 = new Bishop(new Vector2(2, 7), Color.White), RW1 = new Bishop(new Vector2(5, 7), Color.White), RB0 = new Bishop(new Vector2(2, 0), Color.Black), RB1 = new Bishop(new Vector2(5, 0), Color.Black); board.Pieces.Add(RW0); board.Pieces.Add(RW1); board.Pieces.Add(RB0); board.Pieces.Add(RB1); //Test standard move Debug.Assert(RW0.IsMoveValid(board, RW0.Position.AddVector(-1, -1)), "slide up and left"); Debug.Assert(RW0.IsMoveValid(board, RW0.Position.AddVector(-2, -2)), "slide up and left"); Debug.Assert(!RW0.IsMoveValid(board, RW0.Position.AddVector(-3, -3)), "Overshoot edge"); Debug.Assert(RW0.IsMoveValid(board, RW0.Position.AddVector(3, -3)), "Go right instead"); Debug.Assert(RW0.IsMoveValid(board, RW0.Position.AddVector(4, -4)), "Go right instead"); Debug.Assert(RW0.IsMoveValid(board, RW0.Position.AddVector(5, -5)), "Go right instead"); Debug.Assert(!RW0.IsMoveValid(board, RW0.Position.AddVector(6, -6)), "Too far right"); Debug.Assert(!RW0.IsMoveValid(board, RW0.Position.AddVector(-1, -2)), "Not 1:1"); Debug.Assert(!RW0.IsMoveValid(board, RW0.Position.AddVector(-2, -1)), "Not 1:1"); var PB1 = new Pawn(RW0.Position.AddVector(-2, -2), Color.Black); //Create new enemy pawn diagonal to RW0 board.Pieces.Add(PB1); //Attack it Debug.Assert(RW0.IsMoveValid(board, RW0.Position.AddVector(-2, -2)), "Diagonal attack failed"); //Direct attack should pass Debug.Assert(!RW0.IsMoveValid(board, RW0.Position.AddVector(-3, -3)), "Diagonal attack failed"); //Enemy in path should fail Debug.WriteLine("Test: TestBishops, End"); }
internal static void PlayGame() { Debug.WriteLine("Test: PlayGame, Begin"); //Set up board var board = new Board(); IPiece PW0 = new Pawn(new Vector2(0, 6), Color.White), PW1 = new Pawn(new Vector2(1, 6), Color.White), PW2 = new Pawn(new Vector2(2, 6), Color.White), PW3 = new Pawn(new Vector2(3, 6), Color.White), PW4 = new Pawn(new Vector2(4, 6), Color.White), PW5 = new Pawn(new Vector2(5, 6), Color.White), PW6 = new Pawn(new Vector2(6, 6), Color.White), PW7 = new Pawn(new Vector2(7, 6), Color.White), PB0 = new Pawn(new Vector2(0, 1), Color.Black), PB1 = new Pawn(new Vector2(1, 1), Color.Black), PB2 = new Pawn(new Vector2(2, 1), Color.Black), PB3 = new Pawn(new Vector2(3, 1), Color.Black), PB4 = new Pawn(new Vector2(4, 1), Color.Black), PB5 = new Pawn(new Vector2(5, 1), Color.Black), PB6 = new Pawn(new Vector2(6, 1), Color.Black), PB7 = new Pawn(new Vector2(7, 1), Color.Black), KW0 = new Knight(new Vector2(1, 7), Color.White), KW1 = new Knight(new Vector2(6, 7), Color.White), KB0 = new Knight(new Vector2(1, 0), Color.Black), KB1 = new Knight(new Vector2(6, 0), Color.Black), QW = new Queen(new Vector2(3, 7), Color.White), QB = new Queen(new Vector2(3, 0), Color.Black), BW0 = new Bishop(new Vector2(2, 7), Color.White), BW1 = new Bishop(new Vector2(5, 7), Color.White), BB0 = new Bishop(new Vector2(2, 0), Color.Black), BB1 = new Bishop(new Vector2(5, 0), Color.Black), KW = new King(new Vector2(4, 7), Color.White), KB = new King(new Vector2(4, 0), Color.Black); board.Pieces.Add(PW0); board.Pieces.Add(PW1); board.Pieces.Add(PW2); board.Pieces.Add(PW3); board.Pieces.Add(PW4); board.Pieces.Add(PW5); board.Pieces.Add(PW6); board.Pieces.Add(PW7); board.Pieces.Add(PB0); board.Pieces.Add(PB1); board.Pieces.Add(PB2); board.Pieces.Add(PB3); board.Pieces.Add(PB4); board.Pieces.Add(PB5); board.Pieces.Add(PB6); board.Pieces.Add(PB7); board.Pieces.Add(KW0); board.Pieces.Add(KW1); board.Pieces.Add(KB0); board.Pieces.Add(KB1); board.Pieces.Add(QW); board.Pieces.Add(QB); board.Pieces.Add(BW0); board.Pieces.Add(BW1); board.Pieces.Add(BB0); board.Pieces.Add(BB1); board.Pieces.Add(KW); board.Pieces.Add(KB); board.WhiteKing = (King)KW; board.BlackKing = (King)KB; //end Board Setup //Round 1 Debug.Assert(board.MovePiece(PW4, PW4.Position.AddVector(0, -2)), "E4"); Debug.Assert(board.MovePiece(PB4, PB4.Position.AddVector(0, 2)), "E5"); //Round 2 Debug.Assert(board.MovePiece(BW1, BW1.Position.AddVector(-3, -3)), "Bc4"); Debug.Assert(board.MovePiece(BB1, BB1.Position.AddVector(-3, 3)), "Bc5"); //Round 3 Debug.Assert(board.MovePiece(QW, QW.Position.AddVector(4, -4)), "Qh5"); Debug.Assert(board.MovePiece(KB1, KB1.Position.AddVector(-1, 2)), "Nf6"); //Round 4, check established Debug.Assert(board.MovePiece(QW, QW.Position.AddVector(-2, -2)), "Qxf7"); //Round 4, Black player tries various moves to no avail. Debug.Assert(!board.MovePiece(KB, KB.Position.AddVector(-1, -1)), "Check check"); Debug.Assert(!board.MovePiece(KB, KB.Position.AddVector(-1, 1)), "Check check"); Debug.Assert(!board.MovePiece(KB, KB.Position.AddVector(1, 1)), "Check check"); Debug.Assert(!board.MovePiece(KB, KB.Position.AddVector(1, -1)), "Check check"); Debug.Assert(!board.MovePiece(KB, KB.Position.AddVector(0, -1)), "Check check"); Debug.Assert(!board.MovePiece(KB, KB.Position.AddVector(0, 1)), "Check check"); Debug.Assert(!board.MovePiece(KB, KB.Position.AddVector(1, 0)), "Check check"); Debug.Assert(!board.MovePiece(KB, KB.Position.AddVector(-1, 0)), "Check check"); Debug.WriteLine("Test: PlayGame, End"); }
private void SetRegulationBoard() { PieceIndex = new Dictionary <string, IPiece>(); IPiece PW0 = new Pawn(new Vector2(0, 6), Color.White), PW1 = new Pawn(new Vector2(1, 6), Color.White), PW2 = new Pawn(new Vector2(2, 6), Color.White), PW3 = new Pawn(new Vector2(3, 6), Color.White), PW4 = new Pawn(new Vector2(4, 6), Color.White), PW5 = new Pawn(new Vector2(5, 6), Color.White), PW6 = new Pawn(new Vector2(6, 6), Color.White), PW7 = new Pawn(new Vector2(7, 6), Color.White), PB0 = new Pawn(new Vector2(0, 1), Color.Black), PB1 = new Pawn(new Vector2(1, 1), Color.Black), PB2 = new Pawn(new Vector2(2, 1), Color.Black), PB3 = new Pawn(new Vector2(3, 1), Color.Black), PB4 = new Pawn(new Vector2(4, 1), Color.Black), PB5 = new Pawn(new Vector2(5, 1), Color.Black), PB6 = new Pawn(new Vector2(6, 1), Color.Black), PB7 = new Pawn(new Vector2(7, 1), Color.Black), KW0 = new Knight(new Vector2(1, 7), Color.White), KW1 = new Knight(new Vector2(6, 7), Color.White), KB0 = new Knight(new Vector2(1, 0), Color.Black), KB1 = new Knight(new Vector2(6, 0), Color.Black), QW = new Queen(new Vector2(3, 7), Color.White), QB = new Queen(new Vector2(3, 0), Color.Black), BW0 = new Bishop(new Vector2(2, 7), Color.White), BW1 = new Bishop(new Vector2(5, 7), Color.White), BB0 = new Bishop(new Vector2(2, 0), Color.Black), BB1 = new Bishop(new Vector2(5, 0), Color.Black), KW = new King(new Vector2(4, 7), Color.White), KB = new King(new Vector2(4, 0), Color.Black); PieceIndex.Add("PW0", PW0); PieceIndex.Add("PW1", PW1); PieceIndex.Add("PW2", PW2); PieceIndex.Add("PW3", PW3); PieceIndex.Add("PW4", PW4); PieceIndex.Add("PW5", PW5); PieceIndex.Add("PW6", PW6); PieceIndex.Add("PW7", PW7); PieceIndex.Add("PB0", PB0); PieceIndex.Add("PB1", PB1); PieceIndex.Add("PB2", PB2); PieceIndex.Add("PB3", PB3); PieceIndex.Add("PB4", PB4); PieceIndex.Add("PB5", PB5); PieceIndex.Add("PB6", PB6); PieceIndex.Add("PB7", PB7); PieceIndex.Add("KW0", KW0); PieceIndex.Add("KW1", KW1); PieceIndex.Add("KB0", KB0); PieceIndex.Add("KB1", KB1); PieceIndex.Add("QW", QW); PieceIndex.Add("QB", QB); PieceIndex.Add("BW0", BW0); PieceIndex.Add("BW1", BW1); PieceIndex.Add("BB0", BB0); PieceIndex.Add("BB1", BB1); PieceIndex.Add("KW", KW); PieceIndex.Add("KB", KB); Pieces.Add(PW0); Pieces.Add(PW1); Pieces.Add(PW2); Pieces.Add(PW3); Pieces.Add(PW4); Pieces.Add(PW5); Pieces.Add(PW6); Pieces.Add(PW7); Pieces.Add(PB0); Pieces.Add(PB1); Pieces.Add(PB2); Pieces.Add(PB3); Pieces.Add(PB4); Pieces.Add(PB5); Pieces.Add(PB6); Pieces.Add(PB7); Pieces.Add(KW0); Pieces.Add(KW1); Pieces.Add(KB0); Pieces.Add(KB1); Pieces.Add(QW); Pieces.Add(QB); Pieces.Add(BW0); Pieces.Add(BW1); Pieces.Add(BB0); Pieces.Add(BB1); Pieces.Add(KW); Pieces.Add(KB); WhiteKing = (King)KW; BlackKing = (King)KB; //end Board Setup }