使用termui网格创建一个8×8的棋盘。

huangapple go评论121阅读模式
英文:

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 &lt; 8; i++ {
	var cols []interface{}
	for j := 0; j &lt; 8; j++ {
		cols = append(cols, ui.NewCol(1.0/8, &quot;t&quot;))

	}
	rows = append(rows, ui.NewRow(1.0/8, &quot;P&quot;))
}
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.NewRowui.NewCol的第二个参数中,否则在设置行/列到网格时会出错。此外,ui.NewColui.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 = &quot;P&quot;
		
	t := widgets.NewParagraph()
	t.Text = &quot;t&quot;
		
	temp := t
	for i := 0; i &lt; 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...)

huangapple
  • 本文由 发表于 2023年4月18日 01:32:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76037795.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定