Ejemplo n.º 1
0
 public bool Equals(Move m2)
 {
     return(card == m2.card && from == m2.from && to == m2.to);
 }
Ejemplo n.º 2
0
        public void AddValidMoves(List <Move> outMoves, bool winAndCaptureOnly = false)
        {
            if (board.TopWon() || board.BottomWon())
            {
                return;
            }

            var ownMaster        = board.GetBitboard(Piece.Master, player);
            var ownStudents      = board.GetBitboard(Piece.Student, player);
            var opponentMaster   = board.GetBitboard(Piece.Master, player.Opponent());
            var opponentStudents = board.GetBitboard(Piece.Student, player.Opponent());

            byte card1, card2;

            if (player == Player.Top)
            {
                card1 = cards.topCard1;
                card2 = cards.topCard2;
            }
            else
            {
                card1 = cards.bottomCard1;
                card2 = cards.bottomCard2;
            }

            int fromBit = 1;

            for (byte from = 0; from < 25; from++, fromBit <<= 1)
            {
                // Skip if we don't have a piece here

                if (((ownMaster | ownStudents) & fromBit) == 0)
                {
                    continue;
                }

                // Check moves from both cards

                for (int cardIndex = 0; cardIndex < 2; cardIndex++)
                {
                    var card = cardIndex == 0 ? card1 : card2;

                    var destinations = Card.Definitions[card].destinations[(int)player, from];
                    var goalGate     = player == Player.Top ? Board.BottomGateBits : Board.TopGateBits;

                    for (int i = 0; i < destinations.Length; i++)
                    {
                        var dest    = destinations[i];
                        var destBit = 1 << dest;

                        // Can't capture self

                        if (((ownMaster | ownStudents) & destBit) != 0)
                        {
                            continue;
                        }

                        // Check move quality

                        var quality = MoveQuality.Normal;

                        if ((opponentMaster & destBit) != 0 || (destBit == goalGate && (ownMaster & fromBit) != 0))
                        {
                            quality = MoveQuality.Win;
                        }
                        else if ((opponentStudents & destBit) != 0)
                        {
                            quality = MoveQuality.Capture;
                        }

                        if (winAndCaptureOnly && quality == MoveQuality.Normal)
                        {
                            continue;
                        }

                        var move = new Move(card, from, dest, quality);
                        outMoves.Add(move);
                    }
                }
            }

            // Order moves by quality, in place!

            int currentIndex = 0;

            for (int i = 0; i < outMoves.Count; i++)
            {
                if (outMoves[i].quality == (byte)MoveQuality.Win)
                {
                    var tmp = outMoves[currentIndex];
                    outMoves[currentIndex] = outMoves[i];
                    outMoves[i]            = tmp;

                    currentIndex++;
                }
            }

            for (int i = currentIndex; i < outMoves.Count; i++)
            {
                if (outMoves[i].quality == (byte)MoveQuality.Capture)
                {
                    var tmp = outMoves[currentIndex];
                    outMoves[currentIndex] = outMoves[i];
                    outMoves[i]            = tmp;

                    currentIndex++;
                }
            }
        }