The game to play is a well known number guessing game. Alice asks Bob to pick a secret number between 1 and 15. When Bob has selected his number, Alice will guess it. If Alice's guess is correct Bob will respond
Heres what i can see at the moment:
If its a two player game, wont you want bob to enter his secret number? how does bob know what his number is? He has no clue whether to press higher,lower, or correct. A great way to add one onto a variable is using the symbols '++', this is how c++ got its name actually. Its c + 1 in math terms(because its better than c!)
About the code:
your best bet is to use a do while loop, so you ensure the code runs at least once. Here is an empty shell of what this program might look like(from my understanding)
//varibles you'll need:
// store the guess
// store the secret number
// store the answer from bob
// record the amount of attempts
// use a true false variable to run the while loop of the game
//Ask bob for the secret number
//leave this out of the loop, bob only needs to enter his number once
do{
// ask for a guess
// store the input into a variable
// ask bob what to do
// store his input
// create a case(or nested if's if you have not learned cases yet) to find what bob chose
// display his choice on the screen
// break from the case/if statements to prevent unnecessary code running
// add one onto the attempts
}while(//your true false varible)
//display a winning message!
This program will have to guess bobs secrete number in his mind while alice will ask him if her guess his higher lower or correct?
The problem here is i don't know which loop to use and how to write the code.
The program or alice will have to give up at ten attempts.
Also the program will also detect a cheat if bob cheated.For example,alice enters a number and ask bob if the secrete number is higher or lower or correct.if alice enters 10 and bob says higher and alice enters 12 again and bob says lower and alice enter 11 and bob says lower again.Thats a cheat cause his answer should have been 11 .....
Please can someone write this simple program for me cause im taking 2 wiks now doing this assignment
so lets say you are guessing a random number below 10. we need a for loop here.
the standard for loop structure is
1 2 3
for (<#initialization#>; <#condition#>; <#increment#>) {
<#statements#>
}
so lets start it off with int i; for(int i = 0; ;i++)
we still need the condition. lets get a Boolean variable. for(int i = 0; !guessed; i++)now that we have a loop going lets get the code working.
this loop will guess a random number feeding rand an incremented variable. and will end when the number matches the randomly generated number. note that i+1 is the number of guesses if you wanted to keep track of those.
i will not just hand you the code. but i can help u think through it.
so you need the bool operator OR which is ||
|| OPERATOR
a___b___a || b
true|true|true
true|false|true
false|true|true
false|false|false
for your check statement to see if the persons number is below 15.
1 2 3 4 5 6
int i = 0;//your number
while(i > 15 || i <= 0){
std::cout << "enter a number between 0 and 15: ";
std::cin >>number;
//you should make this ALOT more user friendly
}
and that for loop i gave you above is a perfect outline for guessing the number.
The program doesn't check if the persons number is below or above 15.This program only ask(to bob) if his secrete number is higher ,lower or correct if the user input the guess number.
If correct then display congratulation u won.
If the user guess the secret number and fails at ten attempts the program displays ''i give up''
Also the program must be able to detect cheat also.I'v given example above how to detect cheat.
please can you correct the code with better loop or conditions
The program should end if you guess the number and fails at ten attempts
Also this program will also be able to detect cheat....for example the guess number is 7 and you ask someone is this the number your thinking of?
And he/she reply and says no my number is lower than that number. and you guess 5 and he/she says higher and you guess 6 and he/she says higher again while the answer should have been 6....Thats a cheat
this forum is not for doing your homework for you, it is to ask questions. i have provided functions to use. explanation and setup for what you are doing. that is all the help short of cheating and doing the work for you.
http://www.cplusplus.com/doc/tutorial/variables/
second page on this website under basics of c++ has bool data types in it. the only i used that you did not use in your program was a for loop which is still really basic, and a switch which is just a easy as a while loop.