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

Source code in the freebasic programming language

' FB 1.05.0 Win64

Dim hle As String
Dim lowest As Integer = 1
Dim highest As Integer = 20
Dim guess As Integer = 10
Print "Please choose a number between 1 and 20 but don't tell me what it is yet"
Print
Do
  Print "My guess is"; guess
  Do
    Input "Is this higher/lower or equal to your chosen number h/l/e : "; hle
    hle = LCase(hle)
    If hle = "l" AndAlso guess = highest Then
      Print "It can't be more than"; highest; ", try again"
      hle = "i" '' invalid
    ElseIf hle = "h" AndAlso guess = lowest Then
      Print "It can't be less than"; lowest; ", try again"
      hle = "i"
    End If    
  Loop Until hle = "h" OrElse hle = "l" OrElse hle = "e"
  If hle = "e" Then
    Print "Good, thanks for playing the gaame with me!"
    Exit Do
  ElseIf hle = "h" Then
    If highest > guess - 1 Then highest = guess - 1   
  Else
    If lowest < guess + 1  Then lowest = guess + 1
  End If  
  guess = (lowest + highest)\2  
Loop
End

  

You may also check:How to resolve the algorithm Steffensen's method step by step in the Raku programming language
You may also check:How to resolve the algorithm Super-Poulet numbers step by step in the Wren programming language
You may also check:How to resolve the algorithm Associative array/Merging step by step in the Go programming language
You may also check:How to resolve the algorithm Vigenère cipher step by step in the Vedit macro language programming language
You may also check:How to resolve the algorithm Rare numbers step by step in the Wren programming language