2015-10-20 89 views
2

我有一些CGPoint數組,如下面的方法所示。我試圖爲我的精靈過濾一個位置,該位置按照所述順序向下,向左,向上和向右循環移動。如何從數組中隨機選擇一個數組(向下,向左等),然後從所選數組中隨機選擇一個點,並記錄數組,以便當再次調用該方法時,將使用以下組進行生成(遵循循環順序)。例如,因爲down是循環中的第一個,所以隨機選擇一個「向下數組」,如果說,選擇down2,那麼應該從down2隨機選擇一個點,比如p5。下一次調用此方法時,應遵循「左數組」等相同的過程。這似乎相當複雜。如何從數組中隨機篩選和選擇CGPoint

func nextPosition()->CGPoint { 
     let down1 = [p1, p2] 
     let down2 = [p3, p4, p5] 
     let left1 = [p6, p7] 
     let left2 = [p8, p9, p10] 
     let left3 = [p11, p12, p13] 
     let up1 = [p14, p15] 
     let up2 = [p16, p17] 
     let up3 = [p18, p19] 
     let right1 = [p20, p21] 
     let right2 = [p22, p23] 
     let right3 = [p24, p25] 
     let right4 = [p26, p27, p28, p29, p30] 

     //return point  
    } 

回答

0

這應該工作:

func nextPosition() -> CGPoint { 

    var pointsArray:[[CGPoint]] = [[]] 

    let down1 = [p1, p2] 
    pointsArray.append(down1) 
    let down2 = [p3, p4, p5] 
    pointsArray.append(down2) 

    let left1 = [p6, p7] 
    pointsArray.append(left1) 
    let left2 = [p8, p9, p10] 
    pointsArray.append(left2) 
    let left3 = [p11, p12, p13] 
    pointsArray.append(left3) 

    let up1 = [p14, p15] 
    pointsArray.append(up1) 
    let up2 = [p16, p17] 
    pointsArray.append(up2) 
    let up3 = [p18, p19] 
    pointsArray.append(up3) 

    let right1 = [p20, p21] 
    pointsArray.append(right1) 
    let right2 = [p22, p23] 
    pointsArray.append(right2) 
    let right3 = [p24, p25] 
    pointsArray.append(right3) 
    let right4 = [p26, p27, p28, p29, p30] 
    pointsArray.append(right4) 


    let randomDirection: Int = Int(arc4random_uniform(UInt32(pointsArray.count))) 
    let randomPoint: Int = Int(arc4random_uniform(UInt32(pointsArray[randomDirection].count))) 

    return pointsArray[randomDirection][randomPoint] 

} 

我在做什麼基本上是創建一個二維數組。第一個維度是針對方向的,第二個維度是針對該方向的。

+0

追加第一個數組應該是'up1'而不是'up11'。會建議編輯,但它只有一個字符。 –

+0

對不起,錯字,我修好了。 –

+0

此功能也適用於從每個單點中選擇一個隨機變量,但不會按順序返回4個不同點(從下向上,向左,向上和向右)。 –

-1

試試這個。函數的返回值是一個由4個CGPoint組成的數組 - 接下來的四個點,每個方向之一,即第一個是下降點,下一個是左邊點等。函數返回的示例可以是[p3, p7, p19, p25]

func nextPosition() -> [CGPoint] { 

    // Make a separate array for each group of points in each direction 
    let down1 = [p1, p2] 
    let down2 = [p3, p4, p5] 
    let left1 = [p6, p7] 
    let left2 = [p8, p9, p10] 
    let left3 = [p11, p12, p13] 
    let up1 = [p14, p15] 
    let up2 = [p16, p17] 
    let up3 = [p18, p19] 
    let right1 = [p20, p21] 
    let right2 = [p22, p23] 
    let right3 = [p24, p25] 
    let right4 = [p26, p27, p28, p29, p30] 

    // Make separate arrays for each direction which contains the previous arrays 
    let dArray = [down1, down2] 
    let lArray = [left1, left2, left3] 
    let uArray = [up1, up2, up3] 
    let rArray = [right1, right2, right3, right4] 

    // Make an array to group all the directions 
    let array = [dArray, lArray, uArray, rArray] 

    var nextPositions = [CGPoint]() 

    // loop through dArray, lArray etc. 
    for arr in array { 
     // pick random direction array 
     var length = UInt32(arr.count) 
     let random = Int(arc4random_uniform(length)) 

     // pick random point from sub array 
     length = UInt32(arr[random].count) 
     let random2 = Int(arc4random_uniform(length)) 

     // print final random point 
     print(arr[random][random2]) 
     nextPositions.append(arr[random][random2]) 
    } 
    return nextPositions 
}