Created
December 14, 2021 08:31
-
-
Save ivanauliaa/186f31faf2e9b59cc31878c48488f58e to your computer and use it in GitHub Desktop.
Simple Go guess number game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math/rand" | |
"time" | |
) | |
func main() { | |
exit := false | |
for !exit { | |
lives := 3 | |
guess := GetRandomNumber(10) | |
isRight := false | |
var answer int | |
for lives > 0 && !isRight { | |
fmt.Printf("Lives: %d\nGuess number: ", lives) | |
fmt.Scanln(&answer) | |
switch { | |
case answer < guess: | |
fmt.Println("Lesser") | |
case answer > guess: | |
fmt.Println("Greater") | |
default: | |
isRight = true | |
} | |
lives-- | |
fmt.Println() | |
} | |
if isRight { | |
fmt.Println("Nice") | |
} else { | |
fmt.Println("You noob") | |
} | |
fmt.Printf("Your answer: %d | Expected answer: %d\n", answer, guess) | |
fmt.Print("Do you want to exit? [0/1] ") | |
var exitChoice int | |
fmt.Scanln(&exitChoice) | |
if exitChoice == 1 { | |
exit = true | |
} | |
fmt.Println() | |
} | |
fmt.Println("Thank you for playing") | |
} | |
func GetRandomNumber(max int) int { | |
s1 := rand.NewSource(time.Now().Unix()) | |
r1 := rand.New(s1) | |
return r1.Intn(max) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment