Looping from user to computer

Hi everyone,

I had a question concerning looping between the user and the computer.

My assignment for a class project is to have input from the user, switch to the computer, and then switch back to the user, and so on and so forth.

Would I need to use a do-while loop?

Thank you!
I'm sorry but what?

Request input from user is simply
cout <<'Request>> ";
cin >> variable;

but what do you mean switch back to the computer? How is the computer supposed to generate an input?

But as far as do-while, I would reccommend using this only if you are certain that the code you are including inside the do needs to be executed at least once in all cases.
The game allows a human to play against the computer.
I have to code to receive input from the human and then randomize numbers for the computer.

How would I code for the game to switch from the human to the computer?


For example:

(the game would go like this):

Welcome!

group A: 15 group B:25

human
"Which group would you like to take from?" A
"How many items would you like to take from the group? 3

group A: 12 group B:25

computer
"Computer removes 4 items from group B."

group A: 12 group B:21

human...
etc...

I hope you understand what I mean.
If every human action is followed by an action by the computer, simply put them both sequentially in your loop:
1
2
3
4
5
while (!stop) {
  // Human actions

  // Computer actions
}


However, if that guarantee is not given (e.g. the game can quit after the human player's turn, thus skipping the computer's turn), you can use a control variable:
1
2
3
4
5
6
7
8
9
10
bool human_turn = true; // Human player begins
while (!stop) {
  if (human_turn) {
    // Human actions
  }
  else {
    // Computer actions
  }
  human_turn = !human_turn;
}

I see. That makes more sense.

So I presume you are allowed to use rand() right?

would the following eaxmple work?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

do{
      cout <<"Group? >>"
      cin >> grpSel // a char varaible
      cout << "How many?>> "
      cin >> takeSel // an int variable
/*
An if else conditional to take 'takeSel' from selected group
*/

// computer part
srand(time(NULL)); // seed the RNG with current time from Jan 1 1970, different on each                         
                               //   execution.
int rng1=rand()%100;
int rng2= rand()%3 // or any other type of operation you like
if (rng1>=50) { grpSel=='B' // or A your choice};
else {grpSel=='A' // or v.v.}
/* Some modifier of rng2 to get a number between 0 and the max you want the computer to take and then the following operations for the taken numbers */

// change of the terminating test here. [i.e. sentinel value not reached etc.]

}while('terminating test')// the test need be included so that the game stops when the 
                                       // conditions you define are reached 
@ Gaminic:

so is "!stop" a variable??



while (!stop) {
// Human actions

// Computer actions
}
stop is a boolian variable initialized to false. You would need a statement in the while loop to check it after each turn, to see if the stop condition is reached. the ! is the not operator and produces the opposite of the variable inquestion[ i.e. if var=true then !var = false, if var=false then !var=true]

[in case you need it ->
declaring stop:
bool stop=false; ]
Last edited on
Topic archived. No new replies allowed.