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

Explanation:

The provided Wolfram code defines a function guessnumber that plays a guessing game where the user thinks of a number within a specified range and the program tries to guess it.

Function Definition:

  • guessnumber[min0_, max0_] is a function that takes two parameters:
    • min0: The minimum value of the range in which the user's number can be.
    • max0: The maximum value of the range in which the user's number can be.

Body of the Function:

The body of the function is enclosed within a DynamicModule with three internal variables:

  • min: The current minimum value of the range.
  • max: The current maximum value of the range.
  • guess: The current guess made by the program.
  • correct: A flag that indicates whether the program has correctly guessed the number.

Guess Logic:

The program's guess is calculated dynamically as guess[] := Round@Mean@{min, max}. This means that the guess is initially the average of the current range and is updated whenever the range changes.

User Interface:

The user interface is created using the Dynamic construct. It consists of the following elements:

  • If correct is True, it displays a message indicating that the program has correctly guessed the number.
  • Otherwise, it displays:
    • A text row indicating the program's current guess.
    • A row of buttons that allow the user to indicate whether the guess is "too high," "too low," or "correct."

Usage:

To use the program, call the guessnumber function with the desired range as arguments, like guessnumber[1, 100] in the provided code. This starts the game, where you think of a number within the specified range and interact with the user interface to provide feedback on the program's guesses until it correctly guesses your number.

Source code in the wolfram programming language

guessnumber[min0_, max0_] := 
  DynamicModule[{min = min0, max = max0, guess, correct = False}, 
   guess[] := Round@Mean@{min, max}; 
   Dynamic@If[correct, Row@{"Your number is ", guess[], "."}, 
     Column@{Row@{"I guess ", guess[], "."}, 
       Row@{Button["too high", max = guess[]], 
         Button["too low", min = guess[]],
         Button["correct", correct = True]}}]];
guessnumber[1, 100]


  

You may also check:How to resolve the algorithm Shell one-liner step by step in the Huginn programming language
You may also check:How to resolve the algorithm Brazilian numbers step by step in the AWK programming language
You may also check:How to resolve the algorithm Babbage problem step by step in the RPL programming language
You may also check:How to resolve the algorithm Terminal control/Display an extended character step by step in the Befunge programming language
You may also check:How to resolve the algorithm Input loop step by step in the Modula-3 programming language