95 lines
2.0 KiB
Go
95 lines
2.0 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"math/rand"
|
|
"time"
|
|
)
|
|
|
|
/*
|
|
Welcome to the Number Guessing Game!
|
|
I'm thinking of a number between 1 and 100.
|
|
You have 5 chances to guess the correct number.
|
|
|
|
Please select the difficulty level:
|
|
1. Easy (10 chances)
|
|
2. Medium (5 chances)
|
|
3. Hard (3 chances)
|
|
|
|
Enter your choice: 2
|
|
|
|
Great! You have selected the Medium difficulty level.
|
|
Let's start the game!
|
|
|
|
Enter your guess: 50
|
|
Incorrect! The number is less than 50.
|
|
|
|
Enter your guess: 25
|
|
Incorrect! The number is greater than 25.
|
|
|
|
Enter your guess: 35
|
|
Incorrect! The number is less than 35.
|
|
|
|
Enter your guess: 30
|
|
Congratulations! You guessed the correct number in 4 attempts.
|
|
*/
|
|
|
|
const (
|
|
DifficultyEasy = 1
|
|
DifficultyMedium = 2
|
|
DifficultyHard = 3
|
|
)
|
|
|
|
func main() {
|
|
var difficulty int
|
|
answer := rand.New(rand.NewSource(time.Now().UnixNano())).Intn(100) + 1
|
|
var max_attempts int
|
|
var user_attempts int = -1
|
|
var user_answer int
|
|
|
|
fmt.Println("Please select the difficutly level:")
|
|
fmt.Println("1. Easy (10 chances)\n2. Medium (5 chances)\n3. Hard (3 chances)\n\nEnter your choice: ")
|
|
|
|
fmt.Scan(&difficulty)
|
|
|
|
switch difficulty {
|
|
case DifficultyEasy:
|
|
max_attempts = 10
|
|
|
|
case DifficultyMedium:
|
|
max_attempts = 5
|
|
|
|
case DifficultyHard:
|
|
max_attempts = 3
|
|
|
|
}
|
|
|
|
fmt.Println("\nWelcome to the Number Guessing Game!")
|
|
fmt.Println("I'm thinking of a number between 1 and 100.")
|
|
fmt.Printf("You have %d chances to guess the correct number.\n", max_attempts)
|
|
fmt.Println("\nLet's start the game!")
|
|
|
|
for user_attempts < max_attempts {
|
|
fmt.Println("Enter your guess:")
|
|
fmt.Scan(&user_answer)
|
|
if user_answer == answer {
|
|
goto win
|
|
} else {
|
|
if answer > user_answer {
|
|
fmt.Printf("Incorrect! The number is greater than %d\n", user_answer)
|
|
} else {
|
|
fmt.Printf("Incorrect! The number is less than %d\n", user_answer)
|
|
}
|
|
}
|
|
user_attempts++
|
|
}
|
|
if user_attempts >= max_attempts && user_answer != answer {
|
|
goto lose
|
|
}
|
|
win:
|
|
fmt.Printf("Congratulations! You guessed the correct number in %d attempts.\n", user_attempts)
|
|
return
|
|
lose:
|
|
fmt.Printf("You failed! The number was %d", answer)
|
|
}
|