-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_catch.go
34 lines (28 loc) · 979 Bytes
/
command_catch.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
package main
import (
"errors"
"fmt"
"math/rand"
)
func commandCatch(cfg *config, args ...string) error {
if len(args) != 1 {
return errors.New("no pokemon name provided")
}
pokemonName := args[0]
pokemon, err := cfg.pokeApiClient.GetPokemon(pokemonName)
if err != nil {
return err
}
// we use base experience (int) to determine how hard a pokemon is to catch
// a more experienced pokemon is harder to catch
const threshold = 50 // easy mode, typical base level is +/- 30
randNum := rand.Intn(pokemon.BaseExperience) // generates a random number between 0 -> n
fmt.Println("base experience:", pokemon.BaseExperience, "randNum:", randNum, "threshold:", threshold)
if randNum > threshold {
// pokemon is not caught
return fmt.Errorf("failed to catch %s", pokemonName)
}
cfg.caughtPokemon[pokemonName] = pokemon // inserts the pokemon into the pokemon map
fmt.Printf("Succeeded in catching %s!\n", pokemonName)
return nil
}