Improve star-shapes-mk2

This commit is contained in:
raul 2024-04-18 10:34:39 +02:00
parent b8fe0a216b
commit 829a84fabb
1 changed files with 46 additions and 20 deletions

View File

@ -8,40 +8,66 @@ import (
)
type Shaper interface {
Shape(int) Square
GenerateCube(num int)
GenerateLine(num int)
GenerateFlag(num int)
}
type Square struct {
form string
quantity int
type Shape struct {
shape string
numOfChars int
}
func (s Square) Shape(num int) Square {
func (s Shape) GenerateCube(num int) {
for i := 0; i < num; i++ {
for i := 0; i < num; i++ {
s.form += "* "
fmt.Printf("* ")
}
s.form += "\n"
fmt.Printf("\n")
}
s.quantity = num
return s
}
func showShape(s Shaper, num int) {
fmt.Println(s.Shape(num).form)
func (s Shape) GenerateLine(num int) {
for i := 0; i < num; i++ {
fmt.Printf("* ")
}
fmt.Printf("\n")
}
func getNumberStars() int {
numStr := os.Args[1]
num, err := strconv.Atoi(numStr)
if err != nil {
log.Panicln(err)
func (s Shape) GenerateFlag(num int) {
for a := 0; a < num; a++ {
for i := -1; i < a; i++ {
fmt.Printf("* ")
}
fmt.Printf("\n")
}
for i := 0; i < num/2; i++ {
fmt.Println("*")
}
}
func passToShaper(s Shaper, num int, choice int) {
switch choice {
case 1:
s.GenerateLine(num)
case 2:
s.GenerateCube(num)
case 3:
s.GenerateFlag(num)
}
return num
}
func main() {
s := Square{}
showShape(s, getNumberStars())
if len(os.Args) != 3 {
fmt.Println("Use an argument like 5 and then 1")
os.Exit(0)
}
numToGen, err := strconv.Atoi(os.Args[1])
if err != nil {
log.Fatalf("Error: %v\n", err)
}
numToChoose, err := strconv.Atoi(os.Args[2])
s := Shape{}
passToShaper(s, numToGen, numToChoose)
}