Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save simon-andrews/fed8649295f8786c6b524e3d1ae4b84a to your computer and use it in GitHub Desktop.

Select an option

Save simon-andrews/fed8649295f8786c6b524e3d1ae4b84a to your computer and use it in GitHub Desktop.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//System.out.println("Size of magic square: ");
int size = 5; //input.nextInt();
MagicSquare magicSquare = new MagicSquare(size);
System.out.println(magicSquare.getSquareAfter(4, 5)[0]);
magicSquare.computerMagicSquare();
magicSquare.displayMagicSquare();
}
}
class MagicSquare {
private final int size;
private int[][] matrix;
public MagicSquare(int size) {
if (size % 2 == 0) throw new IllegalArgumentException("Size of magic square must be odd");
this.size = size;
matrix = new int[this.size][this.size];
for (int x = 0; x < this.size; x++) {
for (int y = 0; y < this.size; y++) {
matrix[x][y] = 0;
}
}
}
public void computerMagicSquare() {
int middle = size / 2;
matrix[0][middle] = 1;
}
public void displayMagicSquare() {
for (int x = 0; x < this.size; x++) {
for (int y = 0; y < this.size; y++) {
System.out.print(matrix[x][y] + "\t");
}
System.out.println();
}
}
public int[] getSquareAfter(int x, int y) {
int[] point = new int[2];
if(y - 1 < 0) {
point[0] = size;
point[1] = x + 1;
}
else if() {
}
else {
point[0] = y - 1;
point[1] = x + 1;
}
return point;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment