Print multi-line strings on same line

怎甘沉沦 提交于 2019-12-13 01:43:56

问题


what the title says. I have a dice program, and it prints a dice IE

 ------
|      |
|      |
|     1|
 ------

I want to be able to print multiple dice, so it looks like this:

 ------     ------
|      |   |      |
|      |   |      |
|     1|   |     3|
 ------     ------

I have tried fmt.Print, but that still prints them below each other. I was thinking about creating a function as well, that prints the top line of each object, but I couldn't figure out how to do this. Any ideas?


回答1:


This takes care of the printing. The numbers are randomized between 1 and 6 (inclusive). The number of dice taken as a command line argument. So in my case ./roll 6 prints 6 dice with random numbers up to 6.

package main

import (
    "fmt"
    "math/rand"
    "os"
    "strconv"
    "time"
)

func main() {
    numDice := 1

    if len(os.Args) > 1 {
        i, err := strconv.Atoi(os.Args[1])
        if err != nil {
            fmt.Println(err)
        }
        numDice = i
    }

    seed := rand.NewSource(time.Now().UnixNano())
    randomNumber := rand.New(seed)

    die := []string{
        " ------   ",
        "|      |  ",
        "|      |  ",
        "|      |  ",
        " ------   ",
    }

    for i := 0; i < 5; i++ {
        for j, n := 0, numDice; j < n; j++ {
            if i == 3 {
                fmt.Printf("|    %d |  ", randomNumber.Intn(5)+1)
            } else {
                fmt.Print(die[i])
            }
        }
        fmt.Println()
    }
}



回答2:


package main

import (
    "bytes"
    "fmt"
    "strings"
)

func getDie(n int) []string {
    return []string{
        " ------",
        "|      |",
        "|      |",
        fmt.Sprintf("|%6d|", n),
        " ------",
    }
}

func joinLines(between int, items ...[]string) []string {
    if len(items) == 0 {
        return nil
    }
    if len(items) == 1 {
        return items[0]
    }

    lineCount := 0

    maxSizes := make([]int, len(items))
    for i, item := range items {
        for j, line := range item {
            if maxSizes[i] < len(line) {
                maxSizes[i] = len(line)
            }
            if j+1 > lineCount {
                lineCount = j + 1
            }
        }
    }

    lines := make([]string, lineCount)
    for i := 0; i < lineCount; i++ {
        var buff bytes.Buffer

        for j, item := range items {
            diff := 0
            if j+1 < len(items) {
                diff += maxSizes[j] + between
            }
            if i < len(item) {
                line := item[i]
                buff.WriteString(line)
                diff -= len(line)
            }
            if diff > 0 {
                buff.WriteString(strings.Repeat(" ", diff))
            }
        }

        lines[i] = buff.String()
    }

    return lines
}

func main() {
    a, b, c, d := getDie(2), getDie(3), []string{"", "", "="}, getDie(5)
    all := joinLines(3, a, b, c, d)
    for _, line := range all {
        fmt.Println(line)
    }
}

https://play.golang.org/p/NNrTUDdfyn



来源:https://stackoverflow.com/questions/36220839/print-multi-line-strings-on-same-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!