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

Published on 12 May 2024 09:40 PM
#Jq

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

Source code in the jq programming language

# Pick an integer from $lowest through $highest ...
def play($lowest; $highest):
  # Helper function for guessing
  def prompt:
    .guess = (((.lowest + .highest)/2)|floor)
    | .emit += "\nMy guess is \(.guess)." +
               "\nIs this higher/lower than or equal to your chosen number?  h/l/e : " ;
  
  "Please choose a number from \($lowest) to \($highest) inclusive and then answer the questions.",
  ( {$highest, $lowest}
    | prompt
    | .emit,
      ( label $out  
        | foreach inputs as $in (.;
	  .emit = null
          | .hle = ($in|ascii_downcase)
          | if .hle == "l" and .guess == .highest
            then .emit = "It can't be more than \(highest), try again."
            elif .hle == "h" and .guess == .lowest
            then .emit = "It can't be less than \(.lowest), try again."
            elif .hle == "e"
            then .emit = "Good, thanks for playing the game with me!"
	    | .quit = true
            elif .hle == "h"
            then if (.highest > .guess - 1) then .highest = .guess - 1
                 else .
		 end
	    elif .hle == "l"
            then if (.lowest < .guess + 1)  then .lowest = .guess + 1
                 else .
                 end
            else .emit = "Please try again.\n"
	    end
         | if .quit then ., break $out else prompt end ;
      .emit
      ) ) ) ;

def play: play(1;20);

play

  

You may also check:How to resolve the algorithm Sleep step by step in the Ursa programming language
You may also check:How to resolve the algorithm Loops/For step by step in the LiveCode programming language
You may also check:How to resolve the algorithm Fibonacci n-step number sequences step by step in the EchoLisp programming language
You may also check:How to resolve the algorithm XML/Output step by step in the Perl programming language
You may also check:How to resolve the algorithm Weird numbers step by step in the Quackery programming language