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

Source code in the maxscript programming language

inclusiveRange = [1,100]
lowRange = inclusiveRange.x
maxRange = inclusiveRange.y
guesses = 1
inf =  "Think of a number between % and % and I will try to guess it.\n" +\
		"Type -1 if the guess is less than your number,\n"+\
		"0 if the guess is correct, " +\
		"or 1 if it's too high.\nPress esc to exit.\n"
clearListener()
format inf (int lowRange) (int maxRange)
while not keyboard.escpressed do
(
	local chosen = ((lowRange + maxRange) / 2) as integer
	if lowRange == maxRange do format "\nHaving fun?"
	format "\nI choose %.\n" chosen
	local theAnswer = getKBValue prompt:"Answer? "
	case theAnswer of
	(
		(-1): (lowRange = chosen; guesses += 1)
		(0): (format "\nYay. I guessed your number after % %.\n" \
				guesses (if guesses == 1 then "try" else "tries")
				exit with OK)
		(1): (maxRange = chosen; guesses += 1)
		default: (format "\nI don't understand your input.")
	)
)

OK
Think of a number between 1 and 100 and I will try to guess it.
Type -1 if the guess is less than your number,
0 if the guess is correct, or 1 if it's too high.
Press esc to exit.
OK

I choose 50.
Answer?  -1
I choose 75.
Answer?  -1
I choose 87.
Answer?  1
I choose 81.
Answer?  -1
I choose 84.
Answer?  -1
I choose 85.
Answer?  0
Yay. I guessed your number after 6 tries.
OK

  

You may also check:How to resolve the algorithm Hello world/Standard error step by step in the Swift programming language
You may also check:How to resolve the algorithm Make directory path step by step in the FutureBasic programming language
You may also check:How to resolve the algorithm String prepend step by step in the Wart programming language
You may also check:How to resolve the algorithm Pseudo-random numbers/Middle-square method step by step in the Sidef programming language
You may also check:How to resolve the algorithm Loops/Continue step by step in the REBOL programming language