コード例 #1
0
 public void Card()
 {
     bytes.push(LiteralFactory.CreateCardLiteral("my-card-guid"));
     Assert.AreEqual(
         bytes.ReportStackContent(),
         "CARD(my-card-guid) "
         );
 }
コード例 #2
0
 public void CardChoiceCallback(Card chosenCard)
 {
     if (chosenCard != null)
     {
         List <byte> card = LiteralFactory.CreateCardLiteral(chosenCard.GetID());
         GM.AddToStack(card);
     }
     next();
 }
コード例 #3
0
        public void CardBytecode()
        {
            Card card1 = new TestCard();
            Card card2 = new TestCard();
            Card card3 = new TestCard();

            bytes.push(LiteralFactory.CreateCardLiteral(card1.GetID()));
            bytes.push(LiteralFactory.CreateCardLiteral(card2.GetID()));
            bytes.push(LiteralFactory.CreateCardLiteral(card3.GetID()));

            Assert.AreEqual(card3.GetID(), bytes.ReadCardLiteral(dummyCallback));
            Assert.AreEqual(card2.GetID(), bytes.ReadCardLiteral(dummyCallback));
            Assert.AreEqual(card1.GetID(), bytes.ReadCardLiteral(dummyCallback));
        }
コード例 #4
0
        public void MoveToDeck()
        {
            game.Cards = new CardManager();

            Card c1 = new TestCard();
            Card c2 = new TestCard();
            Card c3 = new TestCard();
            Card c4 = new TestCard();

            game.Cards.Discard.AddCard(c1);
            game.Cards.Discard.AddCard(c2);
            game.Cards.Discard.AddCard(c3);
            game.Cards.Discard.AddCard(c4);

            bytes.push(InstructionFactory.Make_MoveToDeck(
                           LiteralFactory.CreateCardLiteral(c1.GetID()),
                           new List <byte> {
                (byte)DeckLocation.TOP
            }
                           ));
            game.ExecuteNext();
            Assert.AreSame(game.Cards.Deck.GetCard(DeckLocation.TOP), c1);

            bytes.push(InstructionFactory.Make_MoveToDeck(
                           LiteralFactory.CreateCardLiteral(c2.GetID()),
                           new List <byte> {
                (byte)DeckLocation.BOTTOM
            }
                           ));
            game.ExecuteNext();
            Assert.AreSame(game.Cards.Deck.GetCard(DeckLocation.BOTTOM), c2);

            bytes.push(InstructionFactory.Make_MoveToDeck(
                           LiteralFactory.CreateCardLiteral(c3.GetID()),
                           new List <byte> {
                (byte)DeckLocation.TOP
            }
                           ));
            game.ExecuteNext();
            Assert.AreSame(game.Cards.Deck.GetCard(DeckLocation.TOP), c3);

            bytes.push(InstructionFactory.Make_MoveToDeck(
                           LiteralFactory.CreateCardLiteral(c4.GetID()),
                           new List <byte> {
                (byte)DeckLocation.SHUFFLE
            }
                           ));
            game.ExecuteNext();
            Assert.IsNotNull(game.Cards.Deck.GetCard(c4.GetID()));
        }
