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

Source code in the icon programming language

procedure main ()
  lower_limit := 1
  higher_limit := 100
  
  write ("Think of a number between 1 and 100")
  write ("Press 'enter' when ready")
  read ()

  repeat {
    if (higher_limit < lower_limit) 
      then { # check that player is not cheating!
        write ("Something has gone wrong ... I give up")
        exit ()
      }
    my_guess := (higher_limit + lower_limit) / 2
    write ("My guess is ", my_guess)
    write ("Enter 'H' if your number is higher, 'L' if lower, or 'E' if equal")
    reply := map(trim(read ()))
    case (reply) of {
      "e" : { 
        write ("I got it correct - thankyou!")
        exit () # game over
      }
      "h" : lower_limit := my_guess + 1
      "l" : higher_limit := my_guess - 1
      default : write ("Pardon? Let's try that again")
    }     
  }
end


  

You may also check:How to resolve the algorithm Inheritance/Single step by step in the EMal programming language
You may also check:How to resolve the algorithm Quine step by step in the VHDL programming language
You may also check:How to resolve the algorithm Identity matrix step by step in the Wren programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Nanoquery programming language
You may also check:How to resolve the algorithm Nonogram solver step by step in the Racket programming language