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

Published on 12 May 2024 09:40 PM
#Go

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

The provided Go code implements a number guessing game. Here's an explanation of each code snippet:

Snippet 1:

  • This code uses the sort.Search function to guess a number between lower and upper (excluding upper).
  • It repeatedly prints a question and reads the user's input ("yes" or "no").
  • The sort.Search function uses a binary search to find the smallest index i such that the function func(i int) bool returns true.
  • In this case, the function checks if the user's number is less than or equal to lower + i.
  • The game ends when the user's number is found, and the guessed number is printed.

Snippet 2:

  • This code plays a more advanced number guessing game.
  • It allows the user to choose a number between lower and upper (including upper).
  • The computer guesses a number, and the user provides feedback by entering "l" (too low), "h" (too high), or "c" (correct).
  • The program uses a loop to repeatedly guess until the user enters "c".
  • The guess is updated based on the user's feedback using binary search.
  • The game ends when the computer correctly guesses the user's number.

Source code in the go programming language

package main

import (
    "fmt"
    "sort"
)

func main() {
    lower, upper := 0, 100
    fmt.Printf(`Instructions:
Think of integer number from %d (inclusive) to %d (exclusive) and
I will guess it. After each guess, I will ask you if it is less than
or equal to some number, and you will respond with "yes" or "no".
`, lower, upper)
    answer := sort.Search(upper-lower, func (i int) bool {
        fmt.Printf("Is your number less than or equal to %d? ", lower+i)
        s := ""
        fmt.Scanf("%s", &s)
        return s != "" && s[0] == 'y'
    })
    fmt.Printf("Your number is %d.\n", lower+answer)
}


package main

import (
    "bufio"
    "fmt"
    "os"
)

func main() {
    lower, upper := 1, 100
    fmt.Printf(`Instructions:
Think of integer number from %d (inclusive) to %d (inclusive) and I will guess it.
After each guess, you respond with l,h,or c depending on
if my guess was too low, too high, or correct.
Press enter when you are thinking of a number. `, lower, upper)
    in := bufio.NewReader(os.Stdin)
    in.ReadString('\n')
    for {
        guess := (upper+lower)/2
        fmt.Printf("My guess: %d (l/h/c) ", guess)
        s, err := in.ReadString('\n')
        if err != nil {
            fmt.Println("\nSo, bye.")
            return
        }
        switch s {
        case "l\n":
            lower = guess + 1
        case "h\n":
            upper = guess - 1
        case "c\n":
            fmt.Println("I did it. :)")
            return
        default:
            fmt.Println("Please respond by pressing l, h, or c")
        }
    }
}


  

You may also check:How to resolve the algorithm Arithmetic/Integer step by step in the Lambdatalk programming language
You may also check:How to resolve the algorithm Vogel's approximation method step by step in the REXX programming language
You may also check:How to resolve the algorithm Non-decimal radices/Output step by step in the Locomotive Basic programming language
You may also check:How to resolve the algorithm Loops/Downward for step by step in the Lhogho programming language
You may also check:How to resolve the algorithm Sieve of Eratosthenes step by step in the WebAssembly programming language