|
@@ -1,16 +1,55 @@
|
|
|
+// guess - игра, в которой игрок должен угадать случайное число.
|
|
|
package main
|
|
|
|
|
|
import (
|
|
|
+ "bufio"
|
|
|
"fmt"
|
|
|
+ "log"
|
|
|
"math/rand"
|
|
|
- "time"
|
|
|
+ "os"
|
|
|
+ "strconv"
|
|
|
+ "strings"
|
|
|
)
|
|
|
|
|
|
+func intInput(message string) int {
|
|
|
+ reader := bufio.NewReader(os.Stdin)
|
|
|
+ fmt.Println(message)
|
|
|
+ input, err := reader.ReadString('\n')
|
|
|
+ if err != nil {
|
|
|
+ log.Fatal(err)
|
|
|
+ }
|
|
|
+ input = strings.TrimSpace(input)
|
|
|
+ ret, err := strconv.Atoi(input)
|
|
|
+ if err != nil {
|
|
|
+ log.Fatal(err)
|
|
|
+ }
|
|
|
+ return ret
|
|
|
+}
|
|
|
+
|
|
|
func main() {
|
|
|
- seconds := time.Now().Unix()
|
|
|
- rand.Seed(seconds)
|
|
|
target := rand.Intn(100) + 1
|
|
|
- fmt.Println("I've chosen a random number berween 1 and 100.")\
|
|
|
+ fmt.Println("I've chosen a random number berween 1 and 100.")
|
|
|
fmt.Println("Can you guess it?")
|
|
|
-
|
|
|
+
|
|
|
+ success := false
|
|
|
+ for guesses := 0; guesses < 10; guesses++ {
|
|
|
+ fmt.Println("You have", 10-guesses, "guesses left.")
|
|
|
+
|
|
|
+ guess := intInput("Make a guess: ")
|
|
|
+
|
|
|
+ if guess < target {
|
|
|
+ fmt.Println("Oops. Your guess was LOW.")
|
|
|
+ } else if guess > target {
|
|
|
+ fmt.Println("Oops. Your guess was HIGH")
|
|
|
+ } else {
|
|
|
+ success = true
|
|
|
+ fmt.Println("Good job! You guessed it!")
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if !success {
|
|
|
+ fmt.Println("Sorry, you didn't guess my number. It was:", target)
|
|
|
+ }
|
|
|
+
|
|
|
}
|