Skip to content

Instantly share code, notes, and snippets.

@Miqueas
Created February 17, 2021 21:54
Show Gist options
  • Save Miqueas/c1e8a7ea3bba0e0aa1d021d6eda91d4e to your computer and use it in GitHub Desktop.
Save Miqueas/c1e8a7ea3bba0e0aa1d021d6eda91d4e to your computer and use it in GitHub Desktop.
[Go] Classic guessing game
package main
import (
Fmt "fmt"
Time "time"
Rand "math/rand"
)
func main() {
// Makes a constantly-changing number
Rand.Seed(Time.Now().UnixNano())
// The "magic" number
var MagicNum = uint8(Rand.Intn(100))
// Stores the user input
var choice uint8
// User lives
var lives uint8 = 3
Fmt.Println("Guess the number!")
Fmt.Println("Enter your choice:")
Fmt.Print("> ")
Fmt.Scanf("%d", &choice)
for {
if choice != MagicNum && lives != 0 {
Fmt.Println("Bad! Try again:")
Fmt.Print("> ")
Fmt.Scanf("%d", &choice)
lives -= 1
continue
} else if choice != MagicNum && lives == 0 {
Fmt.Println("You loss!")
Fmt.Println("The number is", MagicNum)
break
} else {
Fmt.Println("You win!")
break
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment