Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Solution: One-dimensiona spacefight - level 1

package main

import (
	"fmt"
	"math/rand"
	"os"
	"strconv"
	"time"
)

func main() {
	fmt.Println("Welcome to the game!")
	max := 20

	rand.Seed(time.Now().UnixNano())
	hidden := rand.Intn(max) + 1

	fmt.Printf("The hidden number is %v\n", hidden)
	fmt.Printf("Your guess [1-%v]: ", max)

	var input string
	fmt.Scan(&input)

	guess, err := strconv.Atoi(input)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	if guess < hidden {
		fmt.Println("Too small")
	} else if guess > hidden {
		fmt.Println("Too big")
	} else {
		fmt.Println("Direct hit!")
	}
}