【问题标题】:How to build a pyramid in Swift Playground - World Creation?如何在 Swift Playground - World Creation 中建造金字塔?
【发布时间】:2020-05-17 17:43:46
【问题描述】:

我尝试用游乐场积木搭建金字塔,但我不明白应该如何更改条件以移除多余的积木?

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】:
    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
    }
    

    【讨论】:

      猜你喜欢
      • 2020-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-15
      • 2021-07-08
      相关资源
      最近更新 更多