Skip to content

Commit

Permalink
refactor: lucky number exercises
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
paulwaldmann committed Jan 10, 2021
1 parent 8a5d1c6 commit a3fc539
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ func main() {
return
}

if guess < 0 {
if guess <= 0 {
fmt.Println("Please pick a positive number.")
return
}

for turn := 1; turn <= maxTurns; turn++ {
n := rand.Intn(guess + 1)
n := rand.Intn(guess) + 1

// Better, why?
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ func main() {
return
}

if guess < 0 {
if guess <= 0 {
fmt.Println("Please pick a positive number.")
return
}

for turn := 1; turn <= maxTurns; turn++ {
n := rand.Intn(guess + 1)
n := rand.Intn(guess) + 1

if n == guess {
if turn == 1 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ func main() {
return
}

if guess < 0 {
if guess <= 0 {
fmt.Println("Please pick a positive number.")
return
}

for turn := 0; turn < maxTurns; turn++ {
n := rand.Intn(guess + 1)
n := rand.Intn(guess) + 1

if n == guess {
switch rand.Intn(3) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func main() {
}
}

if guess < 0 || guess2 < 0 {
if guess <= 0 || guess2 <= 0 {
fmt.Println("Please pick positive numbers.")
return
}
Expand All @@ -65,7 +65,7 @@ func main() {
}

for turn := 0; turn < maxTurns; turn++ {
n := rand.Intn(min + 1)
n := rand.Intn(min) + 1

if n == guess || n == guess2 {
fmt.Println("🎉 YOU WIN!")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ func main() {
return
}

if guess < 0 {
if guess <= 0 {
fmt.Println("Please pick a positive number.")
return
}

for turn := 1; turn <= maxTurns; turn++ {
n := rand.Intn(guess + 1)
n := rand.Intn(guess) + 1

if verbose {
fmt.Printf("%d ", n)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func main() {
return
}

if guess < 0 {
if guess <= 0 {
fmt.Println("Please pick a positive number.")
return
}
Expand All @@ -56,7 +56,7 @@ func main() {
}

for turn := 0; turn < maxTurns; turn++ {
n := rand.Intn(min + 1)
n := rand.Intn(min) + 1

if n == guess {
fmt.Println("🎉 YOU WIN!")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ func main() {
return
}

if guess < 0 {
if guess <= 0 {
fmt.Println("Please pick a positive number.")
return
}

for turn := (maxTurns + guess/4); turn > 0; turn-- {
n := rand.Intn(guess + 1)
for turn := maxTurns + guess/4; turn > 0; turn-- {
n := rand.Intn(guess) + 1

if n == guess {
fmt.Println("🎉 YOU WIN!")
Expand Down

0 comments on commit a3fc539

Please sign in to comment.