Game Sample: Converse Method

This is a method from a game I worked on. The method takes 6 strings, 1 representing a possible prompt, and then the others are possible responses, and returns the char selected by the user.

This method is used in my text based game to format dialogue, take a response, and return the letter answered. The method can be sent empty strings for answers which are not relevant, and will dispose of them, not allowing a user to pick them as an answer.

Screen Shot 2018-01-28 at 11.39.59 AMScreen Shot 2018-01-28 at 11.43.15 AM

char converse(string statement, string responseA, string responseB, string responseC, string responseD, string responseE){

    //char to be returned, representing answer. then sleeps to create pause
    char answer;
    sleep(3);

    //bools to represent that the letter chosen is valid
    bool answerB = false;
    bool answerC = false;
    bool answerD = false;
    bool answerE = false;

    //question posed
    cout << endl << endl << statement << endl << endl << endl;

    //possible responses
    cout << "A. " << responseA << endl;           //if there are multiple possible responses, this will trigger and set the corresponding bool to true. //B if(responseB.length() > 0){
        cout << "B. " << responseB << endl; answerB = true; } //C if(responseC.length() > 0){
        cout << "C. " << responseC << endl; answerC = true; } //D if(responseD.length() > 0){
        cout << "D. " << responseD << endl; answerD = true; } //E if(responseE.length() > 0){
        cout << "E. " << responseE << endl; answerE = true;     } //loop to allow user to make invalid entry while(true){ //stores user's answer in "answer" char cin >> answer;
        answer = tolower(answer);
//swtich statement to return user's response
        switch(answer){
            //if user answers:
            case 'a':
                return 'a';
                break;
            //if user answers:
            case 'b':
                if(answerB == true) return 'b';
                else break;
            //if user answers:
            case 'c':
                if(answerC == true) return 'c';
                else break;
            //if user answers:
            case 'd':
                if(answerD == true) return 'd';
                else break;
            //if user answers:
            case 'e':
                if(answerE == true) return 'e';
                else break;
            //otherwise, loop again
            default:
                break;
        }

        cout << "INVALID RESPONSE. Please choose a valid response" <<span 				data-mce-type="bookmark" 				id="mce_SELREST_start" 				data-mce-style="overflow:hidden;line-height:0" 				style="overflow:hidden;line-height:0" 			></span>< endl;
    }
}
</pre>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s