본문 바로가기

swift

[swift] 달팽이

// Playground - noun: a place where people can play


import UIKit


let n = 5


var map: [[Int]] = [[Int]](count: n, repeatedValue: [Int](count: n, repeatedValue: 0))

var number = 1

var lineCount = n

var xy: (x:Int, y:Int) = (-1, 0)


while true {

    for i in 0..<lineCount {

        xy.x++

        map[xy.y][xy.x] = number++

    }

    lineCount--

    

    for i in 0..<lineCount {

        xy.y++

        map[xy.y][xy.x] = number++

    }

    if number > n*n {

        break

    }

    

    for i in 0..<lineCount {

        xy.x--

        map[xy.y][xy.x] = number++

    }

    lineCount--

    

    for i in 0..<lineCount {

        xy.y--

        map[xy.y][xy.x] = number++

    }

    if (number > n*n) {

        break

    }

}



for first in map {

    for value in first {

        print("\(value) ")

    }

    println()

}


println("END")




lineCount-- 할 때, 

lineCount -- 와 같이 -- 앞에 한 칸 띄면 안됨 

'swift' 카테고리의 다른 글

[swift] 1차원 배열 만들기  (0) 2015.03.17
[swift] 비어있는 2차원 배열 만들기  (0) 2015.03.15