2016-08-03 8 views
0

2つの整数をパラメータとして取りますが、後でこの関数がStruct arrayのすべての要素を再帰的に呼び出すようにする関数possiblemoves()を定義しました I didn構造体の配列要素をGo言語の関数にパラメータとして渡す

package main 

import (
    "fmt" 
) 

/*type node struct{ 
    prev node 
    current node 
     Next [64] int 
}*/ 
type rowcol struct { 
    row int 
    col int 
} 

func main() { 
    possiblemoves(1, 5) 
} 
func possiblemoves(row int, col int) { 
    var c [8]rowcol 
    var a [16]int 

    a[0] = row + 1 
    a[1] = col - 2 
    a[2] = row - 1 
    a[3] = col + 2 
    a[4] = row + 1 
    a[5] = col + 2 
    a[6] = row - 1 
    a[7] = col - 2 
    a[8] = row - 2 
    a[9] = col + 1 
    a[10] = row - 2 
    a[11] = col - 1 
    a[12] = row + 2 
    a[13] = col - 1 
    a[14] = row + 2 
    a[15] = col + 1 

    for i := 0; i < len(a); i++ { 
     if a[i] <= 0 { 
      a[i] = 0 
     } 
     fmt.Println(a[i]) 
    } 

    c[0] = rowcol{a[0], a[1]} 
    c[1] = rowcol{a[2], a[3]} 
    c[2] = rowcol{a[4], a[5]} 
    c[3] = rowcol{a[6], a[7]} 
    c[4] = rowcol{a[8], a[9]} 
    c[5] = rowcol{a[10], a[11]} 
    c[6] = rowcol{a[12], a[13]} 
    c[7] = rowcol{a[14], a[15]} 

    for j := 0; j < len(c); j++ { 
     { 
      possiblemoves(c[j]) 
     } 
    } 

} 

答えて

3

は単に

type rowcol struct { 
    row int 
    col int 
} 

func possiblemoves(rc []rowcol) {} 

func main() { 
    rc := []rowcol{ 
     rowcol{1, 2}, 
     rowcol{3, 4}, 
    } 
    possiblemoves(rc) 
} 
の操作を行います。「tはまだ終了条件を入れて、私はそれを

コードを終えた後、私はそれを行います210

https://play.golang.org/p/dQ1edTJNhq

[]rowcolは、rowcol構造体のスライスです。次に、rc[1].rowrc[1].colを使用して、これらの構造フィールドにアクセスします。

+0

ありがとうございます! – Riya

関連する問題