private int findHighScore()
    {
        int highScore = int.MinValue;

        for (int testingCombination = 0; testingCombination < Mathf.Pow(2.0f, moveCount); testingCombination++)
        {
            StateScript cursor      = currentState;
            int         cursorScore = 0;
            for (int moves = 0; moves < moveCount; moves++)
            {
                if (Mathf.Floor(testingCombination & (int)Mathf.Pow(2, moves)) > 0)
                {
                    cursorScore += cursor.IReward;
                    cursor       = cursor.nextIState;
                }
                else
                {
                    cursorScore += cursor.UReward;
                    cursor       = cursor.nextUState;
                }
            }
            if (cursorScore > highScore)
            {
                highScore = cursorScore;
            }
        }
        return(highScore);
    }
Ejemplo n.º 2
0
 private void CreateScript(StateScript s)
 {
     if (s.script != null && s.script.GetClass().IsSubclassOf(typeof(IState)))
     {
         s.state = (IState)ScriptableObject.CreateInstance(s.script.GetClass());
     }
 }
    // initial state [3.3] [0.0] left_bank
    private void AutoSolve()
    {
        StateScript startState = new StateScript();

        startState.SetMissionaryCannibalCountInLeft(3, 3);
        startState.SetMissionaryCannibalCountInRight(0, 0);
        startState.boat_position = StateScript.boat_pos.left_bank.ToString();

        StateScript finalState = new StateScript();

        finalState.SetMissionaryCannibalCountInLeft(0, 0);
        finalState.SetMissionaryCannibalCountInRight(3, 3);
        finalState.boat_position = StateScript.boat_pos.right_bank.ToString();

        HelperFunction(startState, finalState);

        foreach (StateScript s in stateStack)
        {
            answerStack.Push(s);
        }

        foreach (StateScript ans in answerStack)
        {
            Debug.Log(ans.cannibal_count_left.ToString() + ", " + ans.missionary_count_left + "," + ans.boat_position);
        }

        auto_play_enabled = true;
        StartCoroutine("GoToNextState");
    }
Ejemplo n.º 4
0
 void ChangeStateA()
 {
     if (!state.Equals(state.onA))
     {
         state = state.onA;
         UpdatePosition();
     }
 }
Ejemplo n.º 5
0
 public void Reset()
 {
     if (!state.Equals(startState))
     {
         state = startState;
         UpdatePosition();
     }
 }
Ejemplo n.º 6
0
 void ChangeStateB()
 {
     if (!state.Equals(state.onB))
     {
         state = state.onB;
         UpdatePosition();
     }
 }
Ejemplo n.º 7
0
    public void Updating(out StateScript stateScript)
    {
        StateScript tmp = null;

        if (state != null)
        {
            if (!entered)
            {
                state.OnEnter();
                entered = true;
                Sort();
            }
            state.Execute();
            if (globalMark)
            {
                foreach (StateScript child in children)
                {
                    bool globalUsed = false;
                    foreach (StateScript c in child.children)
                    {
                        if (c == this)
                        {
                            globalUsed = true;
                            break;
                        }
                    }
                    if (!globalUsed)
                    {
                        child.children.Add(this);
                    }
                }
            }
            foreach (StateScript child in children)
            {
                if (child.state == null)
                {
                    CreateScript(child);
                }
                if (child.state.OnCondition())
                {
                    entered = false;
                    tmp     = child;
                    state.OnExit();
                    child.priority++;
                    if (child.priority > children.Count)
                    {
                        child.priority = 0;
                    }
                    break;
                }
            }
        }
        else
        {
            CreateScript(this);
        }
        stateScript = tmp != null ? tmp:this;
    }
 public void getLevelStateList(List <StateScript> levelList)
 {
     foreach (StateScript state in levelList)
     {
         stackStateList.Push(state);
     }
     currentState = stackStateList.Peek();
     currentState.atInit();
 }
Ejemplo n.º 9
0
    //private Update updateFn;
    void Start()
    {
        theEvents = new EventScript();
        theStates = new StateScript();
        Scene scene = SceneManager.GetActiveScene();

        theEvents = GetComponent <EventScript> ();
        //Debug.Log ("Active Scene is: " + scene.name);
    }
 public void setState(int state)
 {
     if (currentState != null)
     {
         currentState.image.color = unselectedColor;
     }
     currentState             = transform.Find("Background/Game Panel/" + state.ToString()).GetComponent <StateScript>();
     currentState.image.color = selectedColor;
 }
