英文:
Make 8x8 chess board using termui grid
问题
我需要使用GoLang中的gizak/termui包创建一个8*8的网格,并用一些符文填充它,以便稍后可以在其中放置任何一个。
我已经学习了这个包的源代码,所以我尝试这样做:
grid := ui.NewGrid()
grid.SetRect(screenWidth/2-30, screenHeight/2-8, screenWidth/2+30, screenHeight/2+8)
var rows []interface{}
for i := 0; i < 8; i++ {
var cols []interface{}
for j := 0; j < 8; j++ {
cols = append(cols, ui.NewCol(1.0/8, "t"))
}
rows = append(rows, ui.NewRow(1.0/8, "P"))
}
grid.Set(rows...)
但是我遇到了一些类型匹配错误,比如panic: InterfaceSlice() given a non-slice type。请帮助我找到一个可行的方法来创建一个8*8的网格。
英文:
I need to create grid 8*8 using gizak/termui package in GoLang and fill it with some runes so I could put there any one later.
I've learned source code of this package so I tried to do so:
grid := ui.NewGrid()
grid.SetRect(screenWidth/2-30, screenHeight/2-8, screenWidth/2+30, screenHeight/2+8)
var rows []interface{}
for i := 0; i < 8; i++ {
var cols []interface{}
for j := 0; j < 8; j++ {
cols = append(cols, ui.NewCol(1.0/8, "t"))
}
rows = append(rows, ui.NewRow(1.0/8, "P"))
}
grid.Set(rows...)
but I get some type mathcing errors like panic: InterfaceSlice() given a non-slice type
Help me please to make 8*8 grid any working way
答案1
得分: 1
你需要将一个小部件(widget)或一行(row)或一列(column)放在ui.NewRow
或ui.NewCol
的第二个参数中,否则在设置行/列到网格时会出错。此外,ui.NewCol
或ui.NewRow
的用法与你在这个示例中看到的网格用法有些不同。
以下是用于创建一个8*8网格并分配一些文本的代码:
grid := ui.NewGrid()
grid.SetRect(0, 0, screenWidth, screenHeight)
var gridDone []interface{}
p := widgets.NewParagraph()
p.Text = "P"
t := widgets.NewParagraph()
t.Text = "t"
temp := t
for i := 0; i < 8; i++ {
if i == 0 {
t = p
} else if i == 1 {
t = temp
}
gridDone = append(gridDone, ui.NewRow(1.0/8,
ui.NewCol(1.0/8, p),
ui.NewCol(1.0/8, t),
ui.NewCol(1.0/8, t),
ui.NewCol(1.0/8, t),
ui.NewCol(1.0/8, t),
ui.NewCol(1.0/8, t),
ui.NewCol(1.0/8, t),
ui.NewCol(1.0/8, t),
),
)
}
grid.Set(gridDone...)
希望对你有帮助!
英文:
You need put a widget or a row or column to second parameter of ui.NewRow
or ui.NewCol
, otherwise you get error while setting rows/columns to grid. Also usage of ui.NewCol
or ui.NewRow
a bit different than you did as i saw in this example grid usage Grid Example.
And this is the code to make 8*8 grid with some texts assigned.
grid := ui.NewGrid()
grid.SetRect(0, 0, screenWidth, screenHeight)
var gridDone []interface{}
p := widgets.NewParagraph()
p.Text = "P"
t := widgets.NewParagraph()
t.Text = "t"
temp := t
for i := 0; i < 8; i++ {
if i == 0 {
t = p
} else if i == 1 {
t = temp
}
gridDone = append(gridDone, ui.NewRow(1.0/8,
ui.NewCol(1.0/8, p),
ui.NewCol(1.0/8, t),
ui.NewCol(1.0/8, t),
ui.NewCol(1.0/8, t),
ui.NewCol(1.0/8, t),
ui.NewCol(1.0/8, t),
ui.NewCol(1.0/8, t),
ui.NewCol(1.0/8, t),
),
)
}
grid.Set(gridDone...)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论