Last active
April 18, 2016 18:12
-
-
Save bigolpete/82e3c3f216b62083143cabf00af2a50c to your computer and use it in GitHub Desktop.
This file contains 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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace Magic_Square | |
{ | |
class Program | |
{ | |
static int[] threeXMagic = { 8, 1, 6, | |
3, 5, 7, | |
4, 9, 2 }; | |
static int[] fourXMagic = { 16, 2, 3, 13, | |
5, 11, 10, 8, | |
9, 7, 6, 12, | |
4, 14, 15, 1 }; | |
static int[] fiveXMagic = { 17, 24 , 1 , 8 , 15, | |
23, 5 , 7 , 14 , 16, | |
4, 6 , 13, 20 , 22, | |
10, 12 , 19, 21 , 3 , | |
11, 18 , 25, 2 , 9 }; | |
static int[] badFiveXMagic = { 17, 24 , 1 , 8 , 15, | |
23, 5 , 8 , 14 , 16, | |
4, 6 , 13, 20 , 22, | |
10, 12 , 19, 22 , 3 , | |
11, 18 , 25, 2 , 9 }; | |
static int[] eightXMagic = {1,32,36,61,5,28,40,57,62,35,31,2,58,39,27,6, | |
63,34,30,3,59,38,26,7,4,29,33,64,8,25,37,60, | |
9,24,44,53,13,20,48,49,54,43,23,10,50,47,19,14, | |
55,42,22,11,51,46,18,15,12,21,41,56,16,17,45,52 }; | |
static int[] finishedSquare = new int[9]; //Empty array to take in an unfinished array | |
static int[] goodUnfinished = { 8, 1, 6, | |
3, 5, 7 }; | |
static int[] badUnfinished = { 3, 5, 7, | |
8, 1, 6 }; | |
static int totalCount = 0; //Grand total of square (1+2+3+...25 on a 5x5 square) | |
static double size = 0; //How many rows and columns | |
static int perfectCount = 0; //Each line should equal this if perfect | |
static bool isPerfect = true; //Will be flipped to false if anything isnt perfect | |
static void Main(string[] args) | |
{ | |
try | |
{ | |
finishTheSquare(goodUnfinished); | |
finishTheSquare(badUnfinished); | |
runCheckOn(threeXMagic); | |
runCheckOn(fourXMagic); | |
runCheckOn(fiveXMagic); | |
runCheckOn(badFiveXMagic); | |
runCheckOn(eightXMagic); | |
} | |
catch | |
{ | |
Console.WriteLine("Error"); | |
} | |
Console.Read(); //Pause the program | |
} | |
static void runCheckOn(int[] magicSquare) | |
{ | |
countTotal(magicSquare); //Get square stats and print to console | |
printMagicSquare(magicSquare); | |
checkRows(magicSquare); //The check sequance | |
checkColumns(magicSquare); | |
checkTopLeftDiagonal(magicSquare); | |
checkbotLeftDiagonal(magicSquare); | |
printMagicness(); //Print if magic | |
} | |
static void checkRows(int[] magicSquare) | |
{ | |
//Check all rows | |
for (int i = 0; i < magicSquare.Length; i += Convert.ToInt32(size)) | |
{ | |
int counter = 0; | |
for (int j = i; j < i + size; j++) | |
{ | |
counter += magicSquare[j]; | |
} | |
if (counter != perfectCount) | |
{ | |
isPerfect = false; | |
} | |
} | |
} | |
static void checkColumns(int[] magicSquare) | |
{ | |
//Check all columns | |
for (int i = 0; i <size; i++) | |
{ | |
int counter = 0; | |
for (int j = i; j < magicSquare.Length; j += Convert.ToInt32(size)) | |
{ | |
counter += magicSquare[j]; | |
} | |
if (counter != perfectCount) | |
{ | |
isPerfect = false; | |
} | |
} | |
} | |
static void checkTopLeftDiagonal(int[] magicSquare) | |
{ | |
//Check top diagonal | |
int counter = 0; | |
for (int i = 0; i < magicSquare.Length; i += (Convert.ToInt32(size) + 1)) | |
{ | |
counter += magicSquare[i]; | |
} | |
if (counter != perfectCount) | |
{ | |
isPerfect = false; | |
} | |
} | |
static void checkbotLeftDiagonal(int[] magicSquare) | |
{ | |
//Check bottom diagonal | |
int counter = 0; | |
for (int i = (Convert.ToInt32(size)-1); i< magicSquare.Length -1; i += (Convert.ToInt32(size) -1)) | |
{ | |
counter += magicSquare[i]; | |
} | |
if (counter != perfectCount) | |
{ | |
isPerfect = false; | |
} | |
} | |
static void countTotal(int[] magicSquare) | |
{ | |
//Get stats of the magic square | |
for (int i = 0; i < magicSquare.Length; i++) | |
{ | |
totalCount += magicSquare[i]; | |
} | |
size = Math.Sqrt(magicSquare.Length); | |
perfectCount = totalCount / Convert.ToInt32(size); | |
Console.WriteLine("Total of a perfect for this square is {0}", totalCount); //Print stats to console | |
Console.WriteLine("There are {0} rows and columns", size); | |
Console.WriteLine("Each line should add up to {0} to be perfect", perfectCount); | |
} | |
static void printMagicness() | |
{ | |
//Check if the square has been flipped to false and print if magical | |
Console.WriteLine(); | |
if (isPerfect == true) | |
{ | |
Console.WriteLine("It's MAGIC"); | |
} | |
else | |
{ | |
Console.WriteLine("Not magical"); | |
} | |
Console.WriteLine(); | |
isPerfect = true; //Reset the counts for the next check | |
totalCount = 0; | |
perfectCount = 0; | |
size = 0; | |
} | |
static void printMagicSquare(int[] magicSquare) //Prints the array to console | |
{ | |
for (int i = 0; i < magicSquare.Length; i += Convert.ToInt32(size)) | |
{ | |
for (int j = i; j < i + size; j++) | |
{ | |
Console.Write(magicSquare[j] + " "); | |
} | |
Console.WriteLine(); | |
} | |
} | |
static void finishTheSquare(int[] magicSquare) | |
{ | |
for(int i = 0; i<6; i++) //Adds unfinished array to placeholder | |
{ | |
finishedSquare[i] = magicSquare[i]; | |
} | |
finishedSquare[6] = (15 - (magicSquare[0] + magicSquare[3])); //Calculates bottom row and adds to placeholder | |
finishedSquare[7] = (15 - (magicSquare[1] + magicSquare[4])); | |
finishedSquare[8] = (15 - (magicSquare[2] + magicSquare[5])); | |
Console.WriteLine("Manually filled in square to equal 15"); | |
runCheckOn(finishedSquare); //runs check on placeholder | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment