Guessing game-help me with my assignment please.....

Pages: 12
due 15 APRIL
The Assignment

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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//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! 


feel free to ask questions
need4sleep wrote:
(because its better than c!)
debatable.
second there is an error here. int number=rand()%15+1;;tell me if you do not see it.
second
1
2
3
4
5
if ( guess || guess>15)

{
cout<< " you cheat"<<endl;
}
guess is of a bool data type? int guess;no. so this will return true IF the guess has been allocated. which is has.
Thanks for helping me.........

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


Last edited on
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.
1
2
3
for(int i = 0; !guessed; i++){
if(rand(i)%10+1 == number) guessed = true; 
}
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.
Last edited on
Thanks very much


Please can you write the codes for me please.....

using for loop.....

End the loop if the guess fails at ten attempts.............


Im getting burining fuse here...hahahahha please help me.........im kinda with this course........

Also the program will must be able to detect cheat.please write the code for me
Doesn't matter if the code doesn't work as expectected

Thanks
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.
Last edited on
using for loop.....

End the loop if the guess fails at ten attempts.............
please write the code.

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

Thanks any help would be appreciated.Thanks

#include<iostream>
#include<cstdlib>
#include<ctime>

using namespace std;

int main()
{

srand(time(0));
int number=rand()%15+1;;
int guess;

char answer;
int attempt=0;


cout << "Guess a number between 1 and 15: " <<endl;
cin >> guess;


while (guess!=number && attempt <=10 )

{
cout << " Is the number High(H),Lower(L) or Correct(C)?:" << endl;
cin >> answer;

cout << " Guess the number again"<<endl;
cin >> guess;


if ( guess != number && attempt > 10)

{
cout<< " I give up"<<endl;
}

attempt=attempt + 1;
}



cout << " Congratulation you win " << endl;

return 0;
}
Last edited on
Please help me..............
1
2
3
4
5
6
7
8
9
  char relation(){
        char c; 
        std::cout << " Is the number High(H),Lower(L) or Correct(C)?:";
        std::cin >> c; 
        return tolower(c); 
        
    }
    
    
gets the letter function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int limit = 15; 
        bool guessed = false; 
        while(!guessed){
            srand(counter++);
            if(counter == 10) {std::cout << "i give up!"; return 0;}
            std::cout << "is your number " << rand()%limit+1; 
            switch(relation()){
                    case 'h':
                   
                    
                    case 'l':
                    
                    
                    
                    case 'c':
                    
                    default: 
                    std::cout << "invalid letter entered";
                    
            }

basic outline for your main function.


1
2
3
4
5
6
7
8
9
10
   
        int getnumber(){
            int i = -1; 
            while(i > 15 && i < 0){
                std::cout << "enter in your number: ";
                std::cin >> i; 
                if(!(i > 15 && i < 0)) std::cout << "you cheated!";
            }
            return i; 
        }
gets the number

this is VERY close to what you need to do. i will not do all of it for you, i just pushed the code i already gave you into functions.
OH thank you..

while(!guessed)

22 C:\Users\User\Desktop\Semester1 Note 2012\CS111\WEEK 4\lab 3\internet.cpp expected unqualified-id before "while"

this is the error i got when runing the program.

I add the header files to the code you have gave me.Can you correct the code again please
make sure you define guessed as
bool guessed = false;
how do you define that?

#include<iostream>
#include<cstdlib>
#include<ctime>

using namespace std;

int main()
{


int min=1,max=15
int guess;
char answer;
char h,l,c;
int attempt=0;


cout << "Guess a number between 1 and 15: " <<endl;
cin >> guess;

while (guess!=number )

{
cout << " Is the number High(H),Lower(L) or Correct(C)?:" << endl;
cin >> answer;
cout << " Guess the number again"<<endl;
cin >> guess;

if (guess==h)
{
number=max-guess;

}

if ( guess==l)
{
number==guess+min;

}

if (guess==c)
{
cout << "congratulation you got it" << endl;
}

else if !(guess >=1 || guess <=15)

cout << " You Cheat"<< endl;






attempt=attempt + 1;





}
return 0;
}



just edit this code and write better conditions to make this program work
Thats all
1
2
3
4
 bool guessed = false;
while(!guessed){
//do stuff here i WILL not hand you your code. 
}
Last edited on
please can you edit this program.

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

So the programe must be able to detect cheat.

please help me
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.
yeap but the one you gave is too hard to understand............were just beginers


Bool and all those stuff are too hard
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.
Last edited on
but how about the code i gave you?

Pages: 12