예제 #1
0
    public void Neighbor_TryGetNeighbor_Self()
    {
        FastIndex hex = Indexes.A1;

        hex.TryGetNeighbor(HexNeighborDirection.Up, out hex);
        Assert.AreEqual(Indexes.A2, hex);
    }
예제 #2
0
 static IEnumerable <FastIndex> GenerateAllPossibleKingMoves(FastIndex start)
 {
     foreach (var direction in AllDirections)
     {
         if (start.TryGetNeighbor(direction, out var neighbor))
         {
             yield return(neighbor);
         }
     }
 }
예제 #3
0
    public static void AddAllPossiblePawnMoves(List <FastMove> moves, FastIndex start, Team team, FastBoardNode boardNode, bool generateQuiet = true)
    {
        Team enemy = team.Enemy();

        HexNeighborDirection leftAttackDir   = HexNeighborDirection.UpLeft;
        HexNeighborDirection leftPassantDir  = HexNeighborDirection.DownLeft;
        HexNeighborDirection rightAttackDir  = HexNeighborDirection.UpRight;
        HexNeighborDirection rightPassantDir = HexNeighborDirection.DownRight;
        HexNeighborDirection forwardDir      = HexNeighborDirection.Up;

        if (team != Team.White)
        {
            leftAttackDir   = HexNeighborDirection.DownLeft;
            leftPassantDir  = HexNeighborDirection.UpLeft;
            rightAttackDir  = HexNeighborDirection.DownRight;
            rightPassantDir = HexNeighborDirection.UpRight;
            forwardDir      = HexNeighborDirection.Down;
        }

        if (start.TryGetNeighbor(leftAttackDir, out var leftAttack))
        {
            var leftVictim = boardNode[leftAttack];
            if (leftVictim.team == enemy)
            {
                moves.Add(new FastMove(start, leftAttack, MoveType.Attack));
            }
            if (leftVictim.team == Team.None)
            {
                if (start.TryGetNeighbor(leftPassantDir, out var leftPassant) && boardNode.PassantableIndex == leftPassant)
                {
                    moves.Add(new FastMove(start, leftAttack, MoveType.EnPassant));
                }
            }
        }

        if (start.TryGetNeighbor(rightAttackDir, out var rightAttack))
        {
            var rightVictim = boardNode[rightAttack];
            if (rightVictim.team == enemy)
            {
                moves.Add(new FastMove(start, rightAttack, MoveType.Attack));
            }
            if (rightVictim.team == Team.None)
            {
                if (start.TryGetNeighbor(rightPassantDir, out var rightPassant) && boardNode.PassantableIndex == rightPassant)
                {
                    moves.Add(new FastMove(start, rightAttack, MoveType.EnPassant));
                }
            }
        }

        if (!generateQuiet)
        {
            return;
        }

        // One forward
        bool hasForward = start.TryGetNeighbor(forwardDir, out var forward);

        if (hasForward && !boardNode.IsOccupied(forward))
        {
            if (forward.TryGetNeighbor(forwardDir, out var twoForward))
            {
                moves.Add(new FastMove(start, forward, MoveType.Move));

                // Two forward on 1st move
                if (PawnIsAtStart(team, (Index)start) && !boardNode.IsOccupied(twoForward))
                {
                    moves.Add(new FastMove(start, twoForward, MoveType.Move));
                }
            }
            else
            {
                // if two squares forward doesn't exist, that means we're on the last rank for this pawn.
                moves.Add(new FastMove(start, forward, MoveType.Move, FastPiece.Queen));
                moves.Add(new FastMove(start, forward, MoveType.Move, FastPiece.Rook));
                moves.Add(new FastMove(start, forward, MoveType.Move, FastPiece.Knight));
                moves.Add(new FastMove(start, forward, MoveType.Move, FastPiece.Squire));
            }
        }
    }