How to resolve the algorithm Guess the number/With feedback (player) step by step in the Modula-2 programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Guess the number/With feedback (player) step by step in the Modula-2 programming language
Table of Contents
Problem Statement
Write a player for the game that follows the following rules: The computer should guess intelligently based on the accumulated scores given. One way is to use a Binary search based algorithm.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Guess the number/With feedback (player) step by step in the Modula-2 programming language
Source code in the modula-2 programming language
MODULE raden;
IMPORT InOut;
VAR done, ok : BOOLEAN;
guess, upp, low : CARDINAL;
res : CHAR;
BEGIN
InOut.WriteString ("Choose a number between 0 and 1000.");
InOut.WriteLn;
InOut.WriteLn;
upp := 1000;
low := 0;
REPEAT
ok := FALSE;
guess := ( ( upp - low ) DIV 2 ) + low;
InOut.WriteString ("My guess is"); InOut.WriteCard (guess, 4); InOut.Write (11C);
InOut.WriteString ("How did I score? 'L' = too low, 'H' = too high, 'Q' = OK : ");
InOut.WriteBf;
REPEAT
InOut.Read (res);
res := CAP (res)
UNTIL (res = 'Q') OR (res = 'L') OR (res = 'H');
CASE res OF
'L' : low := guess |
'H' : upp := guess
ELSE
ok := TRUE
END;
UNTIL ok;
InOut.WriteString ("So the number is"); InOut.WriteCard (guess, 4);
InOut.WriteLn;
InOut.WriteString ("Thanks for letting me play with you.");
InOut.WriteLn
END raden.
You may also check:How to resolve the algorithm Longest common substring step by step in the Phix programming language
You may also check:How to resolve the algorithm 99 bottles of beer step by step in the Intercal programming language
You may also check:How to resolve the algorithm Partition an integer x into n primes step by step in the C programming language
You may also check:How to resolve the algorithm Maze generation step by step in the PHP programming language
You may also check:How to resolve the algorithm Index finite lists of positive integers step by step in the Wren programming language