How to resolve the algorithm Guess the number/With feedback (player) step by step in the Prolog 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 Prolog 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 Prolog programming language
Source code in the prolog programming language
min(1). max(10).
pick_number(Min, Max) :-
min(Min), max(Max),
format('Pick a number between ~d and ~d, and I will guess it...~nReady? (Enter anything when ready):', [Min, Max]),
read(_).
guess_number(Min, Max) :-
Guess is (Min + Max) // 2,
format('I guess ~d...~nAm I correct (c), too low (l), or too high (h)? ', [Guess]),
repeat,
read(Score),
( Score = l -> NewMin is Guess + 1, guess_number(NewMin, Max)
; Score = h -> NewMax is Guess - 1, guess_number(Min, NewMax)
; Score = c -> writeln('I am correct!')
; writeln('Invalid input'),
false
).
play :-
pick_number(Min, Max),
guess_number(Min, Max).
?- play.
Pick a number between 1 and 10, and I will guess it...
Ready? (Enter anything when ready):y.
I guess 5...
Am I correct (c), too low (l), or too high (h)? h.
I guess 2...
Am I correct (c), too low (l), or too high (h)? l.
I guess 3...
Am I correct (c), too low (l), or too high (h)? c.
I'm correct!
true
You may also check:How to resolve the algorithm Gamma function step by step in the REXX programming language
You may also check:How to resolve the algorithm Jump anywhere step by step in the PL/SQL programming language
You may also check:How to resolve the algorithm Feigenbaum constant calculation step by step in the 11l programming language
You may also check:How to resolve the algorithm Send email step by step in the Emacs Lisp programming language
You may also check:How to resolve the algorithm Determine if a string has all unique characters step by step in the Rust programming language