コード例 #1
0
        public void PlayerListBytecode()
        {
            game.Players = new PlayerManager(3);
            List <GamePlayer> players = new List <GamePlayer>(game.Players.GetPlayers());

            bytes.push(LiteralFactory.CreateListLiteral(players));
            List <GamePlayer> readPlayers = game.ReadPlayerListFromStack();

            Assert.AreEqual(players.Count, readPlayers.Count);

            foreach (GamePlayer player in readPlayers)
            {
                Assert.IsTrue(players.Contains(player));
            }
        }
コード例 #2
0
        public void GetListLength()
        {
            List <GamePlayer> list = new List <GamePlayer> {
                new GamePlayer("P1", 0),
                new GamePlayer("P2", 1),
                new GamePlayer("P3", 2),
                new GamePlayer("P4", 3),
            };

            bytes.push(InstructionFactory.Make_ListLength(
                           LiteralFactory.CreateListLiteral(list)
                           ));

            game.ExecuteNext();
            Assert.AreEqual(bytes.ReadIntLiteral(game.queryCheck), 4);
        }
コード例 #3
0
        public void List()
        {
            game.Players = new PlayerManager(5);
            GamePlayer[] playerList = game.Players.GetPlayers();
            bytes.push(LiteralFactory.CreateListLiteral(
                           new List <GamePlayer>(playerList)
                           ));

            List <Card> cardList = new List <Card> {
                new TestCard()
            };

            bytes.push(LiteralFactory.CreateListLiteral(cardList));

            Assert.AreEqual(
                bytes.ReportStackContent(),
                "LIST(size:1,type:CARD) LIST(size:5,type:PLAYER) "
                );
        }
コード例 #4
0
        public void CardListBytecode()
        {
            List <Card> cards = new List <Card>(new Card[] {
                new TestCard(),
                new TestCard(),
                new TestCard()
            });

            bytes.push(LiteralFactory.CreateListLiteral(cards));

            foreach (Card card in cards)
            {
                game.Cards.Deck.AddCard(card, DeckLocation.TOP);
            }

            List <Card> readCards = game.ReadCardListFromStack();

            Assert.AreEqual(cards.Count, readCards.Count);
            foreach (Card card in readCards)
            {
                Assert.IsTrue(cards.Contains(card));
            }
        }
コード例 #5
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);
        }
    }