Created
January 7, 2011 18:54
-
-
Save allisons/769914 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
public class OddMagicSquare | |
{ | |
public OddMagicSquare(int n) | |
{ | |
if (isOdd(n)) | |
{ | |
int [][] square = new int [n][n]; | |
int fillInteger = 1; | |
int colPointer = 0; | |
int rowPointer = (n - 1)/2; | |
while (fillInteger <= n*n) | |
{ | |
square[colPointer][rowPointer] = fillInteger; | |
colPointer = wrapAround(colPointer--,n); | |
wrapAround(rowPointer++,n); | |
fillInteger++; | |
}} | |
else | |
throw new IllegalArgumentException("This system works for Odd Magic Squares only."); | |
} | |
public static boolean isMagic(int[][] a) | |
{ | |
if (checkRows(a) && checkCols(a) && checkDiags(a)); | |
return true; | |
} | |
public void main(String[] args) | |
{ | |
System.out.println( "Welcome, and thanks for playing Odd Magic Square. I'm your host your computer and we'll be playing this week with...your computer.\n\n" ); | |
System.out.println( "Let's go over the rules shall we? \n"); | |
System.out.println( "Rule 1, I can only handle positive integer inputs. \n"); | |
System.out.println( "Rule 2, I can only handle odd numbered integers. \n"); | |
System.out.println( "Rule 3, No chewing gum. \n"); | |
} | |
private static boolean checkRows(int[][] a) { | |
int rowCounter = 0; | |
while (rowCounter < a.length) | |
{ | |
int subTotal = 0; | |
int colCounter = 0; | |
while (colCounter < a.length) | |
{ | |
subTotal = subTotal + a[colCounter][rowCounter]; | |
colCounter++; | |
} | |
if (subTotal != (a.length * (a.length*a.length + 1) / 2)) | |
{ | |
return false; | |
} | |
rowCounter++; | |
} | |
return true; | |
} | |
private static boolean checkCols(int [][] a){ | |
int colCounter = 0; | |
while (colCounter < a.length) | |
{ | |
int subTotal = 0; | |
int rowCounter = 0; | |
while (rowCounter < a.length) | |
{ | |
subTotal = subTotal + a[colCounter][rowCounter]; | |
rowCounter++; | |
} | |
if (subTotal != (a.length * (a.length*a.length + 1) / 2)) | |
{ | |
return false; | |
} | |
colCounter++; | |
} | |
return true; | |
} | |
private static boolean checkDiags(int [][] a){ | |
int subTotal = 0; | |
int colCounter = 0; | |
int rowCounter = 0; | |
while (colCounter < a.length) | |
{ | |
subTotal = subTotal + a[colCounter][rowCounter]; | |
colCounter++; | |
rowCounter++; | |
} | |
if (subTotal != (a.length * (a.length*a.length + 1) / 2)) | |
{ | |
return false; | |
} | |
colCounter = 0; | |
subTotal = 0; | |
while (colCounter < a.length) | |
{ | |
subTotal = subTotal + a[colCounter][rowCounter]; | |
colCounter++; | |
rowCounter--; | |
} | |
if (subTotal != (a.length * (a.length*a.length + 1) / 2)) | |
{ | |
return false; | |
} | |
return true; | |
} | |
private static boolean isOdd(int w) | |
{ | |
return (0 != w % 2); | |
} | |
private int wrapAround(int input, int arrayLength) | |
{ | |
return (input + arrayLength) % arrayLength; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment