SoFunction
Updated on 2025-03-03

Implement guessing game based on Go language

1. The program needs to achieve the effect:

1. The program first generates a random integer of 0-100.

2. Prompt the player to make a guess.

3. Each time the player needs to enter a number, the program will tell the player the relationship between the number entered and the generated number, and let the player guess again.

4. If you guess correctly, the player will be told to win and exit the program.

2. Ideas analysis:

  1. Since random integers of 0-100 are to be generated (and the numbers generated by each run program are different), the current time (UnixNano) is used as the seed to initialize the random number generator (rand).
  2. Use the random number generator to generate a random integer between 0 and maxNum as the secret number to guess.
  3. Prompt the user into their guess.

  4. Creates a buffered reader() that is read from standard input().

  5. Enter an infinite loop until the user guesses the secret number correctly.

  6. In each loop, first try to read a line of input from the buffer reader, if an error occurs during the reading process, print the error message and skip the loop.

  7. Removes carriage returns and line breaks at both ends of the input string.

  8. Try to convert the input string to an integer, if the conversion fails (i.e. the input is not an integer), print the error message and skip this loop.

  9. If the conversion is successful, print the user's guess.

  10. According to the user's guess and the size relationship between the secret number, a corresponding prompt is given.

  11. If the user guesses the secret number correctly, print the congratulations and jump out of the loop.

3. Specific code

package main
import (
	"bufio"
	"fmt"
	"math/rand"
	"os"
	"strconv"
	"strings"
	"time"
)
func main() {
	maxNum := 100
	(().UnixNano())
	secretNumber := (maxNum)
	// ("The secret number is ", secretNumber)
	("Please input your guess")
	reader := ()
	for {
		input, err := ('\n')
		if err != nil {
			("An error occured while reading input. Please try again", err)
			continue
		}
		input = (input, "\r\n")
		guess, err := (input)
		if err != nil {
			("Invalid input. Please enter an integer value")
			continue
		}
		("You guess is", guess)
		if guess > secretNumber {
			("Your guess is bigger than the secret number. Please try again")
		} else if guess < secretNumber {
			("Your guess is smaller than the secret number. Please try again")
		} else {
			("Correct, you Legend!")
			break
		}
	}
}

4. Possible questions

1. What does it mean to remove carriage returns and line breaks at both ends of a string?

Answer: When reading user input, it is usually necessary to remove carriage returns and line breaks at both ends of the string. This is because these characters are usually generated by the user pressing the Enter key in the terminal or command line, rather than what the user actually enters. Therefore, if we do not remove them, it will cause the program to fail to parse the user's input correctly and may lead to unexpected behavior or errors.

2. Try to convert the input string to an integer. If the conversion fails (i.e., the input is not an integer), print the error message and skip this loop. What does this idea mean?

Answer: In Go language, the input processing is done according to the string. For example, if I enter 97, it is actually the numeric character "97".

This is the end of this article about implementing guessing games based on Go language. For more related Go guessing games, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!