コード例 #1
0
        public void PopInstruction()
        {
            bytes.push(LiteralFactory.CreateStringLiteral("Hello world"));
            bytes.push(LiteralFactory.CreateIntLiteral(5));
            bytes.push(LiteralFactory.CreateBoolLiteral(true));
            bytes.push((byte)Instruction.EFFECT_DELIMITER);

            List <byte> instructionArr = bytes.popInstruction(dummyCallback);

            Assert.AreEqual(1, instructionArr.Count);
            Assert.AreEqual(Instruction.EFFECT_DELIMITER, (Instruction)instructionArr[0]);

            List <byte> boolArr = bytes.popInstruction(dummyCallback);

            Assert.AreEqual(2, boolArr.Count);
            bytes.push(boolArr);
            Assert.AreEqual(true, bytes.ReadBoolLiteral(dummyCallback));

            List <byte> intArr = bytes.popInstruction(dummyCallback);

            Assert.AreEqual(5, intArr.Count);
            bytes.push(intArr);
            Assert.AreEqual(5, bytes.ReadIntLiteral(dummyCallback));

            List <byte> stringArr = bytes.popInstruction(dummyCallback);

            bytes.push(stringArr);
            Assert.AreEqual("Hello world", bytes.ReadStringLiteral(dummyCallback));
        }
コード例 #2
0
        public void Loop()
        {
            int numLoops = 3;

            List <byte> loopCode = new List <byte>();

            loopCode.Insert(0, (byte)Instruction.EFFECT_DELIMITER);
            loopCode.InsertRange(0, LiteralFactory.CreateIntLiteral(4));
            loopCode.Insert(0, (byte)Instruction.EFFECT_DELIMITER);
            loopCode.InsertRange(0, LiteralFactory.CreateStringLiteral("Hello world"));
            loopCode.Insert(0, (byte)Instruction.EFFECT_DELIMITER);
            loopCode.InsertRange(0, LiteralFactory.CreateBoolLiteral(true));

            bytes.push(InstructionFactory.Make_Loop(
                           LiteralFactory.CreateIntLiteral(numLoops),
                           loopCode
                           ));

            game.ExecuteNext();
            for (int i = 0; i < numLoops; i++)
            {
                Assert.AreEqual(Instruction.EFFECT_DELIMITER, (Instruction)bytes.pop());
                Assert.AreEqual(4, bytes.ReadIntLiteral(game.queryCheck));
                Assert.AreEqual(Instruction.EFFECT_DELIMITER, (Instruction)bytes.pop());
                Assert.AreEqual("Hello world", bytes.ReadStringLiteral(game.queryCheck));
                Assert.AreEqual(Instruction.EFFECT_DELIMITER, (Instruction)bytes.pop());
                Assert.AreEqual(true, bytes.ReadBoolLiteral(game.queryCheck));
            }
        }
コード例 #3
0
        public void BoolBytecode()
        {
            bytes.push(LiteralFactory.CreateBoolLiteral(true));
            bytes.push(LiteralFactory.CreateBoolLiteral(false));

            Assert.IsFalse(bytes.ReadBoolLiteral(dummyCallback));
            Assert.IsTrue(bytes.ReadBoolLiteral(dummyCallback));
        }
コード例 #4
0
 public void Bool()
 {
     bytes.push(LiteralFactory.CreateBoolLiteral(true));
     Assert.AreEqual(
         bytes.ReportStackContent(),
         "BOOL(True) "
         );
 }
コード例 #5
0
        public void CheckConditions()
        {
            bytes.push(InstructionFactory.Make_IsFalse(
                           LiteralFactory.CreateBoolLiteral(true)
                           ));
            Condition falseCondition = bytes.ReadConditionLiteral(game.queryCheck);

            Assert.IsFalse(falseCondition.Evaluate());

            bytes.push(InstructionFactory.Make_IsTrue(
                           LiteralFactory.CreateBoolLiteral(true)
                           ));
            Condition trueCondition = bytes.ReadConditionLiteral(game.queryCheck);

            Assert.IsTrue(trueCondition.Evaluate());
        }
コード例 #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);
        }
    }