-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
127 lines (115 loc) · 3.31 KB
/
main.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"fmt"
"bufio"
"os"
"strings"
"github.com/Greeshmanth1909/pokedexCli/api"
)
type cliCommand struct {
name string
description string
callBack func(str ...string) error
}
func helpCallback(str ...string) error {
fmt.Println(`
Welcome to the Pokedex!
Usage:
help: Displays a help message
exit: Exit the Pokedex`)
return nil
}
func exitCallback(str ...string) error {
fmt.Println("Exiting...")
return nil
}
func main() {
// get user input from console
command := map[string] cliCommand{
"help": {
name: "help",
description: "Displays a help message",
callBack: helpCallback,
},
"exit": {
name: "exit",
description: "quits the REPL",
callBack: exitCallback,
},
"map": {
name: "map",
description: "displays the names of 20 location areas in the Pokemon world",
callBack: api.CommandMap,
},
"mapb": {
name: "mapb",
description: "displays the names of previous 20 locations, if there are any",
callBack: api.CommandMapb,
},
"explore": {
name: "explore",
description: "displays the names of pokemon present in the given area",
callBack: api.Explore,
},
"catch": {
name: "catch",
description: "catches a given pokemon base on a random chance seeded by the pokemon's base experience",
callBack: api.Catch,
},
"inspect": {
name: "inspect",
description: "shows statistics about a pokemon in a pokedex",
callBack: api.Inspect,
},
"pokedex": {
name: "pokedex",
description: "prints all pokemon the user caught to the standard output",
callBack: api.Pokedex,
},
}
for true {
fmt.Print("Pokedex >")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
input := scanner.Text()
if input == "help" {
command["help"].callBack()
}
if input == "exit" {
command["exit"].callBack()
return
}
if input == "map" {
command["map"].callBack()
}
if input == "mapb" {
command["mapb"].callBack()
}
// extract multiple inputs, if any
inputs := strings.Fields(input)
if inputs[0] == "explore" {
if len(inputs) != 2 {
fmt.Println("Please enter a city to explore, explore <city-name>")
continue
}
command["explore"].callBack(inputs[1])
}
if inputs[0] == "catch" {
if len(inputs) != 2 {
fmt.Println("Please enter a pokemon to catch, catch <catch-name>")
continue
}
command["catch"].callBack(inputs[1])
}
if inputs[0] == "inspect" {
if len(inputs) != 2 {
fmt.Println("Please enter a pokemon to inspect, inspect <pokemon-name>")
continue
}
command["inspect"].callBack(inputs[1])
}
if input == "pokedex" {
command["pokedex"].callBack()
}
}
}