-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
138 lines (130 loc) · 3.12 KB
/
Copy pathmain.go
File metadata and controls
138 lines (130 loc) · 3.12 KB
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
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
func main() {
player := NewPlayer("Alice")
scanner := bufio.NewScanner(os.Stdin)
fmt.Printf("当前玩家:%s,请输入help查看帮助:", player.Name)
for {
fmt.Print(">")
if !scanner.Scan() {
break
}
line := strings.TrimSpace(scanner.Text())
if line == "" {
continue
}
if line == "quit" {
fmt.Println("再见,", player.Name)
return
}
if line == "help" {
fmt.Println("可用命令: ")
fmt.Println("help 查看帮助")
fmt.Println("quit 退出游戏")
fmt.Println("add <id> <数量> <名称> 添加物品")
fmt.Println("get <id> 查询物品")
fmt.Println("list 查看物品列表")
fmt.Println("remove <id> <数量> 移除物品")
fmt.Println("count 看物品总数")
fmt.Println("save <路径> 保存背包")
continue
}
fmt.Println("收到命令:", line)
fields := strings.Fields(line)
switch fields[0] {
case "add":
if len(fields) < 4 {
fmt.Println("命令格式错误")
continue
}
id := fields[1]
quantity, err := strconv.Atoi(fields[2])
if err != nil {
fmt.Println("数量必须是整数")
continue
}
name := strings.Join(fields[3:], " ")
if err := player.Inventory.Add(id, name, quantity); err != nil {
fmt.Println(err)
continue
}
fmt.Printf("成功添加 %d 个 %s 到库存\n", quantity, name)
case "get":
if len(fields) != 2 {
fmt.Println("命令格式错误")
continue
}
id := fields[1]
item, exists := player.Inventory.Get(id)
if !exists {
fmt.Println("物品", id, "不存在")
continue
}
fmt.Printf("物品 %s 有 %d 个\n", item.Name, item.Quantity)
case "list":
if len(fields) != 1 {
fmt.Println("用法:list")
continue
}
items := player.Inventory.List()
if len(items) == 0 {
fmt.Println("背包为空")
continue
} else {
printInventory(items)
}
case "remove":
if len(fields) != 3 {
fmt.Println("remove 命令格式错误")
continue
}
id := fields[1]
quantity, err := strconv.Atoi(fields[2])
if err != nil {
fmt.Println("数量必须是整数")
continue
}
if err := player.Inventory.Remove(id, quantity); err != nil {
fmt.Println(err)
continue
}
fmt.Printf("成功移除 %d 个 %s\n", quantity, id)
case "count":
if len(fields) != 1 {
fmt.Println("count命令格式错误")
continue
}
fmt.Printf("当前物品总数为 %d\n", player.Inventory.TotalQuantity())
case "save":
if len(fields) != 2 {
fmt.Println("save 命令格式错误")
continue
}
if err := player.Inventory.SaveText(fields[1]); err != nil {
fmt.Println(err)
continue
}
fmt.Println("背包保存成功")
default:
fmt.Println("未知命令")
}
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "reading standard input:", err)
}
items := player.Inventory.List()
fmt.Println(player.Name)
printInventory(items)
}
func printInventory(items []Item) {
for i := 0; i < len(items); i++ {
item := items[i]
fmt.Printf("Id: %s, Name: %s, Quantity: %d\n", item.ID, item.Name, item.Quantity)
}
}