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

The Ruby code you provided includes two methods for guessing a number between a given range:

  1. play method:

    • This method takes three parameters: low and high (the range), and turns (the number of turns, initialized to 1 by default).
    • It calculates the midpoint num as the average of low and high.
    • It prints num and asks the user if it's the correct guess.
    • Depending on the user's response, it calls itself recursively to search for the correct number in a smaller range:
      • If the user says it's too high, it calls play with a lower high value (excluding num).
      • If the user says it's too low, it calls play with a higher low value (including num).
      • If the user says it's correct, it prints that the number was found in turns turns.
    • This method uses a helper method is_it? to compare the guessed number num with the secret number $number.
  2. bsearch method:

    • This method demonstrates a more efficient way to search for the correct number using Ruby's Enumerable#bsearch method, which takes a block that compares elements in the given range.
    • It sets up a range from 1 to 100 and generates a secret number within that range.
    • It initializes turns to 0 and enters a loop:
      • Inside the loop, it prints the current guess and increments turns.
      • It compares the secret number to the current guess using the <=> operator, which returns -1 if the secret is lower, 0 if it's equal, and 1 if it's higher.
      • Based on the result of the comparison, it prints whether the guess is too low, too high, or correct.
      • If the guess is correct, it breaks out of the loop and prints the number of turns taken to find the number.

The two methods demonstrate different ways to perform binary search algorithms for guessing a number within a range. The play method employs recursion, while the bsearch method utilizes Ruby's built-in binary search function. Both methods narrow down the range with each guess until the correct number is found.

Source code in the ruby programming language

def play(low, high, turns=1)
  num = (low + high) / 2
  print "guessing #{num}\t"
  case is_it?(num)
  when 1
    puts "too high"
    play(low, num - 1, turns + 1)
  when -1
    puts "too low"
    play(num + 1, high, turns + 1)
  else
    puts "found the number in #{turns} turns."
  end
end

def is_it?(num)
  num <=> $number
end

low, high = 1, 100
$number = rand(low .. high)

puts "guess a number between #{low} and #{high}"
play(low, high)


r = (1..100)
secret = rand(r)
turns = 0
 
puts "Guess a number between #{r.min} and #{r.max}"
r.bsearch do |guess|                # bsearch works on ranges
  print "Guessing #{guess} \t"
  turns += 1
  low_high = secret <=> guess       # -1, 0, or 1
  puts ["found the number in #{turns} turns", "too low", "too high"][low_high]
  low_high
end


  

You may also check:How to resolve the algorithm Inconsummate numbers in base 10 step by step in the 11l programming language
You may also check:How to resolve the algorithm Leap year step by step in the ERRE programming language
You may also check:How to resolve the algorithm Sorting algorithms/Selection sort step by step in the Quackery programming language
You may also check:How to resolve the algorithm Sierpinski pentagon step by step in the Haskell programming language
You may also check:How to resolve the algorithm Repeat step by step in the J programming language