popei
asked on
help
please tell me how to doo this:
writw a program for a game in which the user guesses what random number between 1 and 1000 the computer has 'thought',untill he or she has found the correct number. The computer should tell the user for each guess whether it was too high, too low or spot on1 The program should also print out how many guesses the user had before the right answer.
writw a program for a game in which the user guesses what random number between 1 and 1000 the computer has 'thought',untill he or she has found the correct number. The computer should tell the user for each guess whether it was too high, too low or spot on1 The program should also print out how many guesses the user had before the right answer.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Never mind, it is deep into december there are no more classes. Ok, here it comes:
It is very commented. every line nearly. :) so don't panic.
program Randomnumbers; {procedureless guessing game, simple as possible by G. du Toit g_dutoit@hotmail.com}
uses
crt;
var
UserNumber : longint; {declaring variable which the user will input to}
CPUNumber : longint; {declaring variable which the CPU will input it's random number }
RangeMin, RangeMax : longint; {declaring variables which the user inputs his desired limits }
Counter : integer; {I am sure no person will take 100 guesses or more. if so make into longint}
begin
clrscr; {guess}
randomize; {init randomizing}
write('Between what numbers do you want to guess in:');
read(RangeMin, RangeMax); {can be written read(RangeMin); read(RangeMax)}
CPUNumber:=random(RangeMax -RangeMin) +RangeMin;
{The user can type 10 20 or 10 return press 20; both will work.}
{random(x); is the random definition, it generates a random number from
0 -> x; Now, for min and max. random(RangeMax) is from 0->RangeMax
but
random(RangeMax) 0->Max random(RangeMax-RangeMin)+ RangeMin
say for instance
min 10 max 15
random(15) will produce 0->15 this is below 10,
random(15) will produce 0->15 but PLUS min. this is now (0->15)+10
makes 10->25 obvious enough? but now it exceeds the limit.
random(RangeMax-RangeMin)+ RangeMin
NOW Random(15-10) is random(5) 0->5, now Plus 10 10->15, hey presto
a limit.}
repeat
write('Guess: '); {prompts 'guess'}
Counter:=Counter+1; { or inc counter this climbs by one everytime this
loop passes by this instruction}
read(UserNumber); {reads user 's number}
if (UserNumber>CPUNumber) then {checking if User number is greater than Comp's}
writeln('Smaller');
if (UserNumber<CPUNumber) then {Checking if smaller, then prompt bigger}
writeln('Bigger');
if (UserNumber=CPUNumber) then {check for equality}
writeln('Spot ON! :) The number of guesses: ', Counter); {displays amount of guesses}
until ((CPUNumber=UserNumber) or (UserNumber=-1)); {repeat fall out statement}
{the conditions for loop to end, if equality or if user gives up.}
if (UserNumber=-1) then
write('Why did you give up! Here is my number: ',CPUNumber); {diplays message with cpu number}
readkey; {checks for keyboard input before ending, so that you may read the previous message;}
{last semicolon not necessary but good practice. Hope this not an assignment that you were given:)
}
end.
{There it is fully commented}
It is very commented. every line nearly. :) so don't panic.
program Randomnumbers; {procedureless guessing game, simple as possible by G. du Toit g_dutoit@hotmail.com}
uses
crt;
var
UserNumber : longint; {declaring variable which the user will input to}
CPUNumber : longint; {declaring variable which the CPU will input it's random number }
RangeMin, RangeMax : longint; {declaring variables which the user inputs his desired limits }
Counter : integer; {I am sure no person will take 100 guesses or more. if so make into longint}
begin
clrscr; {guess}
randomize; {init randomizing}
write('Between what numbers do you want to guess in:');
read(RangeMin, RangeMax); {can be written read(RangeMin); read(RangeMax)}
CPUNumber:=random(RangeMax
{The user can type 10 20 or 10 return press 20; both will work.}
{random(x); is the random definition, it generates a random number from
0 -> x; Now, for min and max. random(RangeMax) is from 0->RangeMax
but
random(RangeMax) 0->Max random(RangeMax-RangeMin)+
say for instance
min 10 max 15
random(15) will produce 0->15 this is below 10,
random(15) will produce 0->15 but PLUS min. this is now (0->15)+10
makes 10->25 obvious enough? but now it exceeds the limit.
random(RangeMax-RangeMin)+
NOW Random(15-10) is random(5) 0->5, now Plus 10 10->15, hey presto
a limit.}
repeat
write('Guess: '); {prompts 'guess'}
Counter:=Counter+1; { or inc counter this climbs by one everytime this
loop passes by this instruction}
read(UserNumber); {reads user 's number}
if (UserNumber>CPUNumber) then {checking if User number is greater than Comp's}
writeln('Smaller');
if (UserNumber<CPUNumber) then {Checking if smaller, then prompt bigger}
writeln('Bigger');
if (UserNumber=CPUNumber) then {check for equality}
writeln('Spot ON! :) The number of guesses: ', Counter); {displays amount of guesses}
until ((CPUNumber=UserNumber) or (UserNumber=-1)); {repeat fall out statement}
{the conditions for loop to end, if equality or if user gives up.}
if (UserNumber=-1) then
write('Why did you give up! Here is my number: ',CPUNumber); {diplays message with cpu number}
readkey; {checks for keyboard input before ending, so that you may read the previous message;}
{last semicolon not necessary but good practice. Hope this not an assignment that you were given:)
}
end.
{There it is fully commented}
Never mind, it is deep into december there are no more classes. Ok, here it comes: Sorry about the duplicate, I lost all the indents.
It is very commented. every line nearly. :) so don't panic.
program Randomnumbers; {procedureless guessing game, simple as possible by G. du Toit g_dutoit@hotmail.com}
uses
crt;
var
UserNumber : longint; {declaring variable which the user will input to}
CPUNumber : longint; {declaring variable which the CPU will input it's random number }
RangeMin, RangeMax : longint; {declaring variables which the user inputs his desired limits }
Counter : integer; {I am sure no person will take 100 guesses or more. if so make into longint}
begin
clrscr; {guess}
randomize; {init randomizing}
write('Between what numbers do you want to guess in:');
read(RangeMin, RangeMax); {can be written read(RangeMin); read(RangeMax)}
CPUNumber:=random(RangeMax -RangeMin) +RangeMin;
{The user can type 10 20 or 10 return press 20; both will work.}
{random(x); is the random definition, it generates a random number from
0 -> x; Now, for min and max. random(RangeMax) is from 0->RangeMax
but
random(RangeMax) 0->Max random(RangeMax-RangeMin)+ RangeMin
say for instance
min 10 max 15
random(15) will produce 0->15 this is below 10,
random(15) will produce 0->15 but PLUS min. this is now (0->15)+10
makes 10->25 obvious enough? but now it exceeds the limit.
random(RangeMax-RangeMin)+ RangeMin
NOW Random(15-10) is random(5) 0->5, now Plus 10 10->15, hey presto
a limit.}
repeat
write('Guess: '); {prompts 'guess'}
Counter:=Counter+1; { or inc counter this climbs by one everytime this
loop passes by this instruction}
read(UserNumber); {reads user 's number}
if (UserNumber>CPUNumber) then {checking if User number is greater than Comp's}
writeln('Smaller');
if (UserNumber<CPUNumber) then {Checking if smaller, then prompt bigger}
writeln('Bigger');
if (UserNumber=CPUNumber) then {check for equality}
writeln('Spot ON! :) The number of guesses: ', Counter); {displays amount of guesses}
until ((CPUNumber=UserNumber) or (UserNumber=-1)); {repeat fall out statement}
{the conditions for loop to end, if equality or if user gives up.}
if (UserNumber=-1) then
write('Why did you give up! Here is my number: ',CPUNumber); {diplays message with cpu number}
readkey; {checks for keyboard input before ending, so that you may read the previous message;}
{last semicolon not necessary but good practice. Hope this not an assignment that you were given:)
}
end.
{There it is fully commented}
It is very commented. every line nearly. :) so don't panic.
program Randomnumbers; {procedureless guessing game, simple as possible by G. du Toit g_dutoit@hotmail.com}
uses
crt;
var
UserNumber : longint; {declaring variable which the user will input to}
CPUNumber : longint; {declaring variable which the CPU will input it's random number }
RangeMin, RangeMax : longint; {declaring variables which the user inputs his desired limits }
Counter : integer; {I am sure no person will take 100 guesses or more. if so make into longint}
begin
clrscr; {guess}
randomize; {init randomizing}
write('Between what numbers do you want to guess in:');
read(RangeMin, RangeMax); {can be written read(RangeMin); read(RangeMax)}
CPUNumber:=random(RangeMax
{The user can type 10 20 or 10 return press 20; both will work.}
{random(x); is the random definition, it generates a random number from
0 -> x; Now, for min and max. random(RangeMax) is from 0->RangeMax
but
random(RangeMax) 0->Max random(RangeMax-RangeMin)+
say for instance
min 10 max 15
random(15) will produce 0->15 this is below 10,
random(15) will produce 0->15 but PLUS min. this is now (0->15)+10
makes 10->25 obvious enough? but now it exceeds the limit.
random(RangeMax-RangeMin)+
NOW Random(15-10) is random(5) 0->5, now Plus 10 10->15, hey presto
a limit.}
repeat
write('Guess: '); {prompts 'guess'}
Counter:=Counter+1; { or inc counter this climbs by one everytime this
loop passes by this instruction}
read(UserNumber); {reads user 's number}
if (UserNumber>CPUNumber) then {checking if User number is greater than Comp's}
writeln('Smaller');
if (UserNumber<CPUNumber) then {Checking if smaller, then prompt bigger}
writeln('Bigger');
if (UserNumber=CPUNumber) then {check for equality}
writeln('Spot ON! :) The number of guesses: ', Counter); {displays amount of guesses}
until ((CPUNumber=UserNumber) or (UserNumber=-1)); {repeat fall out statement}
{the conditions for loop to end, if equality or if user gives up.}
if (UserNumber=-1) then
write('Why did you give up! Here is my number: ',CPUNumber); {diplays message with cpu number}
readkey; {checks for keyboard input before ending, so that you may read the previous message;}
{last semicolon not necessary but good practice. Hope this not an assignment that you were given:)
}
end.
{There it is fully commented}
well, yet another solution :P I think comments are not necessary.
Program Guess_Test;
uses
crt;
const
MAX_TRIES = 100;
var
CorrectTip,
CurrentTipVal : Word;
CurrentTipStr : string[4];
Counter : Byte;
Found : Boolean;
Code : Integer;
begin
ClrScr;
Randomize;
CorrectTip := Random(1001);
if CorrectTip = 0 then
CorrectTip := 1;
Counter := 1;
Found := False;
repeat
WriteLn;
WriteLn('Enter your Tip (',Counter,') : ');
ReadLn(CurrentTipStr);
if CurrentTipStr = '' then
WriteLn('You didn''t enter anything');
Val(CurrentTipStr, CurrentTipVal, Code);
if Code <> 0 then
begin
if ((CurrentTipStr = 'q') or (CurrentTipStr = 'Q')) then
begin
WriteLn('You aborted after attempt #',Counter);
Halt(1);
end
else
WriteLn('hmh, that was not a valid Number');
end
else
begin
if CurrentTipStr <> '' then
begin
if CurrentTipVal < CorrectTip then
WriteLn('hmh, that was too less');
if CurrentTipVal > CorrectTip then
WriteLn('hmh, that was too high');
if CurrentTipVal = CorrectTip then
begin
Found := True;
WriteLn('CORRECT NUMBER FOUND');
end;
if not Found then
inc(Counter);
end;
end;
until ((Counter = MAX_TRIES) or (Found));
if Counter = MAX_TRIES then
begin
WriteLn('You could not find the Number after 100 attempts');
halt(0);
end;
if Found then
begin
WriteLn('You have found the Number after ',Counter,' attempts');
halt(0);
end;
end.
Program Guess_Test;
uses
crt;
const
MAX_TRIES = 100;
var
CorrectTip,
CurrentTipVal : Word;
CurrentTipStr : string[4];
Counter : Byte;
Found : Boolean;
Code : Integer;
begin
ClrScr;
Randomize;
CorrectTip := Random(1001);
if CorrectTip = 0 then
CorrectTip := 1;
Counter := 1;
Found := False;
repeat
WriteLn;
WriteLn('Enter your Tip (',Counter,') : ');
ReadLn(CurrentTipStr);
if CurrentTipStr = '' then
WriteLn('You didn''t enter anything');
Val(CurrentTipStr, CurrentTipVal, Code);
if Code <> 0 then
begin
if ((CurrentTipStr = 'q') or (CurrentTipStr = 'Q')) then
begin
WriteLn('You aborted after attempt #',Counter);
Halt(1);
end
else
WriteLn('hmh, that was not a valid Number');
end
else
begin
if CurrentTipStr <> '' then
begin
if CurrentTipVal < CorrectTip then
WriteLn('hmh, that was too less');
if CurrentTipVal > CorrectTip then
WriteLn('hmh, that was too high');
if CurrentTipVal = CorrectTip then
begin
Found := True;
WriteLn('CORRECT NUMBER FOUND');
end;
if not Found then
inc(Counter);
end;
end;
until ((Counter = MAX_TRIES) or (Found));
if Counter = MAX_TRIES then
begin
WriteLn('You could not find the Number after 100 attempts');
halt(0);
end;
if Found then
begin
WriteLn('You have found the Number after ',Counter,' attempts');
halt(0);
end;
end.
ASKER
but you did not nclude procedures in the program, making it a lttlie hard to de-bug.
Oh, you wanted procedures, what's to debug anyway, it's fully tested. No bugs, whadda I look like m*cros*ft? :)
I will send ye a copy of a procedurized one if you wish. just ask
I will send ye a copy of a procedurized one if you wish. just ask
I will send it once I know what it is for.