This is a code written is Pascal (Delphi). It asks the user to think of a number between min_ and max_ and then guesses the number using either a Binary or a Linear method.
program BandLSearch;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils;
var
min_, max_: integer; // set the max and min to check between the two (inclusive)
BinorLin: string; // Get user input as to which method they wish to use
procedure Linear();
{
This uses a linear search technique to guess the number.
It will ask min_ then min_ +1 etc up to max_. e.g. 1, 2, 3, ... , 99, 100
}
var
input: string;
count: integer; // For the foor loop
begin
for count := min_ to max_ do // loop from min to max and ask if the number is corect, one by one
begin
write('Is your number ',count,'? Y or N? > ');
readln(input);
if (input = 'Y') or (input = 'y') then
begin
writeln('Found your number, it is ',count);
readln; // Prevent the code ending without being able to read the output.
break
end
else
begin
if count = max_ then //If they have reached max and not said yes, they are lying
begin
writeln('You have cheated');
readln;
end;
end;
end;
end;
procedure Binary();
{
This uses a linear search technique to guess the number
It will ask (min_ + max_)/2 and the user says too high or too low.
Depending on the response, it will set either min_ or max_ to the guessed number
Eventually it narrows down, eg:
50, 75, 62, 68, 65, 63, 64
L H L H H L C
}
var
input: string;
guess, preGuess: integer; // For the current computer guess
goesTaken : integer; // To monitor the guesses needed
end_: integer; // Will increase for each guess that max_ = min_
begin
preGuess := -1; // Will not be the guess.
goesTaken := 0;
end_ := 0;
while end_ < 2 do //when it is 2, it will have guessed all the numbers.
begin
inc(goesTaken);
guess := (max_ + min_) div 2;
writeln('Is your number ',guess,'?');
writeln('If guess is too high enter H, if it''s too low, enter L.');
write('If the guess is corect, enter C > ');
readln(input);
if input = 'H' then // Number is too high, so guess lower
max_ := guess - 1
else
if input = 'L' then // Number is too low, so guess higher
min_ := guess + 1
else // Number is correct
begin
writeln('Found your number, it is ',guess);
writeln('It took me ',goesTaken,' goes.');
readln;
break
end;
if guess = preGuess then // If the computer asks the same number two times in a row, the user has cheated.
begin
writeln('You have cheated');
readln;
break
end
else
preGuess := guess;
if (max_ = min_) then // If min is the same as max, then 1 go later the code can exit.
begin
end_ := end_ + 1;
end;
end;
end;
begin
min_ := 0;
max_ := 100;
writeln('Think of a number between ',min_, ' and ', max_, '. I will guess it.');
write('Do you want to use a (l)inear or a (b)inary search? > ');
readln(BinorLin);
if BinorLin = 'l' then // User input comparison
begin
writeln('Starting Linear Search...');
Linear();
end
else;
begin
if BinorLin = 'b' then
begin
writeln('Starting Binary Search...');
Binary();
end
else // didn't enter 'l' or 'b'
begin
exit
end;
end;
end.
This is a high school assignment, however I have already handed it in (today) for marking. I'd be interested in the opinion of more than my teacher.