コード例 #5
0
        public void MoveToDiscard()
        {
            game.Cards = new CardManager();

            Card c1 = new TestCard();
            Card c2 = new TestCard();
            Card c3 = new TestCard();
            Card c4 = new TestCard();

            game.Cards.Deck.AddCard(c1);
            game.Cards.Deck.AddCard(c2);
            game.Cards.Deck.AddCard(c3);
            game.Cards.Deck.AddCard(c4);

            bytes.push(InstructionFactory.Make_MoveToDiscard(
                           LiteralFactory.CreateCardLiteral(c1.GetID())
                           ));
            game.ExecuteNext();
            Assert.IsNotNull(game.Cards.Discard.GetCard(c1.GetID()));

            bytes.push(InstructionFactory.Make_MoveToDiscard(
                           LiteralFactory.CreateCardLiteral(c2.GetID())
                           ));
            game.ExecuteNext();
            Assert.IsNotNull(game.Cards.Discard.GetCard(c2.GetID()));

            bytes.push(InstructionFactory.Make_MoveToDiscard(
                           LiteralFactory.CreateCardLiteral(c3.GetID())
                           ));
            game.ExecuteNext();
            Assert.IsNotNull(game.Cards.Discard.GetCard(c3.GetID()));

            bytes.push(InstructionFactory.Make_MoveToDiscard(
                           LiteralFactory.CreateCardLiteral(c4.GetID())
                           ));
            game.ExecuteNext();
            Assert.IsNotNull(game.Cards.Discard.GetCard(c4.GetID()));
        }
