/
main.go
77 lines (61 loc) 路 1.32 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright 漏 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
// For more tutorials : https://learngoprogramming.com
// In-person training : https://www.linkedin.com/in/inancgumus/
// Follow me on twitter: https://twitter.com/inancgumus
package main
import (
"fmt"
"math/rand"
"os"
"strconv"
"time"
)
const (
maxTurns = 5 // less is more difficult
usage = `Welcome to the Lucky Number Game! 馃崁
The program will pick %d random numbers.
Your mission is to guess one of those numbers.
The greater your number is, harder it gets.
Wanna play?
`
)
func main() {
rand.Seed(time.Now().UnixNano())
args := os.Args[1:]
if len(args) < 1 {
fmt.Printf(usage, maxTurns)
return
}
guess, err := strconv.Atoi(args[0])
if err != nil {
fmt.Println("Not a number.")
return
}
var guess2 int
if len(args) == 2 {
guess2, err = strconv.Atoi(args[1])
if err != nil {
fmt.Println("Not a number.")
return
}
}
if guess <= 0 || guess2 <= 0 {
fmt.Println("Please pick positive numbers.")
return
}
min := guess
if guess < guess2 {
min = guess2
}
for turn := 0; turn < maxTurns; turn++ {
n := rand.Intn(min) + 1
if n == guess || n == guess2 {
fmt.Println("馃帀 YOU WIN!")
return
}
}
fmt.Println("鈽狅笍 YOU LOST... Try again?")
}