public void KnightNot() { var from = new Position(3, 3); var s = new ChessState(); var m = new KnightMoveTemplate(); Assert.IsFalse(m.CanApply(from, new Position(2, 2), s)); }
public void KnightUpRight() { var from = new Position(3, 3); var s = new ChessState(); var m = new KnightMoveTemplate(); Assert.IsTrue(m.CanApply(from, new Position(4, 5), s)); Assert.IsTrue(m.CanApply(from, new Position(5, 4), s)); }
public void KnightDownLeft() { var from = new Position(3, 3); var s = new ChessState(); var m = new KnightMoveTemplate(); Assert.IsTrue(m.CanApply(from, new Position(2, 1), s)); Assert.IsTrue(m.CanApply(from, new Position(1, 2), s)); }
public void KnightCanSkip() { var from = new Position(3, 3); var s = new ChessState(); s[new Position(3, 4)] = new ChessPiece(); var m = new KnightMoveTemplate(); Assert.IsTrue(m.CanApply(from, new Position(4, 5), s)); }
private static Position EnemyPawn(ChessState state) { switch (state.EnPassant.Rank) { case 3: return new Position(state.EnPassant.File, state.EnPassant.Rank + 1); case 6: return new Position(state.EnPassant.File, state.EnPassant.Rank - 1); } throw new ArgumentException(); }
public void PromotionWrongType() { var from = new Position("a7"); var s = new ChessState(); var piece = new ChessPiece { Player = new Player(1) }; s[from] = piece; var m = new Promotion(new PawnMoveTemplate(), new[] { PieceTypes.Knight, PieceTypes.Bishop, PieceTypes.Rook, PieceTypes.Queen }); Assert.IsFalse(m.CanApply(from, new Position("a8"), s, PieceTypes.Pawn)); }
public void GetPossibleMove() { var pos = new Position(1, 1); var s = new ChessState(); var p = new ChessPiece(); p.Move(new Position(2, 2)); p.PossibleMoves.Add(new MockTemplate()); Assert.IsNotNull(p.GetPossibleMove(pos, s, PieceTypes.Pawn)); }
public void CanNotAttack() { var pos = new Position(1, 1); var s = new ChessState(); var p = new ChessPiece(); p.Move(new Position(2, 2)); p.PossibleMoves.Add(new MockTemplate(null, false)); Assert.IsFalse(p.CanAttack(pos, s)); }
public void IsUnderAttackEmpty() { var s = new ChessState(); var players = new List<Player> { new Player(1), new Player(2) }; var from = new Position(1, 1); var to = new Position(2, 2); s[from] = ChessPieceFactory.CreatePiece('K', new EnglishFENPresentation(), players); s[from].Move(from); Assert.IsTrue(s.IsUnderAttack(to, players[1])); }
public void EnPassantFar() { var from = new Position("c5"); var to = new Position("a3"); var s = new ChessState(); s.EnPassant = to; s[from] = new ChessPiece { Player = new Player(2) }; var m = new EnPassantTemplate(); Assert.IsFalse(m.CanApply(from, to, s)); }
public void NoPossibleMoves() { var s = new ChessState(); var from = new Position(1, 1); var to = new Position(1, 3); var p = new ChessPiece(); s[from] = p; s[from].Move(from); p.PossibleMoves.Add(new MockTemplate(1)); Assert.IsNull(p.GetPossibleMove(to, s, PieceTypes.Pawn)); }
public void IsKingOpen() { var s = new ChessState(); var players = new List<Player> {new Player(1), new Player(2)}; var from = new Position(1, 1); var to = new Position(3, 3); s[from] = ChessPieceFactory.CreatePiece('K', new EnglishFENPresentation(), players); s[to] = ChessPieceFactory.CreatePiece('q', new EnglishFENPresentation(), players); s[from].Move(from); s[to].Move(to); Assert.IsTrue(s.IsKingOpen(players[0])); }
public ChessState(ChessState o) : this() { Player = o.Player; Castling = o.Castling; EnPassant = o.EnPassant; HalfMoves = o.HalfMoves; FullMoves = o.FullMoves; foreach (ChessPiece p in o) Board[p.Pos.File - 1, p.Pos.Rank - 1] = new ChessPiece(p); }
public void PromotionApply() { var from = new Position("a7"); var to = new Position("a8"); var piece = new ChessPiece { Type = PieceTypes.Pawn, Player = new Player(1) }; var s = new ChessState(); s[from] = piece; var m = new Promotion(new PawnMoveTemplate(), new[] { PieceTypes.Knight, PieceTypes.Bishop, PieceTypes.Rook, PieceTypes.Queen }); var concretePromotion = m.Concretize(from, to, PieceTypes.Queen); concretePromotion.Apply(s); Assert.AreEqual(PieceTypes.Queen, piece.Type); }
public void CanAttack() { var s = new ChessState(); var from = new Position(1, 1); var to = new Position(2, 2); var p = new ChessPiece(); s[from] = p; s[from].Move(from); s[to] = new ChessPiece(); s[to].Move(to); s[from].PossibleMoves.Add(new MockTemplate(null, true)); Assert.IsTrue(p.CanAttack(to, s)); }
public void CastlingKingside() { var c = new CastlingTemplate(CastlingTemplate.CastlingType.Kingside); var from = new Position(5, 1); var to = new Position(7, 1); var rook = new Position(8, 1); var s = new ChessState(); s[rook] = new ChessPiece {Type = PieceTypes.Rook}; s[rook].Move(rook); s[from] = new ChessPiece {Type = PieceTypes.King}; s[from].Move(from); Assert.IsTrue(c.CanApply(from, to, s)); }
protected virtual IConcreteMove Solve(ChessState state) { if (CastlingKingside) { From = new Position(5, Player.Order == 1 ? 1 : 8); To = new Position(7, Player.Order == 1 ? 1 : 8); } if (CastlingQueenside) { From = new Position(5, Player.Order == 1 ? 1 : 8); To = new Position(3, Player.Order == 1 ? 1 : 8); } Position from = null; var possibleMoves = new List<IConcreteMove>(); foreach (ChessPiece p in state) if (!p.IsEmpty && p.Type == PieceType && p.Player == Player && From.Equals(p.Pos)) { var mv = p.GetPossibleMove(To, state, PromotionTo); if (mv != null) { mv.Apply(state); if (!state.IsKingOpen(Player)) { from = p.Pos; possibleMoves.Add(mv); } mv.Rollback(state); } } if (possibleMoves.Count != 1) { throw new Exception("Ambiguous move."); } else { From = from; return possibleMoves[0]; } }
public void EnPassantCapture() { var enmy = new Position("a4"); var from = new Position("b4"); var to = new Position("a3"); var s = new ChessState(); s.EnPassant = to; s[from] = new ChessPiece { Player = new Player(2) }; s[from].Move(from); s[enmy] = new ChessPiece(); s[enmy].Move(enmy); var m = new EnPassantConcrete(from, to); Assert.IsFalse(s[enmy].IsEmpty); m.Apply(s); Assert.IsTrue(s[enmy].IsEmpty); }
public void CastlingBarrierQ() { var c = new CastlingTemplate(CastlingTemplate.CastlingType.Queenside); var from = new Position(5, 1); var to = new Position(3, 1); var rook = new Position(1, 1); var enmy = new Position(2, 1); var white = new Player(1); var s = new ChessState(); s[rook] = new ChessPiece { Type = PieceTypes.Rook, Player = white }; s[rook].Move(rook); s[from] = new ChessPiece { Type = PieceTypes.King, Player = white }; s[from].Move(from); s[enmy] = new ChessPiece { Type = PieceTypes.Rook, Player = white }; s[enmy].Move(enmy); Assert.IsFalse(c.CanApply(from, to, s)); }
public void EnPassantSetting() { var from = new Position("a2"); var to = new Position("a4"); var s = new ChessState(); s[from] = new ChessPiece { Player = new Player(1) }; var m = new PawnConcreteMove(from, to); m.Apply(s); Assert.AreEqual(new Position("a3"), s.EnPassant); }
public void PawnCaptureLeft() { var from = new Position("b3"); var to = new Position("a4"); var s = new ChessState(); s[from] = new ChessPiece { Player = new Player(1) }; s[to] = new ChessPiece(); var m = new PawnCaptureTemplate(); Assert.IsTrue(m.CanApply(from, to, s)); }
public void SwitchPlayersTest() { var s = new ChessState(); Assert.AreEqual('w', s.Player); s.SwitchPlayers(); Assert.AreEqual('b', s.Player); s.SwitchPlayers(); Assert.AreEqual('w', s.Player); }
public void NewEmptyPieceTest() { var s = new ChessState(); var pos = new Position(1, 1); s[pos] = new ChessPiece(); s.NewEmptyPiece(pos); Assert.IsTrue(s[pos].IsEmpty); }
public ChessMove FindBestMove(ChessState state) { _BestMove = null; Recheck = false; Error = false; if (_Path == null) { Error = true; return null; } if (P != null && !P.HasExited) P.Kill(); while (Worker.IsAlive) Worker.Abort(); Worker = new Thread(StartProcess); Worker.Start(); GameState = state; return null; }
private void SetPosition(Process engine, ChessState gameState) { if (gameState != null) engine.StandardInput.WriteLine("position fen " + FENParser.Generate(gameState)); }
private Boolean IsKingsPathFree(Position from, Position to, ChessState cState) { Boolean result = true; switch (CType) { case CastlingType.Kingside: for (int kingPathFile = from.File + 1; kingPathFile <= to.File; ++kingPathFile) result &= !cState.IsUnderAttack(new Position(kingPathFile, from.Rank), cState[from].Player); break; case CastlingType.Queenside: for (int kingPathFile = from.File - 1; kingPathFile >= to.File; --kingPathFile) result &= !cState.IsUnderAttack(new Position(kingPathFile, from.Rank), cState[from].Player); break; } return result; }
public void RookPathUnderAttack() { var c = new CastlingTemplate(CastlingTemplate.CastlingType.Kingside); var from = new Position(5, 1); var to = new Position(7, 1); var rook = new Position(8, 1); var enmy = new Position(8, 3); var white = new Player(1); var s = new ChessState(); s[rook] = new ChessPiece { Type = PieceTypes.Rook, Player = white }; s[rook].Move(rook); s[from] = new ChessPiece { Type = PieceTypes.King, Player = white }; s[from].Move(from); s[enmy] = new ChessPiece { Type = PieceTypes.Rook, Player = new Player(2) }; s[enmy].Move(enmy); ChessPieceFactory.AddMoves((ChessPiece)s[enmy]); Assert.IsTrue(c.CanApply(from, to, s)); }
public void MakeMove() { state = null; Game.MakeMove(); OnPropertyChanged("FEN"); }
public void UndoMove() { state = null; Game.UndoMove(); OnPropertyChanged("FEN"); }
public void PawnUp() { var from = new Position("a2"); var to = new Position("a3"); var s = new ChessState(); s[from] = new ChessPiece {Player = new Player(1)}; var m = new PawnMoveTemplate(); Assert.IsTrue(m.CanApply(from,to,s)); }