1
\$\begingroup\$

I am updating my post to include more of the code and cleaned up the function hopefully.

I am trying to write a function for the computer to keep calling a play in a do while loop (which I am assuming is correct) with a pause in between and for the menu to be brought up to the user after each selection from the playbook function that contains the playbook menu. I am not an expert with the cin operator, but my playbook function works perfectly when the user is entering the plays but the cursor hangs when it is calling the function_cpu (computer) for the computer to make the selection. I am sure I am missing something in the code here. I put some notes in the code below.

int playbook(int &playbook_choice) {

cout << "Please choose a play from the options below : " << endl;
cout << "================================================" << endl;
cout << "1." << plays[0] << endl;
cout << "2." << plays[1] << endl;
cout << "3." << plays[2] << endl;
cout << "4." << plays[3] << endl;
cout << "5." << plays[4] << endl;
cout << "6." << plays[5] << endl;
cout << "7." << plays[6] << endl;
cout << "8." << plays[7] << endl;
cout << "9." << plays[8] << endl;
cout << "10." << plays[9] << endl;
cout << "11." << plays[10] << endl;
cout << "12." << plays[11] << endl;
cout << "13." << plays[12] << endl;
cout << "14." << plays[13] << endl;
cout << "15." << plays[14] << endl;
cout << "16." << plays[15] << endl;
cout << "17." << plays[16] << endl;
cout << "18." << plays[17] << endl;
cout << "19." << plays[18] << endl;


cin >> playbook_choice;

do {

    switch (playbook_choice) {

    case 1:
        cout << "You selected the" << " " << plays[0] << " " << "play" << endl;
        break;
    case 2:
        cout << "You selected the" << " " << plays[1] << " " << "play" << endl;
        break;
    case 3:
        cout << "You selected the" << " " << plays[2] << " " << "play" << endl;
        break;
    case 4:
        cout << "You selected the" << " " << plays[3] << " " << "play" << endl;
        break;
    case 5:
        cout << "You selected the" << " " << plays[4] << " " << "play" << endl;
        break;
    case 6:
        cout << "You selected the" << " " << plays[5] << " " << "play" << endl;
        break;
    case 7:
        cout << "You selected the" << " " << plays[6] << " " << "play" << endl;
        break;
    case 8:
        cout << "You selected the" << " " << plays[7] << " " << "play" << endl;
        break;
    case 9:
        cout << "You selected the" << " " << plays[8] << " " << "play" << endl;
        break;
    case 10:
        cout << "You selected the" << " " << plays[9] << " " << "play" << endl;
        break;
    case 11:
        cout << "You selected the" << " " << plays[10] << " " << "play" << endl;
        break;
    case 12:
        cout << "You selected the" << " " << plays[11] << " " << "play" << endl;
        break;
    case 13:
        cout << "You selected the" << " " << plays[12] << " " << "play" << endl;
        break;
    case 14:
        cout << "You selected the" << " " << plays[13] << " " << "play" << endl;
        break;
    case 15:
        cout << "You selected the" << " " << plays[14] << " " << "play" << endl;
        break;
    case 16:
        cout << "You selected the" << " " << plays[15] << " " << "play" << endl;
        break;
    case 17:
        cout << "You selected the" << " " << plays[16] << " " << "play" << endl;
        break;
    case 18:
        cout << "You selected the" << " " << plays[17] << " " << "play" << endl;
        break;
    case 19:
        cout << "You selected the" << " " << plays[18] << " " << "play" << endl;
        break;
    case 0:
        std::exit(0);
        break;
    default:
        cout << "Invalid selection!!! , Please enter a number from 1-19 to choose a play!" << endl;
        break;
    }

} while (playbook_choice > 19 || playbook_choice < 1);


return playbook_choice;

}

int function_CPU(int &cpu_random_play) {
// want this function to keep to looping and the cpu keep calling plays until downs > 4 -- with a pause in between
// so the end user can see what the cpu is calling

do {

    //have CPU call a random play from the list -- not dependent on down and distance or offense/defense attribution
    srand((unsigned)time(NULL));

    //generate random 1 or 2 -- either qb sneak or off guard run
    cpu_random_play = rand() % 2 + 1; 

    //calls this function which has a menu to a choose but then stops at the cin prompt
    //from within that function

    //for the player 1 function works perfectly but when the cpu is trying to access cin, prompt is
    //waiting for user input and doesnt get the cpu input

    playbook(playbook_choice);

    //??? How to make the CPU automatically select a play!!!!!!!!
    if (cpu_random_play == 1) {     
        //chooses the play 1 from the playbook menu
        playbook_choice = 1;        
        playbook_qb_sneak(result, down, yards_to_go);   
    }

    if (cpu_random_play == 2) {
        //chooses the play 2 from the playbook menu
        playbook_choice = 2;
        off_guard_run(result, down, yards_to_go);
    }


}while (player_control == 2 && down <= 4);

return cpu_random_play;

}

\$\endgroup\$
  • \$\begingroup\$ Why are you trying to return multiple values? Your function only returns an int. \$\endgroup\$ – user1118321 Dec 15 '18 at 5:45
  • \$\begingroup\$ Calling any AI or automated process a CPU is fine when you’re talking about a game with players, but very confusing when asking a question about programming. (The CPU is a component of your computer, not the coach-AI I’m assuming you mean.) \$\endgroup\$ – Bruno Ely Dec 15 '18 at 6:27
  • \$\begingroup\$ It’s hard to tell what’s going on without the complete code. What’s in playbook(...)? \$\endgroup\$ – Bruno Ely Dec 15 '18 at 6:31

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.