Beispiel #1
0
    // I drew out a state machine to control a game, including serves, and implemented it below:

    public eRallyOutcome MakeStep(eRallyStateMachineAction rsma)
    {
        // Debug:
        switch (rsma)
        {
        case eRallyStateMachineAction.RSMA_ATT_RACK:
            Debug.Log("Attacker racket.");
            break;

        case eRallyStateMachineAction.RSMA_DEF_RACK:
            Debug.Log("Defender racket.");
            break;

        case eRallyStateMachineAction.RSMA_ATT_TABLE:
            Debug.Log("Attacker's half of the table.");
            break;

        case eRallyStateMachineAction.RSMA_DEF_TABLE:
            Debug.Log("Defender's half of the table.");
            break;

        case eRallyStateMachineAction.RSMA_OUT:
            Debug.Log("Ball went out.");
            break;
        }


        switch (currState)
        {
        case eRallyState.S0:
            switch (rsma)
            {
            case eRallyStateMachineAction.RSMA_ATT_TABLE:
                currState = eRallyState.S1;
                return(eRallyOutcome.RO_NONE);

            case eRallyStateMachineAction.RSMA_DEF_RACK:
                return(eRallyOutcome.RO_ATT_WINS);

            default:
                return(eRallyOutcome.RO_DEF_WINS);
            }

        case eRallyState.S1:
            switch (rsma)
            {
            case eRallyStateMachineAction.RSMA_DEF_TABLE:
                currState = eRallyState.S2;
                return(eRallyOutcome.RO_NONE);

            case eRallyStateMachineAction.RSMA_DEF_RACK:
                return(eRallyOutcome.RO_ATT_WINS);

            default:
                return(eRallyOutcome.RO_DEF_WINS);
            }

        case eRallyState.S2:
            switch (rsma)
            {
            case eRallyStateMachineAction.RSMA_DEF_RACK:
                currState = eRallyState.S3;
                return(eRallyOutcome.RO_NONE);

            case eRallyStateMachineAction.RSMA_ATT_RACK:
                return(eRallyOutcome.RO_DEF_WINS);

            default:
                return(eRallyOutcome.RO_ATT_WINS);
            }

        case eRallyState.S3:
            switch (rsma)
            {
            case eRallyStateMachineAction.RSMA_ATT_TABLE:
                currState = eRallyState.S4;
                return(eRallyOutcome.RO_NONE);

            case eRallyStateMachineAction.RSMA_ATT_RACK:
                return(eRallyOutcome.RO_DEF_WINS);

            default:
                return(eRallyOutcome.RO_ATT_WINS);
            }

        case eRallyState.S4:
            switch (rsma)
            {
            case eRallyStateMachineAction.RSMA_ATT_RACK:
                currState = eRallyState.S1;
                return(eRallyOutcome.RO_NONE);

            case eRallyStateMachineAction.RSMA_DEF_RACK:
                return(eRallyOutcome.RO_ATT_WINS);

            default:
                return(eRallyOutcome.RO_DEF_WINS);
            }

        default:
            return(eRallyOutcome.RO_NONE);            // this code should not be reached
        }
    }
Beispiel #2
0
    private eRallyState currState = eRallyState.S4;     // To bypass serving for now...

    private void OnEvent_rallyEnded()
    {
        currState = eRallyState.S0;
    }