Exemple #1
0
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            if (TR.AuthOn)
            {
                bool pass = false;

                TBController ctrl = filterContext.Controller as TBController;
                if (Roles != String.Empty)
                {
                    foreach (var role in Roles.Split(TR.or.ToCharArray()))
                    {
                        if (pass = ctrl.User.IsInRole(role)) // deliberate assignment
                        {
                            break;
                        }
                    }
                }
                else
                {
                    pass = ctrl.User.IsInRole(TR.All);
                }
                if (!pass)
                {
                    filterContext.Result = new RedirectToRouteResult(
                        new RouteValueDictionary
                    {
                        { "action", "Unauthorized" },
                        { "controller", "Errors" }
                    });
                    return;
                }
                base.OnAuthorization(filterContext);
            }
        }
Exemple #2
0
    public void StartGame()
    {
        TBController.playMode   = (PlayMode)modeDropdown.value;
        TBController.botVersion = (BotVersion)botDropdown.value;

        EnableBotSettings(false);
        EnableWeightFields(TBController.playMode == PlayMode.ShowOff);

        OSController.SendValuesToBoard();
        TBController.InitializeGame();

        playButton.interactable  = false;
        pauseButton.interactable = true;
        stopButton.interactable  = true;
    }
Exemple #3
0
    public void StopButtonClick()
    {
        TBController.GameOver(true);

        UpdateNowPlayingText(true, false, null);
    }
Exemple #4
0
    /// <summary>
    /// Main loop of the bot. It's based on the same method of TetrisBot, but with the add-on that checks if a Tetris can be played
    /// </summary>
    /// <param name="nextPieceType"></param>
    /// <param name="budget"></param>
    /// <returns></returns>
    public override IEnumerator ActCoroutine(PieceType nextPieceType, float budget)
    {
        t0 = 0.0f;

        PieceModel nextPiece = new PieceModel(nextPieceType);

        List <PieceAction> possibleActions = currentTetrisState.GetActions(nextPiece);

        bestAction = null;

        yield return(null);

        t0 += Time.deltaTime;

        //If the currentPiece is a I piece, the Tetris is checked since there is no possibility to make a Tetris with another type of piece
        if (nextPieceType == PieceType.I)
        {
            CheckTetris(nextPiece, currentTetrisState, possibleActions, budget);
        }

        //If there is no possibility of Tetris, the same algorithm than TetrisBot is played
        if (bestAction == null)
        {
            float bestScore = -float.MaxValue;

            int i            = Random.Range(0, possibleActions.Count);
            int initialIndex = i;
            while (t0 < budget)
            {
                if (!TBController.pausedGame)
                {
                    t0 += Time.deltaTime;

                    TetrisState newState = currentTetrisState.CloneState();

                    newState.DoAction(nextPiece, possibleActions[i]);
                    nextPiece.ResetCoordinates();

                    float score = newState.GetHumanizedScore();

                    if (score > bestScore)
                    {
                        bestScore  = score;
                        bestAction = possibleActions[i];
                    }

                    i++;

                    if (i == possibleActions.Count)
                    {
                        i = 0;
                    }
                    if (i == initialIndex)
                    {
                        break;
                    }
                }

                yield return(null);
            }
        }
        else
        {
            Debug.Log("BOOM! Tetris for bot");
        }

        //If there is no bestAction at this point, a random action is going to be played
        if (bestAction == null)
        {
            bestAction = currentTetrisState.GetRandomAction(nextPiece);
        }
        currentTetrisState.DoAction(nextPiece, bestAction);

        TBController.DoActionByBot(bestAction);
    }
Exemple #5
0
    /// <summary>
    /// Main method of the bot. While the time budget is not spent, the algorithm makes rollouts over the different nodes of the tree, choosing the best child
    /// of the current root node, and finally, chooses the best action for the current piece in the current state
    /// </summary>
    /// <param name="nextPieceType"></param>
    /// <param name="budget"></param>
    /// <returns></returns>
    public override IEnumerator ActCoroutine(PieceType nextPieceType, float budget)
    {
        t0       = 0.0f;
        rollouts = 0;

        PieceModel nextPiece = new PieceModel(nextPieceType);

        //Debug
        //yield return TetrisBoardController.Instance.StartCoroutine(TetrisBoardController.Instance.ShowPossibleActionsCoroutine(currentNode.state.GetActions(nextPiece)));

        MCTSNode currentRollingNode = currentNode;

        //if the currentRollingNode doesn't have children at the beginning of the method, that means that its child are the result of playing the nextPiece in
        //the state of the currentRollingNode
        if (currentRollingNode.children.Count == 0)
        {
            currentRollingNode.ExtendNode(nextPiece);
        }

        //While there is still time
        while (t0 < budget)
        {
            if (!TBController.pausedGame)
            {
                t0 += Time.deltaTime;

                //For each child, a rollout is made
                foreach (MCTSNode child in currentRollingNode.children)
                {
                    //If the state is not terminal, the rollout is made, and the score stored and backpropagated
                    if (!child.state.IsTerminal())
                    {
                        float score = Rollout(child);
                        child.score += score;
                        child.n     += 1;

                        Backpropagation(child.parent, score);
                    }
                    //If it's terminal, it store like a rollout was made in order to not choose this child when the best one is searched
                    else
                    {
                        child.n += 1;
                    }
                }

                //After the rollouts, the best child is chosen
                MCTSNode bestChild = currentRollingNode.GetBestChild();

                //If the best child it doesn't have children
                if (bestChild.children.Count == 0)
                {
                    //If their children piece is known
                    if (bestChild.height < pieces.Count)
                    {
                        //The node will be extended
                        bestChild.ExtendNode(new PieceModel(pieces[bestChild.height]));
                        RolloutOneRandomChild(bestChild);
                    }
                    //And then, it goes back to the currentRootNode
                    currentRollingNode = currentNode;
                }
                //Otherwise, the algorithm moves to that best child
                else
                {
                    currentRollingNode = bestChild;
                }
            }

            yield return(null);
        }

        //When the loop ends, the recommended child is chosen from the children of the current root node
        MCTSNode recommendedChild = GetRecommendedChild(currentNode);

        currentNode = recommendedChild;                                  //The current root node is updated
        currentTetrisState.DoAction(nextPiece, recommendedChild.action); //Its action is played in the currentState

        //Debug.Log("Rollouts: " + rollouts);

        TBController.DoActionByBot(recommendedChild.action); //And it is played in the real board
    }
Exemple #6
0
 /// <summary>
 /// The MCTSTetrisBot is created giving it some initialPieces and a budget of time, and it only creates the tree
 /// </summary>
 /// <param name="initialPieces"></param>
 /// <param name="budget"></param>
 public MCTSTetrisBot(PieceType[] initialPieces, float budget)
 {
     TBController.StartCoroutine(BotConstructionCoroutine(initialPieces, budget));
 }