refactor: lucky number exercises
* guess < 0 doesn't check for definitive positivity * n := rand.Intn(guess + 1) may pick 0, a non positive integer * remove: redundant parentheses from the loop in 06-dynamic-difficulty
This commit is contained in:
@ -45,13 +45,13 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if guess < 0 {
|
if guess <= 0 {
|
||||||
fmt.Println("Please pick a positive number.")
|
fmt.Println("Please pick a positive number.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for turn := 1; turn <= maxTurns; turn++ {
|
for turn := 1; turn <= maxTurns; turn++ {
|
||||||
n := rand.Intn(guess + 1)
|
n := rand.Intn(guess) + 1
|
||||||
|
|
||||||
// Better, why?
|
// Better, why?
|
||||||
//
|
//
|
||||||
|
@ -45,13 +45,13 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if guess < 0 {
|
if guess <= 0 {
|
||||||
fmt.Println("Please pick a positive number.")
|
fmt.Println("Please pick a positive number.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for turn := 1; turn <= maxTurns; turn++ {
|
for turn := 1; turn <= maxTurns; turn++ {
|
||||||
n := rand.Intn(guess + 1)
|
n := rand.Intn(guess) + 1
|
||||||
|
|
||||||
if n == guess {
|
if n == guess {
|
||||||
if turn == 1 {
|
if turn == 1 {
|
||||||
|
@ -45,13 +45,13 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if guess < 0 {
|
if guess <= 0 {
|
||||||
fmt.Println("Please pick a positive number.")
|
fmt.Println("Please pick a positive number.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for turn := 0; turn < maxTurns; turn++ {
|
for turn := 0; turn < maxTurns; turn++ {
|
||||||
n := rand.Intn(guess + 1)
|
n := rand.Intn(guess) + 1
|
||||||
|
|
||||||
if n == guess {
|
if n == guess {
|
||||||
switch rand.Intn(3) {
|
switch rand.Intn(3) {
|
||||||
|
@ -54,7 +54,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if guess < 0 || guess2 < 0 {
|
if guess <= 0 || guess2 <= 0 {
|
||||||
fmt.Println("Please pick positive numbers.")
|
fmt.Println("Please pick positive numbers.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -65,7 +65,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for turn := 0; turn < maxTurns; turn++ {
|
for turn := 0; turn < maxTurns; turn++ {
|
||||||
n := rand.Intn(min + 1)
|
n := rand.Intn(min) + 1
|
||||||
|
|
||||||
if n == guess || n == guess2 {
|
if n == guess || n == guess2 {
|
||||||
fmt.Println("🎉 YOU WIN!")
|
fmt.Println("🎉 YOU WIN!")
|
||||||
|
@ -53,13 +53,13 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if guess < 0 {
|
if guess <= 0 {
|
||||||
fmt.Println("Please pick a positive number.")
|
fmt.Println("Please pick a positive number.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for turn := 1; turn <= maxTurns; turn++ {
|
for turn := 1; turn <= maxTurns; turn++ {
|
||||||
n := rand.Intn(guess + 1)
|
n := rand.Intn(guess) + 1
|
||||||
|
|
||||||
if verbose {
|
if verbose {
|
||||||
fmt.Printf("%d ", n)
|
fmt.Printf("%d ", n)
|
||||||
|
@ -45,7 +45,7 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if guess < 0 {
|
if guess <= 0 {
|
||||||
fmt.Println("Please pick a positive number.")
|
fmt.Println("Please pick a positive number.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -56,7 +56,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for turn := 0; turn < maxTurns; turn++ {
|
for turn := 0; turn < maxTurns; turn++ {
|
||||||
n := rand.Intn(min + 1)
|
n := rand.Intn(min) + 1
|
||||||
|
|
||||||
if n == guess {
|
if n == guess {
|
||||||
fmt.Println("🎉 YOU WIN!")
|
fmt.Println("🎉 YOU WIN!")
|
||||||
|
@ -45,13 +45,13 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if guess < 0 {
|
if guess <= 0 {
|
||||||
fmt.Println("Please pick a positive number.")
|
fmt.Println("Please pick a positive number.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for turn := (maxTurns + guess/4); turn > 0; turn-- {
|
for turn := maxTurns + guess/4; turn > 0; turn-- {
|
||||||
n := rand.Intn(guess + 1)
|
n := rand.Intn(guess) + 1
|
||||||
|
|
||||||
if n == guess {
|
if n == guess {
|
||||||
fmt.Println("🎉 YOU WIN!")
|
fmt.Println("🎉 YOU WIN!")
|
||||||
|
Reference in New Issue
Block a user