Created
August 2, 2012 02:43
-
-
Save anonymous/3232693 to your computer and use it in GitHub Desktop.
guesser.go
This file contains hidden or 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() { | |
r := rand.New(rand.NewSource(time.Now().Unix())) | |
guess := (r.Intn(99)) | |
var tries int | |
var input string | |
for input != "y" { | |
tries++ | |
fmt.Println("Think of a number between 1 and 100. I will try to guess it. Is it ", guess+1, "?") | |
fmt.Print("[y] yes, [h] higher [l] lower > ") | |
fmt.Scanf("%v", &input) | |
if input == "l" { | |
guess-- | |
} else if input == "h" { | |
guess++ | |
} else if input == "y" { | |
fmt.Println("Yay! I got it in", tries, "tries!") | |
} else { | |
fmt.Println("Not sure what that means..") | |
} | |
if guess < 0 || guess > 99 { | |
fmt.Println("Sorry, that's out of my range!") | |
break | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment