英文:
Create a global bidimensional slice with length taken in input
问题
我需要创建一个二维切片,其长度由输入确定。该切片是扫雷游戏的场地,因此长度将决定场地的维度。
我希望这个切片是全局的,目前我直接将切片初始化为场地的固定维度,但我正在实现难度选择,所以需要使场地的维度可变。
//目前的代码
const dimX int = 16 //X轴场地维度
const dimY int = 16 //Y轴场地维度
var bombs int = 40 //地雷数量
var number [dimX][dimY]int8 //单元格中的数字,如果为空则为0,如果是地雷则为-1
var isOpen [dimX][dimY]bool //单元格是否对用户可见
var isFlagged [dimX][dimY]bool //单元格是否标记为地雷
是否有办法在声明变量后,在函数中对它们进行初始化,类似于这样(显然这种方式在Go语言中是不可能的):
//我希望的做法
var dimX,dimY,bombs int
var number [][]int8
func main() {
Scan(&dimX, &dimY, &bombs)
number = [dimX][dimY]
...
}
我还尝试了使用make函数,但它给我一个不兼容的赋值错误(无法将make([][]uint8, dimX)(类型为[][]uint8)赋值给[number] [16] [16] int8):
func main() {
Scan(&dimX, &dimY, &bombs)
number = make([][]uint8, dimX)
for i := range number {
number[i] = make([]uint8, dimY)
}
...
}
我找到的唯一可行的方法是使用append()函数:将场地声明为[][]或[0][0],在用户输入后运行一个函数,该函数将x次和y次append()到切片中。
英文:
I need to create a bidimensional slice with a custom length, taken in input. The slice is the field of my minesweeper, so the length will be the dimension of the field.
I need the slice to be global, right now I inizialize the slice directly to the costant dimension of the field, but I am implementing the difficulty, so I need the field to be variable.
//my code right now
const dimX int = 16 //X field dimension
const dimY int = 16 //Y field dimension
var bombs int = 40 //number of mines
var number [dimX][dimY]int8 //number contained in the cell, 0 if empty, -1 if mine
var isOpen [dimX][dimY]bool //if the cell is visible to the user
var isFlagged [dimX][dimY]bool //if the cell is marked as a bomb
Is there a way I can declare the variables and then initialize them in a function, something like this (that way is obv not possible in go):
//what i'd like to do
var dimX, dimY, bombs int
var number [][]int8
func main() {
Scan(&dimX, &dimY, &bombs)
number = [dimX][dimY]
...
}
I tried also something with make, but it gives me an incompatible assign (cannot use make([][]uint8, dimX) (value of type [][]uint8) as [16][16]int8):
func main() {
Scan(&dimX, &dimY, &bombs)
number = make([][]uint8, dimX)
for i := range number {
number[i] = make([]uint8, dimY)
}
...
}
The only way I found is viable is via the append() function: declare the field to [][] or [0][0] and after the user input run a function that append() x times and y times to the slice
答案1
得分: 3
问题中的第一个代码片段使用了一个数组的数组。数组的长度在编译时确定。
使用第二个代码片段中的切片的切片。切片的长度是动态的。
像第三个代码片段中所示,使用make来分配切片。
var dimX, dimY, bombs int
var number [][]int8 // 空的 [] 是一个切片。
fmt.Scan(&dimX, &dimY, &bombs)
number = make([][]int8, dimX)
for i := range number {
number[i] = make([]int8, dimY)
}
注意:问题中的代码在引用元素时使用了 int8
和 uint8
,这是不同的类型。本回答使用 int8
。
英文:
The first snippet of code in the question uses an array of arrays. The length of an array is set at compile time.
Use a slice of slices as in the second snippet of code. The length of a slice is dynamic.
Allocate the slices with make as shown in the third snippet of code.
var dimX, dimY, bombs int
var number [][]int8 // empty [] is a slice.
fmt.Scan(&dimX, &dimY, &bombs)
number = make([][]int8, dimX)
for i := range number {
number[i] = make([]int8, dimY)
}
Note: The code in the question uses int8
and uint8
when referring to the elements. These are different types. This answer uses int8
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论