/
main.go
105 lines (88 loc) · 2.16 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
package main
import (
"fmt"
"math/rand"
"strings"
)
const (
APPLE = "🍎"
STAR = "🌟"
BELL = "🔔"
TREE = "🎄"
DOOR = "🚪"
GIFT = "🎁"
)
func main() {
//先来个5层高的
ct := newChristmasTree(4)
fmt.Print(ct)
}
type ChristmasTree struct {
floor int
treeBuilder strings.Builder
}
func (ct *ChristmasTree) String() string {
return ct.treeBuilder.String()
}
func newChristmasTree(floor int) *ChristmasTree {
ct := &ChristmasTree{floor: floor}
ct.gen()
return ct
}
// 获取第floor层的第line行的圣诞树数量
func getLineAmount(floor, line int) int {
return 1 + line*2 + floor*4 + int(floor/2*2)*int((floor+1)/2)
}
// 随机按比例分配星星、铃铛、苹果和圣诞树
func randAppleTree() string {
r := rand.Intn(100)
if r < 1 {
return STAR
} else if r < 2 {
return BELL
} else if r < 10 {
return APPLE
} else {
return TREE
}
}
// Blog:www.flysnow.org
// Wechat:flysnow_org
// 生成一整颗圣诞树
func (ct *ChristmasTree) gen() {
bottomAmount := getLineAmount(ct.floor, ct.floor+4)
//一层,一行的生成
for floor := 0; floor < ct.floor; floor++ {
for line := 0; line < floor+5; line++ {
lineAmount := getLineAmount(floor, line)
for i := (bottomAmount-lineAmount)/2 - 1; i > 0; i-- {
ct.treeBuilder.WriteString(" ")
}
for i := 0; i < lineAmount; i++ {
ct.treeBuilder.WriteString(randAppleTree())
}
ct.treeBuilder.WriteString("\n")
}
}
//居中、生成圣诞树根
for i := 0; i < ct.floor; i++ {
lineAmount := ct.floor + (ct.floor+1)%2 //一个更接近层数的近似值
for i := (bottomAmount-lineAmount)/2 - 1; i > 0; i-- {
ct.treeBuilder.WriteString(" ")
}
for i := 0; i < lineAmount; i++ {
ct.treeBuilder.WriteString(DOOR)
}
ct.treeBuilder.WriteString("\n")
}
//在圣诞树下放点礼物
if ct.floor > 3 {
//主要是从倒数第2行开始放
lines := strings.Split(ct.treeBuilder.String(), "\n")
lines[len(lines)-4] += " " + GIFT
lines[len(lines)-3] += " " + GIFT + GIFT + GIFT
lines[len(lines)-2] += " " + GIFT + GIFT + GIFT + GIFT + GIFT
ct.treeBuilder.Reset()
ct.treeBuilder.WriteString(strings.Join(lines, "\n"))
}
}