Skip to content

Instantly share code, notes, and snippets.

@jorgeflorescarlos
Created February 12, 2020 02:26
Show Gist options
  • Save jorgeflorescarlos/cdde73ea905ef5386d34dd90d5c2c793 to your computer and use it in GitHub Desktop.
Save jorgeflorescarlos/cdde73ea905ef5386d34dd90d5c2c793 to your computer and use it in GitHub Desktop.
Cuadro mágico Swift
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