Ejemplo n.º 11
0
 void Start()
 {
     if (m_StartState == null)
     {
         Debug.LogError("Each state machine must have a start state set via the inspector view.");
     }
     curState_ = m_StartState;
     curState_.OnStateEntered();
 }
Ejemplo n.º 12
0
 void Start()
 {
     if (m_StartState == null)
     {
         Debug.LogError("Each state machine must have a start state set via the inspector view.");
     }
     curState_ = m_StartState;
     curState_.OnStateEntered();
 }
Ejemplo n.º 13
0
 // Use this for initialization
 void Start()
 {
     feasibleEvents = new List <string> ()
     {
         "wait", "play-game-button-clicked", "quit-button-clicked", "log-out-button-clicked"
     };
     lastEvent   = feasibleEvents [0];
     myStatesObj = GetComponent <StateScript> ();
     test        = true;
 }
 private void Awake()
 {
     if (stackStateList != null)
     {
         stackStateList.Clear();
     }
     stackStateList = null;
     currentState   = null;
     stackStateList = new Stack <StateScript>();
     initMachine();
 }
 private bool CheckForValueInBuffer(StateScript ss)
 {
     foreach (StateScript temp in this.buffer)
     {
         if (temp.cannibal_count_left == ss.cannibal_count_left && temp.cannibal_count_right == ss.cannibal_count_right && temp.missionary_count_left == ss.missionary_count_left && temp.missionary_count_right == ss.missionary_count_right && temp.boat_position == ss.boat_position)
         {
             return(true);
         }
     }
     return(false);
 }
Ejemplo n.º 16
0
 void Update()
 {
     if (stateStart != null)
     {
         if (currState == null)
         {
             currState = stateStart;
         }
         currState.Updating(out currState);
     }
 }
 public void nextState()
 {
     if (stackStateList.Count != 0)
     {
         stackStateList.Pop();
         currentState.atEnd();
         currentState.enabled = false;
         if (stackStateList.Count != 0)
         {
             currentState = stackStateList.Peek();
             currentState.atInit();
         }
     }
 }
Ejemplo n.º 18
0
    //Function : PushStateFunction
    //Method : This is the Function that used For
    //Pushing The Statement
    public void PushStateFunction(System.Action enter, System.Action exit, System.Action update)
    {
        if (GetCrtStateFunction() != null)

        {
            GetCrtStateFunction().ExitFunction();
        }

        StateScript stateScript = new StateScript(enter, exit, update);


        stateStackScript.Push(stateScript);
        stateScript.EnterFunction();
    }
 private void changeState(int reward, StateScript nextState)
 {
     score += reward;
     if (currentState == targetState)
     {
         currentState.image.color = targetColor;
     }
     else
     {
         currentState.image.color = unselectedColor;
     }
     currentState          = nextState;
     nextState.image.color = selectedColor;
     moveCount--;
 }
 private bool CheckForValidState(StateScript ss)
 {
     // return true if it is a valid state else return false
     // in m_count >= c_count in both bank then it's a valid
     // m_count < c_count is also valid iff m_count == 0
     if (ss.cannibal_count_left > ss.missionary_count_left && ss.missionary_count_left != 0)
     {
         return(false);
     }
     if (ss.cannibal_count_right > ss.missionary_count_right && ss.missionary_count_right != 0)
     {
         return(false);
     }
     return(true);
 }
Ejemplo n.º 21
0
    public void Sort()
    {
        int j        = 0;
        int keyValue = 0;

        for (int i = 0; i < children.Count; ++i)
        {
            keyValue = i;
            j        = i - 1;
            while (j >= 0 && children[j].priority > children[keyValue].priority)
            {
                StateScript tmp = children[j];
                children[j]        = children[keyValue];
                children[keyValue] = tmp;
                keyValue           = j--;
            }
        }
    }
Ejemplo n.º 22
0
 // Use this for initialization
 void Start()
 {
     startState = state;
     srs        = GameObject.FindGameObjectWithTag("StringRecorder").GetComponent <StringRecorderScript>();
 }
    private void newTrial()
    {
        centerText.text = moveCount.ToString() + " moves";
        switch (trialNumber)
        {
        case 0:
            showOverlay("Instructions", "Welcome to the first part of the training!");
            break;

        case 1:
            showOverlay("Instructions", "You will see 6 rectangles on the screen. You can move back and forth between any of the rectangles by pressing the 'U' or 'I' keys.");
            break;

        case 2:
            showOverlay("Instructions", "One key goes through the middle, the other around the circle. You will have some time to try this out. Familiarize yourself with which key you have to press in order to get away from other rectangles.");
            break;

        case 3:
            showOverlay("Instructions", "Below the rectangles there will be indicators denoted by pluses and minuses that show if you will gain or lose points. Two pluses or minuses indicate you will win or lose more. The indicators are separated by a '/'  on the left corresponds to pressing 'U' to go in a circle while the right corresponds with pressing 'I' to go across.");
            break;

        case 4:
            showOverlay("Instructions", "Now it is time to practice!");
            break;

        case 5:
            setState(1);
            topText.text    = "Tutorial";
            centerText.text = "Press U to move counter-clockwise around the circle.";
            break;

        case 6:
            setState(2);
            centerText.text = "Press I to move across the circle.";
            break;

        case 7:
            setState(5);
            centerText.text = "Experiment with U and I to get used to how you can move around the circle.";
            turnTimer       = 10.0f;
            break;

        case TUTORIAL_END:
            showOverlay("Practice 1", "Now we will practice reaching a target. Get to the red target in the allotted amount of moves.");
            break;

        case TARGET_PRACTICE_END:
            successes = 0;
            showOverlay("Test 1", "In order to continue, you must score at least 7 out of 10 successes.");
            break;

        case TARGET_TEST_END:
            if (successes > MINIMUM_SCORE)
            {
                showOverlay("Test 1", "You have achieved " + successes.ToString() + " successes and passed the test.");
            }
            else
            {
                _trialNumber = TARGET_PRACTICE_END - 1;
                showOverlay("Test 1", "You have achieved " + successes.ToString() + " successes and failed the test. Please try again.");
            }
            break;

        case TARGET_TEST_END + 1:
            targetState.image.color = unselectedColor;
            targetState             = null;
            showOverlay("Practice 2", "Welcome to the second part of the training. Your goal now is to memorize how many points each move delivers. Try to get the maximum amount of points with the amount of moves you are given.");
            break;

        case PROFIT_PRACTICE_END:
            successes = 0;
            showOverlay("Test 2", "In order to continue, you must achieve the highest possible score in the next question.");
            break;

        case PROFIT_TEST_END:
            if (score >= targetScore)
            {
                showOverlay("Test 2", "You have passed the test.");
            }
            else
            {
                _trialNumber = PROFIT_TEST_END - 2;
                showOverlay("Test 2", "You have failed the test.");
            }
            break;

        case PROFIT_TEST_END + 1:
            showOverlay("Trial", "You have successfully passed the tutorials and practice rounds, and are now beginning the trial.");
            audioSource.clip = songChoices[participantID % songChoices.Length];
            audioSource.Play();
            break;

        case TRIAL_END:
            showOverlay("End", "You have finished the trial.");
            break;

        case TRIAL_END + 1:
            Application.Quit();
            break;

        default:
            if (trialNumber < TARGET_PRACTICE_END)
            {
                if (trialNumber % 2 == 1)
                {
                    topText.text = "Practice 1 Review " + ((trialNumber - TUTORIAL_END) / 2 + 1 + " / " + (TARGET_PRACTICE_END - TUTORIAL_END) / 2);
                    if (targetState != null)
                    {
                        targetState.image.color = unselectedColor;
                    }
                    setState(UnityEngine.Random.Range(1, 7));
                    targetState = currentState;
                    moveCount   = 2;
                    for (int moves = moveCount; moves > 0; moves--)
                    {
                        targetState = UnityEngine.Random.Range(0.0f, 1.0f) >= 0.5f ? targetState.nextIState : targetState.nextUState;
                    }
                    if (targetState == currentState)
                    {
                        moveCount++;
                        targetState = UnityEngine.Random.Range(0.0f, 1.0f) >= 0.5f ? targetState.nextIState : targetState.nextUState;
                    }
                    targetState.image.color = targetColor;
                }
                else
                {
                    if (currentState == targetState)
                    {
                        showOverlay("Practice 1", "Success.");
                    }
                    else
                    {
                        showOverlay("Practice 1", "Failure.");
                    }
                }
            }
            else if (trialNumber < TARGET_TEST_END)
            {
                if (trialNumber % 2 == 0)
                {
                    topText.text = "Test 1 Question " + ((trialNumber - TARGET_PRACTICE_END) / 2 + 1) + " / " + (TARGET_TEST_END - TARGET_PRACTICE_END) / 2;
                    if (targetState != null)
                    {
                        targetState.image.color = unselectedColor;
                    }
                    setState(UnityEngine.Random.Range(1, 7));
                    targetState = currentState;
                    moveCount   = 2;
                    for (int moves = moveCount; moves > 0; moves--)
                    {
                        targetState = UnityEngine.Random.Range(0.0f, 1.0f) >= 0.5f ? targetState.nextIState : targetState.nextUState;
                    }
                    if (targetState == currentState)
                    {
                        moveCount++;
                        targetState = UnityEngine.Random.Range(0.0f, 1.0f) >= 0.5f ? targetState.nextIState : targetState.nextUState;
                    }
                    targetState.image.color = targetColor;
                }
                else
                {
                    if (currentState == targetState)
                    {
                        successes++;
                        showOverlay("Test 1", "Success.");
                    }
                    else
                    {
                        showOverlay("Test 1", "Failure.");
                    }
                }
            }
            else if (trialNumber < PROFIT_PRACTICE_END)
            {
                if (trialNumber % 2 == 0)
                {
                    score        = 0;
                    topText.text = "Practice 2 Review " + (trialNumber - TARGET_TEST_END) / 2 + " / " + (PROFIT_PRACTICE_END - TARGET_TEST_END) / 2;
                    setState(UnityEngine.Random.Range(1, 7));
                    moveCount   = UnityEngine.Random.Range(2, 4);
                    targetScore = findHighScore();
                }
                else
                {
                    if (score >= targetScore)
                    {
                        showOverlay("Practice 2", "Success.");
                    }
                    else
                    {
                        showOverlay("Practice 2", "Failure.");
                    }
                }
            }
            else if (trialNumber < PROFIT_TEST_END)
            {
                if (trialNumber % 2 == 0)
                {
                    score        = 0;
                    topText.text = "Test 2 Quiz";
                    setState(UnityEngine.Random.Range(1, 4));
                    moveCount   = UnityEngine.Random.Range(3, 4);
                    targetScore = findHighScore();
                }
            }
            else if (trialNumber < TRIAL_END)
            {
                if (trialNumber % 2 == 1)
                {
                    turnTimer = 0;
                    score     = 0;
                    setState(latinSquare(6, participantID + trialNumber) + 1);
                    moveCount = latinSquare(7, participantID + trialNumber) + 2;
                }
                else
                {
                    showOverlay("Trial episode " + (trialNumber - PROFIT_TEST_END) / 2 + " / " + (TRIAL_END - PROFIT_TEST_END) / 2, "You scored " + score + " points.");
                }
            }
            break;
        }
    }
    IEnumerator GoToNextState()
    {
        StateScript thisState = null;
        StateScript thatState = null;

        Transform pos = gameControllerScript.charPosition;

        while (answerStack.Count > 1)
        {
            thisState = answerStack.Pop();
            thatState = answerStack.Peek();

            int miss = 0;
            int cann = 0;
            if (thisState.boat_position == StateScript.boat_pos.left_bank.ToString())
            {
                miss = thisState.missionary_count_left - thatState.missionary_count_left;
                cann = thisState.cannibal_count_left - thatState.cannibal_count_left;


                // missionary
                for (int i = 0; i < 3; i++)
                {
                    if (pos.GetChild(0).GetChild(i).childCount != 0 && miss != 0)
                    {
                        miss -= 1;
                        gameControllerScript.MouseDownFromMissionary(pos.GetChild(0).GetChild(i).GetChild(0));
                    }
                }

                // cannibal
                for (int i = 3; i < 6; i++)
                {
                    if (pos.GetChild(0).GetChild(i).childCount != 0 && cann != 0)
                    {
                        cann -= 1;
                        gameControllerScript.MouseDownFromCannibal(pos.GetChild(0).GetChild(i).GetChild(0));
                    }
                }
            }
            else
            {
                miss = thisState.missionary_count_right - thatState.missionary_count_right;
                cann = thisState.cannibal_count_right - thatState.cannibal_count_right;


                // missionary
                for (int i = 0; i < 3; i++)
                {
                    if (pos.GetChild(2).GetChild(i).childCount != 0 && miss != 0)
                    {
                        miss -= 1;
                        gameControllerScript.MouseDownFromMissionary(pos.GetChild(2).GetChild(i).GetChild(0));
                    }
                }

                // cannibal
                for (int i = 3; i < 6; i++)
                {
                    if (pos.GetChild(2).GetChild(i).childCount != 0 && cann != 0)
                    {
                        cann -= 1;
                        gameControllerScript.MouseDownFromCannibal(pos.GetChild(2).GetChild(i).GetChild(0));
                    }
                }
            }

            gameControllerScript.boatScript.MoveTheBoat();
            yield return(new WaitForSeconds(3.5f));

            for (int i = 0; i < 2; i++)
            {
                if (pos.GetChild(1).GetChild(i).childCount != 0)
                {
                    if (pos.GetChild(1).GetChild(i).GetChild(0).tag == "cannibal")
                    {
                        gameControllerScript.MouseDownFromCannibal(pos.GetChild(1).GetChild(i).GetChild(0));
                        Debug.Log("cannibal");
                    }

                    else if (pos.GetChild(1).GetChild(i).GetChild(0).tag == "missionary")
                    {
                        gameControllerScript.MouseDownFromMissionary(pos.GetChild(1).GetChild(i).GetChild(0));
                        Debug.Log("missionary");
                    }
                }
            }
            yield return(new WaitForSeconds(0.75f));
        }
    }
    // this function is responsible for selecting appropriate state using DFS algorithm
    private void HelperFunction(StateScript s, StateScript final)
    {
        StateScript currentState = s;
        StateScript finalState   = final;

        stateStack.Push(currentState);

//		Debug.Log (currentState == finalState);

        StateScript tempState = null;

        while (true)
        {
            currentState = stateStack.Peek();

            if (currentState.cannibal_count_left == finalState.cannibal_count_left && currentState.missionary_count_left == finalState.missionary_count_left && currentState.cannibal_count_right == finalState.cannibal_count_right && currentState.missionary_count_right == finalState.missionary_count_right && currentState.boat_position == finalState.boat_position)
            {
                break;
            }

            if (!CheckForValueInBuffer(currentState))
            {
                buffer.Push(currentState);
            }
            // if boat is in the left bank then next state will result the boat in right bank
            if (currentState.boat_position == StateScript.boat_pos.left_bank.ToString())
            {
                bool found = false;
                for (int missionary = 0; missionary <= currentState.missionary_count_left; missionary++)
                {
                    for (int cannibal = 0; cannibal <= currentState.cannibal_count_left; cannibal++)
                    {
                        if ((missionary + cannibal) == 0 || (missionary + cannibal) > 2)
                        {
                            continue;
                        }

                        tempState = new StateScript(currentState.missionary_count_left - missionary, currentState.cannibal_count_left - cannibal, currentState.missionary_count_right + missionary, currentState.cannibal_count_right + cannibal, StateScript.boat_pos.right_bank.ToString());
                        if (CheckForValidState(tempState) && !CheckForValueInBuffer(tempState))
                        {
                            stateStack.Push(tempState);
                            found = true;
                            break;
                        }
                    }
                    if (found)
                    {
                        break;
                    }
                }

                if (!found)
                {
                    stateStack.Pop();
                }
            }

            // if boat is in the right bank then next state will result the boat in left bank
            else
            {
                bool found = false;
                for (int missionary = 0; missionary <= currentState.missionary_count_right; missionary++)
                {
                    for (int cannibal = 0; cannibal <= currentState.cannibal_count_right; cannibal++)
                    {
                        if ((missionary + cannibal) == 0 || (missionary + cannibal) > 2)
                        {
                            continue;
                        }

                        tempState = new StateScript(currentState.missionary_count_left + missionary, currentState.cannibal_count_left + cannibal, currentState.missionary_count_right - missionary, currentState.cannibal_count_right - cannibal, StateScript.boat_pos.left_bank.ToString());

                        if (CheckForValidState(tempState) && !CheckForValueInBuffer(tempState))
                        {
                            stateStack.Push(tempState);
                            found = true;
                            break;
                        }
                    }
                    if (found)
                    {
                        break;
                    }
                }
                if (!found)
                {
                    StateScript popped = stateStack.Pop();
                }
            }
        }
    }
 /// <summary>
 /// Switches to a different state.
 /// </summary>
 /// <param name="script">The script to enter.</param>
 public abstract void EnterState(StateScript script);
Ejemplo n.º 27
0
    public void ChangeState(StateScript newState)
    {
//		curState_.OnStateExit();
        curState_ = newState;
        curState_.OnStateEntered();
    }
Ejemplo n.º 28
0
 public void ChangeState(StateScript newState)
 {
     //		curState_.OnStateExit();
     curState_ = newState;
     curState_.OnStateEntered();
 }