From dbad010c93be12871007df4f434756030f572175 Mon Sep 17 00:00:00 2001 From: Andrew Haynes Date: Mon, 16 Nov 2026 10:58:36 -0500 Subject: [PATCH] Done --- .gitignore | 9 ++++++ README.md | 1 + main.go | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 main.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..38f9e2e --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +.DS_Store + +.vscode/ +.idea/ + +.env +.env.* + +bin/ \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..3332ffb --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# Hello from your fellow README.md \ No newline at end of file diff --git a/main.go b/main.go new file mode 100644 index 0000000..67da54a --- /dev/null +++ b/main.go @@ -0,0 +1,94 @@ +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) +}