Created
February 12, 2020 02:26
-
-
Save jorgeflorescarlos/cdde73ea905ef5386d34dd90d5c2c793 to your computer and use it in GitHub Desktop.
Cuadro mágico Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func generateSquare(){ | |
var positionX = startingPositionX | |
var positionY = 0 | |
square[positionY][positionX] = 1 | |
var currentPositionX = positionX | |
var currentPositionY = positionY | |
for j in 2...Int(pow(Double(size), 2)) { | |
positionY -= 1 | |
positionX -= 1 | |
if positionX < 0 { | |
positionX = square.count - 1 | |
} | |
if positionY < 0 { | |
positionY = square.count - 1 | |
} | |
if square[positionY][positionX] == 0 { | |
square[positionY][positionX] = j | |
} else { | |
positionX = currentPositionX | |
positionY = currentPositionY + 1 | |
square[positionY][positionX] = j | |
} | |
currentPositionX = positionX | |
currentPositionY = positionY | |
} | |
renderMagicSquare() | |
} | |
func renderMagicSquare(){ | |
for sq in square { | |
print(sq) | |
} | |
} | |
generateSquare() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment