-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_catch.go
More file actions
47 lines (36 loc) · 916 Bytes
/
command_catch.go
File metadata and controls
47 lines (36 loc) · 916 Bytes
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
package main
import (
"errors"
"fmt"
"math/rand"
"strings"
"time"
)
func commandCatch(cfg *config, args []string) error {
if len(args) < 1 {
return errors.New("usage: catch <pokemon>")
}
name := strings.ToLower(args[0])
if _, caught := cfg.pokedex[name]; caught {
fmt.Printf("%s is already caught!\n", name)
return nil
}
fmt.Printf("Throwing a Pokeball at %s...\n", name)
pokemon, err := cfg.pokeapiClient.GetPokemon(name)
if err != nil {
return err
}
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
catchProbability := 1.0 - float64(pokemon.BaseExperience)/400.0
if catchProbability < 0.1 {
catchProbability = 0.1
}
if rng.Float64() > catchProbability {
fmt.Printf("%s escaped!\n", pokemon.Name)
return nil
}
cfg.pokedex[name] = pokemon
fmt.Printf("%s was caught!\n", pokemon.Name)
fmt.Println("You may now inspect it with the inspect command.")
return nil
}