コード例 #6
0
    public void executeNext()
    {
        Instruction next = this.next();

        try {
            switch (next)
            {
            // FUNCTIONS

            case Instruction.RANDOM_NUMBER: {
                int upperBound = ReadIntLiteral(skipToNext);
                push(LiteralFactory.CreateIntLiteral(UnityEngine.Random.Range(1, upperBound)));
                break;
            }

            case Instruction.ADD: {
                int a = ReadIntLiteral(skipToNext);
                int b = ReadIntLiteral(skipToNext);
                push(LiteralFactory.CreateIntLiteral(a + b));
                break;
            }

            case Instruction.IF: {
                int         controlID  = ReadIntLiteral(skipToNext);
                Condition   condition  = ReadConditionLiteral(skipToNext);
                List <byte> blockBytes = new List <byte>();

                while (HasBytes())
                {
                    byte b = pop();
                    if (b == (byte)Instruction.ENDIF)
                    {
                        int id = ReadIntLiteral(skipToNext);
                        if (id == controlID)
                        {
                            break;
                        }
                        else
                        {
                            blockBytes.Insert(0, (byte)Instruction.ENDIF);
                            blockBytes.InsertRange(0, LiteralFactory.CreateIntLiteral(id));
                        }
                    }
                    else
                    {
                        blockBytes.Insert(0, b);
                    }
                }
                if (condition.Evaluate())
                {
                    push(blockBytes);
                }
                break;
            }

            case Instruction.UNLESS: {
                int         controlID  = ReadIntLiteral(skipToNext);
                Condition   condition  = ReadConditionLiteral(skipToNext);
                List <byte> blockBytes = new List <byte>();

                while (HasBytes())
                {
                    byte b = pop();
                    if (b == (byte)Instruction.ENDIF)
                    {
                        int id = ReadIntLiteral(skipToNext);
                        if (id == controlID)
                        {
                            break;
                        }
                        else
                        {
                            blockBytes.Insert(0, (byte)Instruction.ENDIF);
                            blockBytes.InsertRange(0, LiteralFactory.CreateIntLiteral(id));
                        }
                    }
                    else
                    {
                        blockBytes.Insert(0, b);
                    }
                }
                if (!condition.Evaluate())
                {
                    push(blockBytes);
                }
                break;
            }

            case Instruction.LIST_LENGTH: {
                List <byte[]> list = ReadList(skipToNext);
                push(LiteralFactory.CreateIntLiteral(list.Count));
                break;
            }

            case Instruction.CARD_HAS_TAG: {
                Card   card    = GM.ReadCardFromStack();
                string tagName = ReadStringLiteral(skipToNext);
                push(LiteralFactory.CreateConditionLiteral(
                         LiteralFactory.CreateBoolLiteral(card.HasTag(tagName)),
                         LiteralFactory.CreateBoolLiteral(true),
                         ConditionType.BOOL,
                         ConditionOperator.EQUAL
                         ));
                break;
            }

            case Instruction.PLAYER_IS_WINNING: {
                GamePlayer player  = GM.ReadPlayerFromStack();
                bool       winning = true;
                foreach (GamePlayer otherPlayer in GM.Players.GetPlayers())
                {
                    if (otherPlayer.Points > player.Points)
                    {
                        winning = false;
                        break;
                    }
                }
                push(LiteralFactory.CreateConditionLiteral(
                         LiteralFactory.CreateBoolLiteral(winning),
                         LiteralFactory.CreateBoolLiteral(true),
                         ConditionType.BOOL,
                         ConditionOperator.EQUAL
                         ));
                break;
            }

            case Instruction.PLAYER_IS_LOSING: {
                GamePlayer player = GM.ReadPlayerFromStack();
                bool       losing = true;
                foreach (GamePlayer otherPlayer in GM.Players.GetPlayers())
                {
                    if (otherPlayer.Points < player.Points)
                    {
                        losing = false;
                        break;
                    }
                }
                push(LiteralFactory.CreateBoolLiteral(losing));
                break;
            }

            case Instruction.MULTIPLY: {
                int a = ReadIntLiteral(skipToNext);
                int b = ReadIntLiteral(skipToNext);
                push(LiteralFactory.CreateIntLiteral(a * b));
                break;
            }

            case Instruction.LOOP: {
                int id  = ReadIntLiteral(skipToNext);
                int num = ReadIntLiteral(skipToNext);
                List <List <byte> > instructionArrays = new List <List <byte> >();
                while (HasBytes())
                {
                    if (peek() == (byte)Instruction.ENDLOOP)
                    {
                        pop();
                        int endloopID = ReadIntLiteral(skipToNext);
                        if (endloopID == id)
                        {
                            break;
                        }
                        else
                        {
                            List <byte> endloopBytes = new List <byte>(LiteralFactory.CreateIntLiteral(endloopID));
                            endloopBytes.Add((byte)Instruction.ENDLOOP);
                            instructionArrays.Insert(0, endloopBytes);
                        }
                    }
                    else
                    {
                        List <byte> arr = popInstruction(skipToNext);
                        instructionArrays.Insert(0, arr);
                    }
                }
                for (int n = 0; n < num; n++)
                {
                    for (int m = 0; m < instructionArrays.Count; m++)
                    {
                        push(instructionArrays[m]);
                    }
                }
                break;
            }

            case Instruction.FOR_LOOP: {
                int                 ID                = ReadIntLiteral(skipToNext);
                List <byte[]>       items             = ReadList(skipToNext);
                List <List <byte> > instructionArrays = new List <List <byte> >();
                while (HasBytes())
                {
                    if (peek() == (byte)Instruction.ENDLOOP)
                    {
                        pop();
                        int endloopID = ReadIntLiteral(skipToNext);
                        if (endloopID == ID)
                        {
                            break;
                        }
                        else
                        {
                            List <byte> endloopBytes = new List <byte>(LiteralFactory.CreateIntLiteral(endloopID));
                            endloopBytes.Add((byte)Instruction.ENDLOOP);
                            instructionArrays.Insert(0, endloopBytes);
                        }
                    }
                    List <byte> arr = popInstruction(skipToNext);
                    instructionArrays.Insert(0, arr);
                }

                List <byte> idBytes = LiteralFactory.CreateIntLiteral(ID);
                for (int i = 0; i < items.Count; i++)
                {
                    List <byte> currentItem = new List <byte>(items[i]);
                    currentItem.Reverse();
                    List <byte> addToRegister = InstructionFactory.Make_AddToRegister(idBytes, currentItem);
                    for (int m = 0; m < instructionArrays.Count; m++)
                    {
                        push(instructionArrays[m]);
                    }
                    push(addToRegister);
                }
                break;
            }

            case Instruction.ADD_TO_REGISTER: {
                int         ID    = ReadIntLiteral(skipToNext);
                int         size  = ReadIntLiteral(skipToNext);
                List <byte> bytes = pop(size);
                register[ID] = bytes;
                break;
            }

            case Instruction.PLACEHOLDER: {
                int         ID    = ReadIntLiteral(skipToNext);
                List <byte> fetch = new List <byte>(register[ID]);
                fetch.Reverse();
                push(fetch);
                break;
            }

            // QUERIES

            case Instruction.GET_ACTIVE_PLAYER: {
                push(LiteralFactory.CreatePlayerLiteral(GM.Players.GetActivePlayer()));
                break;
            }

            case Instruction.GET_ALL_OPPONENTS: {
                push(LiteralFactory.CreateListLiteral(
                         new List <GamePlayer>(GM.Players.GetOpponents())
                         ));
                break;
            }

            case Instruction.GET_ALL_PLAYERS: {
                push(LiteralFactory.CreateListLiteral(
                         new List <GamePlayer>(GM.Players.GetPlayers())
                         ));
                break;
            }

            case Instruction.GET_CARDS_IN_DECK: {
                push(LiteralFactory.CreateListLiteral(
                         new List <Card>(GM.Cards.Deck.GetCards())
                         ));
                break;
            }

            case Instruction.GET_CARDS_IN_DISCARD: {
                push(LiteralFactory.CreateListLiteral(
                         new List <Card>(GM.Cards.Discard.GetCards())
                         ));
                break;
            }

            case Instruction.GET_CARDS_IN_HAND: {
                GamePlayer player = GM.ReadPlayerFromStack();
                push(LiteralFactory.CreateListLiteral(
                         new List <Card>(player.Hand.GetCards())
                         ));
                break;
            }

            case Instruction.GET_PLAYER_POINTS: {
                GamePlayer player = GM.ReadPlayerFromStack();
                int        points = player.Points;
                push(LiteralFactory.CreateIntLiteral(points));
                break;
            }

            case Instruction.READ_COUNTER: {
                string key   = ReadStringLiteral(skipToNext);
                int    count = GM.Variables.GetCounter(key);
                push(LiteralFactory.CreateIntLiteral(count));
                break;
            }

            case Instruction.BOOL_COMPARISON: {
                bool operandA     = ReadBoolLiteral(skipToNext);
                byte operatorEnum = ReadEnumLiteral();
                bool operandB     = ReadBoolLiteral(skipToNext);
                push(LiteralFactory.CreateConditionLiteral(
                         new CompareBool(operandA, operandB, (ConditionOperator)operatorEnum)
                         ));
                break;
            }

            case Instruction.NUM_COMPARISON: {
                int  operandA     = ReadIntLiteral(skipToNext);
                int  operandB     = ReadIntLiteral(skipToNext);
                byte operatorEnum = ReadEnumLiteral();
                push(LiteralFactory.CreateConditionLiteral(
                         new CompareNum(operandA, operandB, (ConditionOperator)operatorEnum)
                         ));
                break;
            }

            case Instruction.TARGET_PLAYER: {
                GM.UI.PresentChoiceOfPlayers(new List <GamePlayer>(GM.Players.GetPlayers()), this);
                break;
            }

            case Instruction.TARGET_OPPONENT: {
                GM.UI.PresentChoiceOfPlayers(new List <GamePlayer>(GM.Players.GetOpponents()), this);
                break;
            }

            case Instruction.TARGET_CARD_IN_DECK: {
                GM.UI.PresentChoiceOfCards(new List <Card>(GM.Cards.Deck.GetCards()), this);
                break;
            }

            case Instruction.TARGET_CARD_IN_DISCARD: {
                GM.UI.PresentChoiceOfCards(new List <Card>(GM.Cards.Discard.GetCards()), this);
                break;
            }

            case Instruction.TARGET_CARD_IN_HAND: {
                GamePlayer player = GM.ReadPlayerFromStack();
                GM.UI.PresentChoiceOfCards(new List <Card>(player.Hand.GetCards()), this);
                break;
            }

            case Instruction.RANDOM_PLAYER: {
                GamePlayer[] players      = GM.Players.GetPlayers();
                GamePlayer   randomPlayer = players[UnityEngine.Random.Range(0, players.Length)];
                push(LiteralFactory.CreatePlayerLiteral(randomPlayer));
                break;
            }

            case Instruction.RANDOM_OPPONENT: {
                GamePlayer[] opponents      = GM.Players.GetOpponents();
                GamePlayer   randomOpponent = opponents[UnityEngine.Random.Range(0, opponents.Length)];
                push(LiteralFactory.CreatePlayerLiteral(randomOpponent));
                break;
            }

            case Instruction.RANDOM_CARD_IN_DECK: {
                Card[] deckCards  = GM.Cards.Deck.GetCards();
                Card   randomCard = deckCards[UnityEngine.Random.Range(0, deckCards.Length)];
                push(LiteralFactory.CreateCardLiteral(randomCard));
                break;
            }

            case Instruction.RANDOM_CARD_IN_DISCARD: {
                Card[] discardCards = GM.Cards.Discard.GetCards();
                Card   randomCard   = discardCards[UnityEngine.Random.Range(0, discardCards.Length)];
                push(LiteralFactory.CreateCardLiteral(randomCard));
                break;
            }

            case Instruction.RANDOM_CARD_IN_HAND: {
                GamePlayer player     = GM.ReadPlayerFromStack();
                Card[]     handCards  = player.Hand.GetCards();
                Card       randomCard = handCards[UnityEngine.Random.Range(0, handCards.Length)];
                push(LiteralFactory.CreateCardLiteral(randomCard));
                break;
            }

            // EFFECTS

            case Instruction.INCREMENT_PLAYER_POINTS: {
                GamePlayer player    = GM.ReadPlayerFromStack();
                int        pointsNum = ReadIntLiteral(skipToNext);
                GM.SetPlayerPoints(player, player.Points + pointsNum);
                break;
            }

            case Instruction.PLAYER_DRAW_CARD: {
                GamePlayer player   = GM.ReadPlayerFromStack();
                int        numCards = ReadIntLiteral(skipToNext);
                for (int n = 0; n < numCards; n++)
                {
                    GM.PlayerDrawCard(player);
                }
                break;
            }

            case Instruction.SET_COUNTER: {
                string key   = ReadStringLiteral(skipToNext);
                int    count = ReadIntLiteral(skipToNext);
                GM.Variables.SetCounter(key, count);
                break;
            }

            case Instruction.SET_PLAYER_DRAW: {
                GamePlayer player = GM.ReadPlayerFromStack();
                int        num    = ReadIntLiteral(skipToNext);
                player.SetDrawPerTurn(num);
                break;
            }

            case Instruction.SET_PLAYER_MAX_HAND: {
                GamePlayer player = GM.ReadPlayerFromStack();
                int        num    = ReadIntLiteral(skipToNext);
                player.Hand.SetMax(num);
                break;
            }

            case Instruction.SET_PLAYER_POINTS: {
                GamePlayer player    = GM.ReadPlayerFromStack();
                int        pointsNum = ReadIntLiteral(skipToNext);
                GM.SetPlayerPoints(player, pointsNum);
                break;
            }

            case Instruction.MOVE_TO_DECK: {
                Card         card    = GM.ReadCardFromStack();
                DeckLocation posEnum = (DeckLocation)ReadEnumLiteral();
                card.Zone.MoveCard(GM.Cards.Deck, card.GetID());
                GM.Cards.Deck.MoveLastAddedCard(posEnum);
                break;
            }

            case Instruction.MOVE_TO_DISCARD: {
                Card card = GM.ReadCardFromStack();
                card.Zone.MoveCard(GM.Cards.Discard, card.GetID());
                break;
            }
            }
        } catch (UnexpectedByteException e) {
            Debug.LogError(e);
        } catch (StackFullException e) {
            Debug.LogError(e);
        } catch (StackEmptyException e) {
            Debug.LogError(e);
        }
    }