Created
December 24, 2013 20:02
-
-
Save rorcraft/8117308 to your computer and use it in GitHub Desktop.
Christmas tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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