How to resolve the algorithm Guess the number/With feedback (player) step by step in the Julia programming language

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Guess the number/With feedback (player) step by step in the Julia 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 Julia programming language

The provided Julia code is an implementation of the classic number guessing game, where the computer tries to guess a number that the user has in mind by asking a series of questions and refining its guess based on the user's responses.

A step-by-step explanation of the code:

  1. Input Upper Bound: The code first prompts the user to enter an upper bound for the number they have in mind. This sets the maximum value that the computer can guess. If the entered upper bound is less than 1, it throws a DomainError.

  2. Initialization:

    • lower is initialized to 0, representing the lower bound of the guess range.
    • The input variable is used to read the user's input for the upper bound.
    • upper is parsed as an integer from the user's input and represents the upper bound of the guess range.
    • attempts is initialized to 1, representing the number of attempts the computer has made.
    • The user is instructed to think of a number within the specified range and press ENTER.
  3. Maximum Attempts Calculation:

    • maxattempts is calculated as the rounded integer value of the ceiling of -log(1 / (upper - lower)) / log(2). This formula provides an estimate of the maximum number of attempts required to guess the number within the given range.
  4. Guessing Loop:

    • The code enters a loop that continues until the computer guesses the correct number or exceeds the maximum attempts.
    • In each iteration of the loop:
      • The computer calculates a new guess as the lower bound plus half the difference between the upper and lower bounds, rounded to the nearest integer.
      • It checks if the current guess is the same as the previous guess or if the number of attempts has exceeded the maximum attempts. If either condition is true, it prints an error message and exits the loop.
      • The computer asks the user for feedback on its guess:
        • If the user enters "l", the upper bound is updated to the current guess.
        • If the user enters "h", the lower bound is updated to the current guess.
        • If the user enters "c", it breaks out of the loop, indicating that the computer has guessed the correct number.
  5. Result:

    • After the loop completes, the code prints the number of attempts it took to guess the correct number.

Source code in the julia programming language

print("Enter an upper bound: ")
lower = 0
input = readline()
upper = parse(Int, input)

if upper < 1
    throw(DomainError)
end

attempts = 1
print("Think of a number, ", lower, "--", upper, ", then press ENTER.")
readline()
const maxattempts = round(Int, ceil(-log(1 / (upper - lower)) / log(2)))
println("I will need at most ", maxattempts, " attempts ",
    "(⌈-log(1 / (", upper, " - ", lower, ")) / log(2)⌉ = ",
    maxattempts, ").\n")
previous = -1
guess = -1

while true
    previous = guess
    guess = lower + round(Int, (upper - lower) / 2, RoundNearestTiesUp)

    if guess == previous || attempts > maxattempts
        println("\nThis is impossible; did you forget your number?")
        exit()
    end

    print("I guess ", guess, ".\n[l]ower, [h]igher, or [c]orrect? ")
    input = chomp(readline())
    
    while input ∉ ["c", "l", "h"]
        print("Please enter one of \"c\", \"l\", or \"h\". ")
        input = chomp(readline())
    end

    if input == "l"
        upper = guess
    elseif input == "h"
        lower = guess
    else
        break
    end

    attempts += 1
end

println("\nI win after ", attempts, attempts == 1 ? " attempt." : " attempts.")


  

You may also check:How to resolve the algorithm Nth root step by step in the Picat programming language
You may also check:How to resolve the algorithm Start from a main routine step by step in the Forth programming language
You may also check:How to resolve the algorithm Loops/Do-while step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Map range step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Sum digits of an integer step by step in the C# programming language