-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMagicSquares.swift
More file actions
105 lines (86 loc) · 3.25 KB
/
Copy pathMagicSquares.swift
File metadata and controls
105 lines (86 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright (c) 2022 Mel Aguoth All rights reserved.
//
// Created By: Mel Aguoth
// Date: January 31, 2022
// This generates, authenticates, and displays 3 by 3 magic squares.
// Import modules.
import Foundation
func printMagicSquare(outputSquare: [Int]) {
// Format and display the magic squares.
print("\n" + "*****")
for counter in 0..<outputSquare.count {
if counter == 3 || counter == 6 {
print("\n" + "\(outputSquare[counter])", terminator: " ")
} else {
print("\(outputSquare[counter])", terminator: " ")
}
}
print("\n" + "*****")
}
func magicCheck(intArray: [Int]) -> Bool {
// If the array isn't a valid 9 element array, return false.
if intArray.count != 9 {
return false
}
// If the array has repeating numbers, return false.
for outerCounter in 0..<intArray.count {
for innerCounter in 0..<intArray.count {
if outerCounter == innerCounter {
continue
} else if intArray[outerCounter] == intArray[innerCounter] {
return false
}
}
}
// If the horizontal sums aren't 15, return false.
for horizontalCounter in stride(from: 0, to: 6, by: 3) where intArray[horizontalCounter]
+ intArray[horizontalCounter + 1] + intArray[horizontalCounter + 2] != 15 {
return false
}
// If the vertical sums aren't 15, return false.
for verticalCounter in 0..<intArray.count / 3 where intArray[verticalCounter]
+ intArray[verticalCounter + 3] + intArray[verticalCounter + 6] != 15 {
return false
}
// If the main diagonal sums aren't 15, return false.
if intArray[0] + intArray[4] + intArray[8] != 15 || intArray[2] + intArray[4] + intArray[6] != 15 {
return false
}
// If the array is a magic square, return true.
else {
return true
}
}
func magicSquares(magicSquareArray: [Int], extraArray: [Int], wizardNum: Int) {
// Declare constants.
let tempMagicNum: Int = wizardNum
// Declare variables.
var tempMagicArray: [Int] = magicSquareArray
var tempEmptyArray: [Int] = extraArray
// Generate a magic square.
for magicCounter in 0..<tempMagicArray.count where tempEmptyArray[magicCounter] == 0 {
tempEmptyArray[magicCounter] = 1
tempMagicArray[magicCounter] = tempMagicNum + 1
// Recurse until the magic square array is full.
if tempMagicNum < tempMagicArray.count - 1 {
magicSquares(magicSquareArray: tempMagicArray, extraArray: tempEmptyArray, wizardNum: tempMagicNum + 1)
}
/* When full, check if the magic square array is a genuine magic square,
* and if so, print it to the user. */
else if magicCheck(intArray: tempMagicArray) {
printMagicSquare(outputSquare: tempMagicArray)
}
// Reset the extra array.
tempEmptyArray[magicCounter] = 0
}
}
// Declare the arrays.
let numArray: [Int] = Array(repeating: 0, count: 9)
let freeSpaceArray: [Int] = Array(repeating: 0, count: numArray.count)
// Introduce the program.
print("This program displays all possible magic squares with a magic order of 3."
+ " In other words, it shows all the 3 by 3 magic squares whose vertical, horizontal,"
+ " and main diagonal sums add to 15.")
// Call magicSquares.
print("\n" + "Here are all the possible order 3 magic squares: ")
magicSquares(magicSquareArray: numArray, extraArray: freeSpaceArray, wizardNum: 0)