简体   繁体   中英

Increment Values in an array (Swift)

So I'm in class and I'm trying to make our thing a little easier. We are trying to make a Q*bert like landscape, and we are using arrays. I'm trying to make it a lot shorter and repeatable. So I have:

let allCoordinate = world.allPossibleCoordinates
let coordinaterow = coordinate.row
var coordinaterow = 3

while coodinaterow != 8 {

    var heights: [Int] = [0,0,0,0,0,0,0,0,1]

    var index = 0

    for coordinate in allCoordinates {
        if coordinate.row ==3 {
            if index == height.count {
                index = 0
            }

            for i in 0...heights[index] {
                world.place(Block(), at: coordinate)
            }
            index += 1
        }
    }

    allCoordinate += 1
    coordinaterow += 1
}

How would Increment the heights array like:

0,0,0,0,0,0,0,0,1 -- 0,0,0,0,0,0,0,1,2 -- 0,0,0,0,0,0,1,2,3

and so forth

You can iterate your collection indices reversed and check if each element is equal to zero, increase and break the iteration, otherwise just increase it:

var heights: [Int] = [0,0,0,0,0,0,0,1,2]

for index in heights.indices.reversed() {
    if heights[index] == 0 {
        heights[index] += 1
        break
    }
    heights[index] += 1
}

print(heights)   // [0, 0, 0, 0, 0, 0, 1, 2, 3]

try this:

func increment(array: [Int]) -> [Int] {

    var result = array

    var lastValue = false

    result = result.reversed().map { value in
        if lastValue { return 0 }
        if value == 0 {
            lastValue = true
            return 1
        }
        return value + 1
    }
    result.reverse()
    return result
}

An alternative approach to getting a height ramp (which seems to be what you are trying to achieve) is not to think about incrementing an existing array, but creating it. The following code demonstrates this:

let heightCount = 10
let rampCount = heightCount // can be any value <= heightCount
for ramp in 1...rampCount {
    let rampHeights = [Int](repeating: 0, count: heightCount-ramp) + [Int](1...ramp)
    print(rampHeights)
}

The output from this code will be

[0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0, 0, 1, 2]
[0, 0, 0, 0, 0, 0, 0, 1, 2, 3]
[0, 0, 0, 0, 0, 0, 1, 2, 3, 4]
[0, 0, 0, 0, 0, 1, 2, 3, 4, 5]
[0, 0, 0, 0, 1, 2, 3, 4, 5, 6]
[0, 0, 0, 1, 2, 3, 4, 5, 6, 7]
[0, 0, 1, 2, 3, 4, 5, 6, 7, 8]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

As you can see the heightCount value determines the overall length of the array and within the loop the ramp value determines the number of non-zero values in the ramp .

Note: the loop is just for demonstration purposes. The key line of code is:

let rampHeights = [Int](repeating: 0, count: heightCount-ramp) +
                  [Int](1...ramp)

which you can put wherever you need in your code. You just need to make sure heightCount and ramp have appropriate values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM