I just learned how to make a console application, and as practice, took some very basic source code I found and worked around it.
This is a game to guess a randomized number. The Maximum number is randomly generated each time as well, to keep it well and scrambled. The number of guesses allowed is also randomly generated on each round.
For the record, my best score was 11 guesses where the number was 1373 out of 4672. I only had 11 guesses allowed that round There's a trick to it, but I'll let you figure it out...
Enjoy!
JD Solutions
This is a game to guess a randomized number. The Maximum number is randomly generated each time as well, to keep it well and scrambled. The number of guesses allowed is also randomly generated on each round.
For the record, my best score was 11 guesses where the number was 1373 out of 4672. I only had 11 guesses allowed that round There's a trick to it, but I'll let you figure it out...
Enjoy!
Code:
program GuessANumber;
{$APPTYPE CONSOLE}
uses
SysUtils;
var
Min, Max, Tries, rn, un, cnt: Integer;
Guessed, Passed, DoContinue, DoExit, DoNext: Boolean;
Entered: String;
begin
DoContinue:= True;
while (DoContinue) do begin
Randomize;
Min:= 1;
Max:= Trunc(Random(5000)+100);
Tries:= Trunc(Random(20)+10);
rn:= Trunc(Random(Max-Min)+Min);
WriteLn;
WriteLn('Computer has picked a number from '+
IntToStr(Min)+' to '+IntToStr(Max)+'. Can you guess it?');
WriteLn('You have a total of '+IntToStr(Tries)+' tries!');
Write(' Try a Number: ');
cnt:= 0;
un:= 0;
Guessed:= False;
Passed:= False;
DoExit:= False;
DoNext:= False;
while (Guessed = False) and (Passed = False)
and (DoExit = False) and (DoNext = False) do
begin
inc(Cnt);
ReadLn(Entered);
if UpperCase(Entered) = 'EXIT' then begin
DoExit:= True;
WriteLn(' You gave up!');
WriteLn(' The number was: '+IntToStr(Rn));
end else
if UpperCase(Entered) = 'NEXT' then begin
DoNext:= True;
end else begin
Un:= StrToIntDef(Entered, Min-1);
if Un = Rn then begin
Guessed:= True;
WriteLn(' Correct!');
WriteLn(' It took you '+IntToStr(cnt)+' times to guess!');
end else
if (Cnt >= Tries) and (Guessed = False) then begin
Passed:= True;
WriteLn(' You failed!');
WriteLn(' The number was: '+IntToStr(Rn));
end else
if Un > Rn then begin
Write(' Try Smaller: ');
end else
if Un < Rn then begin
Write(' Try Larger: ');
end else begin
end;
end;
end;
WriteLn;
if not DoNext then begin
Write('Would you like to try again? (Y/N): ');
ReadLn(Entered);
if (UpperCase(Entered) = 'N')
or (UpperCase(Entered) = 'NO') then DoContinue:= False;
end;
end;
WriteLn;
WriteLn('Thank you for playing!');
WriteLn(' Original code found at: ');
WriteLn(' [URL unfurl="true"]http://delphi.about.com/od/objectpascalide/l/aa091101b.htm');[/URL]
WriteLn(' Project Completed by JD: ');
WriteLn(' [URL unfurl="true"]http://www.jdsoftwareinc.com');[/URL]
WriteLn;
WriteLn('Press enter to exit...');
ReadLn;
end.
JD Solutions