public void AddPieceTest() { var pieceValue = new PieceValue(); var board = new Board(); var position = new Position(board, pieceValue); position.AddPiece(Pieces.WhiteKing, Squares.a7); var pieces = position.Pieces(); var square = pieces.Lsb(); Assert.Equal(Squares.a7, square.Value); var piece = position.GetPiece(square); Assert.Equal(Pieces.WhiteKing, piece.Value); // test overload pieces = position.Pieces(piece); Assert.False((pieces & square).IsEmpty); // Test piece type overload board = new Board(); position = new Position(board, pieceValue); position.AddPiece(PieceTypes.Knight.MakePiece(Player.Black), Squares.d5); pieces = position.Pieces(); square = pieces.Lsb(); Assert.Equal(Squares.d5, square.Value); piece = position.GetPiece(square); Assert.Equal(Pieces.BlackKnight, piece.Value); }
private int PieceValueToInt(PieceValue value) { int numberValue = 1; switch (value) { case PieceValue.One: numberValue = 1; break; case PieceValue.Two: numberValue = 2; break; case PieceValue.Three: numberValue = 3; break; case PieceValue.Four: numberValue = 4; break; default: Debug.Log("Piece Value out of range"); break; } return(numberValue); }
public void FoolsCheckMateTest() { // generate moves var moves = new List <Move>(4) { Move.Create(Squares.f2, Squares.f3), Move.Create(Squares.e7, Squares.e5), Move.Create(Squares.g2, Squares.g4), Move.Create(Squares.d8, Squares.h4) }; // construct game and start a new game var board = new Board(); var pieceValue = new PieceValue(); var position = new Position(board, pieceValue); var game = GameFactory.Create(position); game.NewGame(); var state = new State(); // make the moves necessary to create a mate foreach (var move in moves) { position.MakeMove(move, state); } // verify in check is actually true Assert.True(position.InCheck); var resultingMoves = position.GenerateMoves(); // verify that no legal moves actually exists. Assert.True(!resultingMoves.Any()); }
// test for piece occupying position internal bool IsOccupied(PlayerKinds kind, PieceValue piece, PositionValue position) { return(IsPiece(piece, position) && (kind == PlayerKinds.Friend ? IsFriend(position) : kind == PlayerKinds.Enemy ? IsEnemy(position) : true)); }
// create all drops and moves for piece, position and board // checks movetype restriction if supplied, but Any means any // CHECK: I thought this was the way it used to be??? // checks position or zone restriction by peeking into movegen internal IEnumerable <MoveModel> CreateMoves(MoveKinds kind, PieceValue piece, PositionValue position, BoardModel board, MoveTypeValue movetype) { var movegens = (movetype == MoveTypeValue.Any) ? _movegens.Values.SelectMany(m => m) : _movegens.SafeLookup(movetype); if (movegens == null) { yield break; } foreach (var movegen in movegens) { var ok = (kind == MoveKinds.Move || movegen.PosOrZone.IsNull); if (!ok) { var posorzone = movegen.PosOrZone.Value as PositionOrZone; ok = (posorzone.IsPosition) ? (position == posorzone.Position) : board.InZone(posorzone.Zone, position); } if (ok) { Logger.WriteLine(4, "CreateMoves {0} piece:{1} position:{2} movetype:{3}", kind, piece, position, movetype); foreach (var move in movegen.Exec(kind, piece, position, board)) { yield return(move); } } } // TODO: after each Exec if the partial flag is set, add any possible further moves // (perhaps of a specific move type) }
// main entry point for generating moves for player, piece and position // note that a gencode block can contain multiple sections and generate multiple moves for the position // CHECK: the state block persists and must be reinitialised internal IList <MoveModel> Exec(MoveKinds kind, PieceValue piece, PositionValue position, BoardModel board) { _board = board; _state = MoveGenState.Create(kind, piece, position, board); EvalExec(); return(_state.MoveList); }
// true if any such piece in zone internal bool IsOccupied(PlayerKinds kind, PieceValue piece, ZoneValue zone) { if (zone == null) { return(PositionIter().Any(p => IsOccupied(kind, piece, p))); } return(Def.PositionIter(zone, CurrentPlayer).Any(p => IsOccupied(kind, piece, p))); }
// count pieces remaining for player and type internal int PiecesCount(PlayerKinds kind, PieceValue piece) { return(_playedpieces.Where(p => piece == null || p.Value.Piece == piece) .Where(p => kind == PlayerKinds.Any || (kind == PlayerKinds.Friend && p.Value.Player == CurrentPlayer) || (kind == PlayerKinds.Enemy && p.Value.Player != CurrentPlayer)) .Count()); }
internal static PolyOffStore Create(PlayerValue player, PieceValue piece, int quantity) { return(new PolyOffStore { Player = player.Value, Piece = piece.Value, Quantity = quantity, }); }
internal static PolyPieceImage Create(PieceValue piece, PlayerValue player, IList <TextValue> image) { return(new PolyPieceImage { Piece = piece.Value, Player = player.Value, Images = image.Select(i => i.Value).ToList(), }); }
internal static MoveGenState Create(MoveKinds kind, PieceValue piece, PositionValue position, BoardModel board) { //internal static MoveGenState Create(MoveKinds kind, PlayerValue turnplayer, PlayerValue asplayer, PieceValue piece, PositionValue position) { return(new MoveGenState { _board = board, Kind = kind, Position = position, Piece = piece, Current = position, Mark = position, }.Reset()); }
public void Setup() { var board = new Board(); var pieceValue = new PieceValue(); var pos = new Position(board, pieceValue); _game = new Game(pos); }
public void SetFenTest() { var board = new Board(); var pieceValue = new PieceValue(); var pos = new Position(board, pieceValue); var game = GameFactory.Create(pos); game.NewGame(); }
public void GetFenTest() { var board = new Board(); var pieceValue = new PieceValue(); var pos = new Position(board, pieceValue); var game = GameFactory.Create(pos); game.NewGame(); var expectedFen = new FenData(Fen.StartPositionFen); var actualFen = game.GetFen(); actualFen.Should().Be(expectedFen); }
// Combine two piece lookups into a list, adding each to the symbol table IList <Node> GetPieceList(Dictionary <string, Node> variantpiecelookup) { var piecelist = new List <Node>(); foreach (var name in _gpiecelookup.Keys) { piecelist.Add(variantpiecelookup.ContainsKey(name) ? variantpiecelookup[name] : _gpiecelookup[name]); _symbols.DefineValue(name, PieceValue.Create(name)); } foreach (var name in variantpiecelookup.Keys) { if (!_gpiecelookup.ContainsKey(name)) { piecelist.Add(variantpiecelookup[name]); _symbols.DefineValue(name, PieceValue.Create(name)); } } return(piecelist); }
void AddPieceMove(PieceValue piece) { Logger.WriteLine(4, "Add moves piece:{0} position:{1} from:{2} changes:{3} captures:{4}", piece, To ?? Current, From, _changes.Count, _captures.Count); var basemovepart = CreateMovePart(piece); if (!(basemovepart.Kind == MoveKinds.Drop && _board.OffStoreCount(basemovepart.Player, basemovepart.Piece) <= 0)) { AddMovePart(basemovepart); foreach (var movepart in _changes) { AddMovePart(movepart); } foreach (var movepart in _captures) { AddMovePart(movepart); } MoveList.Add(_move); } Reset(); }
public void InCheckMoveGenerationTest() { const string fen = "rnbqkbnr/1ppQpppp/p2p4/8/8/2P5/PP1PPPPP/RNB1KBNR b KQkq - 1 6"; const int expectedMoves = 4; var board = new Board(); var pieceValue = new PieceValue(); var pos = new Position(board, pieceValue); var fd = new FenData(fen); pos.SetFen(fd); // make sure black is in check Assert.True(pos.InCheck); // generate moves for black var mg = pos.GenerateMoves(); var actualMoves = mg.Length; Assert.Equal(expectedMoves, actualMoves); }
public void RookBorderBlocked() { /* * Test purpose : Testing blocked bishop attacks */ BitBoard border = 0xff818181818181ff; BitBoard borderInner = 0x7e424242427e00; BitBoard corners = 0x8100000000000081; const int expectedCorner = 14; const int expectedSide = 8; // 7 to each side and 1 blocked /* * borderInner (X = set bit) : * * 0 0 0 0 0 0 0 0 * 0 X X X X X X 0 * 0 X 0 0 0 0 X 0 * 0 X 0 0 0 0 X 0 * 0 X 0 0 0 0 X 0 * 0 X 0 0 0 0 X 0 * 0 X X X X X X 0 * 0 0 0 0 0 0 0 0 * */ // just to get the attacks var board = new Board(); var pieceValue = new PieceValue(); var pos = new Position(board, pieceValue); foreach (var square in border) { var attacks = pos.GetAttacks(square, PieceTypes.Rook, borderInner); attacks.IsEmpty.Should().BeFalse(); var expected = corners & square ? expectedCorner : expectedSide; var actual = attacks.Count; actual.Should().Be(expected); } }
public void FanRankFileAmbiguationPositiveTest() { // Tests both knights moving to same square for Rank ambiguity const string fen = "8/6k1/8/8/8/8/1K1N1N2/8 w - - 0 1"; const MoveNotations notation = MoveNotations.Fan; var movingPiece = new Piece(Pieces.WhiteKnight); var fromOneSquare = new Square(Squares.d2); var fromTwoSquare = new Square(Squares.f2); var toSquare = new Square(Squares.e4); var uniChar = movingPiece.GetUnicodeChar(); var toSquareString = toSquare.ToString(); var expectedPrimary = $"{uniChar}{fromOneSquare.FileChar}{toSquareString}"; var expectedSecondary = $"{uniChar}{fromTwoSquare.FileChar}{toSquareString}"; var board = new Board(); var pieceValue = new PieceValue(); var pos = new Position(board, pieceValue); var g = GameFactory.Create(pos); g.NewGame(fen); var w1 = new Move(fromOneSquare, toSquare); var w2 = new Move(fromTwoSquare, toSquare); var ambiguity = new MoveAmbiguity(pos); var actualPrimary = ambiguity.ToNotation(w1, notation); var actualSecondary = ambiguity.ToNotation(w2, notation); Assert.Equal(expectedPrimary, actualPrimary); Assert.Equal(expectedSecondary, actualSecondary); }
internal MovePartModel CreateMovePart(PieceValue piece) { return((Kind == MoveKinds.Move || Kind == MoveKinds.Copy) ? MovePartModel.Create(Kind, Player, From, piece ?? Piece, To ?? Current) : MovePartModel.Create(Kind, Player, To ?? Current, piece ?? Piece, null)); }
// Create any kind of move except set attribute internal static MovePartModel Create(MoveKinds kind, PlayerValue player, PositionValue position, PieceValue piece, PositionValue final = null) { return(new MovePartModel { Kind = kind, Player = player, Piece = piece, Position = position, Final = final, }); }
// factory: parts default to empty internal static MoveModel Create(PlayerValue player, PositionValue position, PieceValue piece) { return(new MoveModel { Player = player, Position = position, Piece = piece, }); }
// count pieces remaining for player internal int PiecesCount(PlayerValue player, PieceValue piece = null) { return(_playedpieces.Where(p => piece == null || p.Value.Piece == piece) .Where(p => p.Value.Player == player) .Count()); }
internal void AddChangePiece(PositionValue position, PieceValue piece) { _changes.Add(MovePartModel.Create(MoveKinds.Piece, Player, position, piece)); }
// Create set attribute move internal static MovePartModel CreateSetAttribute(PlayerValue player, PositionValue position, PieceValue piece, IdentValue ident, TypedValue value) { var mpm = Create(MoveKinds.Attrib, player, position, piece); mpm.Attribute = ident; mpm.Value = value; return(mpm); }
internal bool IsPiece(PieceValue piece, PositionValue position) { return(IsPlayed(position) && _playedpieces[position].Piece == piece); }
internal void IncOffStore(PlayerValue player, PieceValue piece, int increment) { var pair = Pair.Create(player, piece); OffStoreLookup[pair] += increment; }
internal int OffStoreCount(PlayerValue player, PieceValue piece) { var pair = Pair.Create(player, piece); return(OffStoreLookup.SafeLookup(pair)); // conveniently zero if not found }
// create a piece for a player internal PieceModel CreatePiece(PlayerValue player, PieceValue piece) { return(PieceModel.Create(player, Def.PieceLookup[piece])); }
internal void AddDrop(PlayerValue player, PositionValue position, PieceValue piece) { _changes.Add(MovePartModel.Create(MoveKinds.Drop, player, position, piece)); }