我有以下代码,如果您愿意,请随时提供指示:
package main
import (
"fmt"
)
type Grid struct {
rows int
cols int
tiles []Tile
}
type Tile struct {
x int
y int
contents int
}
func (g Grid) AddTile(t Tile) {
g.tiles = append(g.tiles, t)
}
func (g *Grid) Row(num int) []Tile {
numTiles := len(g.tiles)
row := []Tile{}
for i := 0; i < numTiles; i++ {
tile := g.tiles[i]
if (tile.y == num) {
row = append(row, tile)
}
}
return row
}
/*
HERE IS WHERE I NEED HELP
*/
func (g *Grid) SetRow(num, val int) {
row := g.Row(num)
rowLength := len(row)
for i := 0; i < rowLength; i++ {
tile := &row[i]
tile.contents = val
}
}
func (g Grid) Col(num int) []Tile {
numTiles := len(g.tiles)
col := []Tile{}
for i := 0; i < numTiles; i++ {
tile := g.tiles[i]
if (tile.x == num) {
col = append(col, tile)
}
}
return col
}
func MakeTile(x, y int) Tile {
tile := Tile{x: x, y: y}
return tile
}
func MakeGrid(rows, cols int) Grid {
g := Grid{ rows: rows, cols: cols}
for r := 1; r <= rows; r++ {
for c := 1; c <= cols; c++ {
g.tiles = append(g.tiles, MakeTile(r, c))
}
}
return g
}
func main() {
g := MakeGrid(256, 256)
g.SetRow(100, 5)
fmt.Println(g.Row(100))
}
我正在做这个,最重要的是,作为一个帮助我学习 Go 的简单项目。遇到的问题就在这里
/*
HERE IS WHERE I NEED HELP
*/
func (g *Grid) SetRow(num, val int) {
row := g.Row(num)
rowLength := len(row)
for i := 0; i < rowLength; i++ {
tile := &row[i]
tile.contents = val
}
}
在某处似乎我需要指向我试图修改的实际瓷砖。事实上,该SetRow函数实际上并没有修改任何东西。我究竟做错了什么?请记住,我 2 天前才开始学习 Go,所以这是一次学习经历 :)
慕哥6287543
相关分类