Skip to content

Instantly share code, notes, and snippets.

@rorcraft
Created December 24, 2013 20:02
Show Gist options
  • Save rorcraft/8117308 to your computer and use it in GitHub Desktop.
Save rorcraft/8117308 to your computer and use it in GitHub Desktop.
Christmas tree
package main
import "fmt"
import "math/rand"
import "time"
func tree(width int, level int) {
rand.Seed(time.Now().UnixNano())
full_width := width + (2 * level)
tree_level(1, width, full_width)
for i := 0; i < level - 1; i++ {
tree_level(width - 2, width + 2, full_width)
}
trunk(full_width)
}
func tree_level(level int, width int, full_width int) {
for level <= width {
print_times("*", level, full_width)
level += 2
}
}
func trunk(width int) {
print_times("#", 1, width)
print_times("#", 1, width)
}
func print_times(c string, times int, width int) {
for i := 0; i < (width - times)/2; i++ {
fmt.Print(" ")
}
for i := 0; i < times; i++ {
if i > 0 && i < times -1 && rand.Intn(10) > 8 {
fmt.Print("o")
} else {
fmt.Print(c)
}
}
fmt.Print("\n")
}
func main() {
width := 13
level := 3
tree(width, level)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment