I need help with where to start with this java program. A magic square is a grid
ID: 3852815 • Letter: I
Question
I need help with where to start with this java program.
A magic square is a grid of unique numbers (i.e. no number can appear more than once) in which the sum of all the rows, columns, and diagonals are the same. Here is an example in which all of the rows, columns and diagonals (upper left corner, center, lower right corner and upper right corner, center, and lower left corner) sum to 15. Generally, the numbers within a magic square range between 1 and n2, where n is the size of the side of the square.
4
9
2
3
5
7
8
1
6
Your goal for this project is to write a program that helps a user to create a magic square. Your program should meet the following requirements:
Initialization: A Magic square is a grid of numbers with n numbers along each side. For example, in the magic square above, n = 3. The value for n can technically be any positive number except 2 (because then there would be no diagonals), but due to hardware limitations it is probably a good idea to put an upper bound on n. For this program we will allow n to be at most 8. When your program begins its execution it should ask the user how big the magic square should be. For example:
Let’s make a Magic Square! How big should it be? 2
That would violate the laws of mathematics!
Let’s make a Magic Square! How big should it be? -4
That would violate the laws of mathematics!
Let’s make a Magic Square! How big should it be? 12
That’s huge! Please enter a number less than 9.
Let’s make a Magic Square! How big should it be? 4
Great!
Display: After the size has been chosen and in between each user input, your program should display the current state of the square. Use zeros to indicate empty places. For example, after the initialization above, your program should display:
The square currently looks like this:
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
Value entry: Allow the user to enter or change a value in the puzzle by providing the row and column of the value they want to change, along with the new value. Keep in mind that a user might want to overwrite an existing value. Also, remember that all numbers must be between 1 and n2. For example:
Where do you want to put a new value?
Row: 0
Column: 3
What value should go there? 2
The square currently looks like this:
0 0 0 2
0 0 0 0
0 0 0 0
0 0 0 0
Where do you want to put a new value?
Row: 0
Column: 3
What value should go there? 4
The square currently looks like this:
0 0 0 4
0 0 0 0
0 0 0 0
0 0 0 0
Where do you want to put a new value?
Row: 0
Column: 3
What value should go there? 191
You can only use numbers between 1 and 16 for this square.
The square currently looks like this:
0 0 0 4
0 0 0 0
0 0 0 0
0 0 0 0
Solution Recognition: When the magic square is complete, congratulate the user. For example:
The square currently looks like this:
7 12 1 14
2 13 8 11
16 3 10 5
9 6 15 4
Victory!
4
9
2
3
5
7
8
1
6
Explanation / Answer
import java.util.*;
class Magic{
int no;
int magic[][];
Scanner sc;
//constructer
public Magic(int no){
this.no=no;
magic=new int[no][no];
sc=new Scanner(System.in);
play();
}
//play
public void play(){
int val,row=-1,col=-1;
boolean done=false;
while(!done){
printSquare();
if(isMagicSquare(magic)){
done=true;
}
else{
System.out.println("Where do you want to put a new value?");
System.out.printf("Row:");
row=sc.nextInt();
System.out.printf("COl:");
col=sc.nextInt();
System.out.println("What value should go there?");
val=sc.nextInt();
if(val>=1 && val<=this.no*this.no){
magic[row][col]=val;
}
else{
System.out.println("You can only use numbers between "+1+" and "+this.no*this.no+" for this square.");
}
}
}
System.out.println("victory");
}
//to print square
public void printSquare(){
System.out.println("The square currently looks like this:");
int i,j;
for(i=0;i<this.no;i++){
for(j=0;j<this.no;j++){
System.out.printf("%3d ",magic[i][j]);
}
System.out.println();
}
}
// to check given square is magic square or not
public boolean isMagicSquare(int[][] arr){
final int n=arr.length;
final int nSquare=n*n;
final int M=(n*n*(n*n+1)/2)/n;
int sumOfRow=0, sumOfColoumns=0, sumOfPrimaryDiagonal=0, sumOfSecondaryDiagonal=0;
boolean[] flag= new boolean[n*n];
for(int row=0; row<n; row++){
sumOfRow=0;
sumOfColoumns=0;
for(int col=0; col<n; col++){
if(arr[row][col]<1 || arr[row][col]>nSquare) return false;
if(flag[arr[row][col]-1]==true) return false;
flag[arr[row][col]-1]=true;
sumOfRow += arr[row][col];
sumOfColoumns += arr[col][row];
}
sumOfPrimaryDiagonal += arr[row][row];
sumOfSecondaryDiagonal += arr[row][n-row-1];
if(sumOfRow!=M || sumOfColoumns!=M) return false;
}
if(sumOfPrimaryDiagonal!=M || sumOfSecondaryDiagonal!=M) return false;
return true;
}
public static void main(String []args){
int size=-1;
Scanner sc=new Scanner(System.in);
boolean inputtaken=false;
while(!inputtaken){
System.out.printf("Lets make a Magic Square! How big should it be?");
size=sc.nextInt();
if(size<3){
System.out.println("That would violate the laws of mathematics!");
}
else if(size>9){
System.out.println("Thats huge! Please enter a number less than 9.");
}
else{
System.out.println("Great");
inputtaken=true;
}
}
Magic mg=new Magic(size);
}
}