/
guessinggame-c.go
79 lines (62 loc) · 3.3 KB
/
guessinggame-c.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
78
79
/* Alta3 Research | RZFeeser
Go Guessing Game */
package main
import (
"fmt" // import the fmt package which allows you to use text
//formatting, reading input & printing output functions
"math/rand" //import the rand package which allows you to
//generate random numbers
"time" // import the package which will provide time functionality to measure time
)
func main() {
// Print some basic information on the screen for the user
var mycount int // Var containing # of user requested tries
var myrand int // Var to hold rand num max
myrand = 11 // Default rand max
fmt.Println("Enter the number of tries you would like: ")
fmt.Scanln(&mycount) // Prompt use for # of tries
if mycount > 5 { // If more than 5 requested increase rand num
myrand = 100
}
fmt.Println("Game: Guess a number between 0 and 10")
fmt.Println("You have", mycount, "tries ")
// generating random numbers
source := rand.NewSource(time.Now().UnixNano())
// The default number generator is predictable, so it will produce the same sequence of numbers each time. To
// produce varying range of numbers, give it a seed that changes (in this case: time would ensure it changes ).
// Note that this is not safe to use for random numbers you want to be secret; use crpyto/rand for those.
// now we can generate some chaos with our "source" seed
randomizer := rand.New(source)
// secretNumber := randomizer.Intn(11) // generate numbers between 0 and 10 only
secretNumber := randomizer.Intn(myrand) // generate numbers between 0 and 10 only
// we declare a variable of type int called "guess"
var guess int
// create a for loop that begins at 1 and loops to 3
for try := 1; try <= mycount; try++ {
// declaring the conditions for the for loop ; the shorthand form of declaring a variable was used here.
// Declare and Initialize ‘ := ‘ you declare and assign a value upon declaration. Go will automatically
// infer the type of the variable since you already assigned a value to it.
// print out the number of times the player has made a guess
fmt.Println("Round:", try)
// the program will prompt the player to make a guess and enter a number
fmt.Println("Please enter your number")
fmt.Scan(&guess) // this function makes it possible for the program to receive the input
if guess < secretNumber {
// if the guessed number is less than or greater than the correct number; give the player a hint
fmt.Printf("Sorry, wrong guess ; number is too small\n ")
} else if guess > secretNumber {
fmt.Printf("Sorry, wrong guess ; number is too large\n ")
} else {
fmt.Printf("You win!\n")
break
// Print out "you win" message when the player guesses the correct number
}
if try == mycount {
// if the number of tries is equal to 3, print game over and also the correct number
fmt.Printf("Game over!!\n ")
fmt.Printf("The correct number is %d\n", secretNumber)
}
}
// every time the game ends, print this message
fmt.Println("Thanks for playing, see you next time!")
}