这是一个遍历点数组的函数.点是一个包含int类型元素x和y的 struct .遍历函数是从另一个值为dir的函数调用的.可以避免循环开始时的if判断,因为调用函数后,dir值不会改变.有没有更好的方式来表达围棋中的这种逻辑?struct方法返回一个点.我提供了一个struct方法的示例.
type Point struct {
x int
y int
}
func (p Point) next_row() Point {
nearby := Point{p.x + 1, p.y}
return nearby
}
func trv_point(p Point, p_list []Point, dir string) int {
seg_count := 0
var nearby Point
for {
if dir == "up" {
nearby = p.prev_row()
}
if dir == "down" {
nearby = p.next_row()
}
if dir == "left" {
nearby = p.prev_col()
}
if dir == "right" {
nearby = p.next_col()
}
if !check_point_in_slice(p_list, nearby) {
break
}
seg_count++
p = nearby
}
return seg_count
}