Skip to content

Instantly share code, notes, and snippets.

@duskvirkus
Created April 22, 2020 15:53
Show Gist options
  • Select an option

  • Save duskvirkus/d71a9a560b95eff115609e9eea7ba1a7 to your computer and use it in GitHub Desktop.

Select an option

Save duskvirkus/d71a9a560b95eff115609e9eea7ba1a7 to your computer and use it in GitHub Desktop.
Guessing With Go. A simple guessing game made in golang.
// Guessing With Go
// Violet Graham
// Based on guessing game created by Jack Mott (https://github.com/jackmott).
// In Games with go episode 2 (https://youtu.be/R9LPV44RLv4)
package main
import (
"bufio"
"fmt"
"math/rand"
"os"
"time"
)
func randomI(low int, high int) int {
return rand.Intn(high - low) + low
}
func main() {
scanner := bufio.NewScanner(os.Stdin)
rand.Seed(time.Now().UnixNano())
fmt.Println("Think of a number between 1 and 100.")
fmt.Print("Press ENTER when ready.")
scanner.Scan()
low := 0
high := 100
guess := randomI(low, high)
tries := 0
for {
fmt.Println("I guess", guess)
fmt.Println("Is that:")
fmt.Println("(a) too low?")
fmt.Println("(b) too high?")
fmt.Println("(c) correct?")
scanner.Scan()
reply := scanner.Text()
if reply == "a" {
low = guess
} else if reply == "b" {
high = guess
} else if reply == "c" {
fmt.Println("It took me", tries, "tries to guess correctly.")
fmt.Println("Thanks for playing!")
break
} else {
fmt.Println("You entered,", reply, "Please try again")
}
tries++
if high <= low {
fmt.Println("You're confusing me, I give up.")
break
}
guess = randomI(low, high)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment