如何在Swift Playground中建立金字塔-世界创造?

问题描述 投票:0回答:1

我试图用运动场上的积木建造金字塔,但我不知道如何更改条件以移除多余的积木?

enter image description here

let allCoordinates = world.allPossibleCoordinates
var index = 0

while index != 11 { 
    for coordinate in allCoordinates { 
        if coordinate.row == index || coordinate.column == index || coordinate.row == 11 - index || coordinate.column == 11 - index { 
                for _ in 0...index {
                    world.place(Block(), at: coordinate)
            }
        }
    }
    index += 1
        }
swift swift-playground
1个回答
0
投票
let allCoordinates = world.allPossibleCoordinates
var index = [0, 11]

while index[0] != 11 { 
    for coordinate in allCoordinates { 
        if coordinate.row > index[0] && coordinate.column < index[1] {
            if coordinate.column > index[0] && coordinate.row < index [1] { 
                world.place(Block(), at: coordinate)   
            }
        }
    }
    index[0] += 1
    index[1] -= 1
}
© www.soinside.com 2019 - 2024. All rights reserved.