This is the code for the Magic Square 4 * 4 game. I’d like for you to make some changes to the my code.

Question

//This is the code for the Magic Square 4 * 4 game. I’d like for you to make some changes to my code.

1) Use methods to calculate the sum of rows and columns in the square

2) Ask users to try again if they input the same number twice.

import java.util.Scanner;
public class Main {
  public static void main(String[] args) {
    System.out.println("Welcome to Magic Square!");
    System.out.println("Please enter 16 numbers!");
    int board[][] = new int[4][4];
    Scanner reader = new Scanner(System.in);
    for (int i = 0; i < board.length; i++) {
        for (int j = 0; j < board.length; j++) {
            board[i][j] = reader.nextInt();
        }
    }
    System.out.println("\n[Sqaure]");
    for (int i = 0; i < board.length; i++) {
        for (int j = 0; j < board   [i].length; j++) {
            System.out.printf("%-5d", board[i][j]);
        }
        System.out.println();
    }

    int sum1 = 0;
    int sum2 = 0;
    int sum3 = 0;
    int sum4 = 0;

    int flag = 0;

    for (int i   = 0; i < board.length; i++) {
        sum1 = sum1 + board[i][i]; 
    }

    for (int i = 0; i < board.length; i++){
        sum2 = sum2 + board[i][board.length-1-i]; 
    }

    if(sum1 != sum2){
        flag = 1;
    }

    for (int i = 0; i < board.length; i++) { 
      sum3 = 0;
      for (int j = 0; j < board[i].length; j++){
        sum3 += board[i][j];   
      }
      if(sum3 != sum1){
        flag = 1;
        break;
      }
    } 
 
    for (int i = 0; i < board.length; i++) { 
      sum4 = 0;
      for (int j = 0; j < board.length; j++){
        sum4 += board[j][i]; 
      }
      if(sum4 != sum1){
        flag = 1;
        break;
      }
    }

    if(flag == 0){
        System.out.println("\nIt is a magic square.");
        System.exit(0);
    }else{
        System.out.println("\nIt is not a magic square.\n");
    }
  }
}

 

Summary

In response to the query, two changes are made to the specified program: First, every number is examined to see if it has previously been input. If the answer is affirmative, the user will be asked to re-enter the number.
This ensures that the square is filled with 16 different numbers.
The addition of the sumOfRows and sumOfColumns functions, which add the sum of each row in the square and the sum of each column in the square, respectively, is another chance.

Explanation

  1. The user will be asked to enter 16 unique numbers in the supplied code.
  2. If the user enters a number that already exists, the user will be required to input it again because the one entered currently is not unique.
  3. The prime diagonal sum is first discovered and placed in sum1, then the other diagonal sum is found and compared to see whether they are equal; if they are not, the square is not a magic square.
  4. If affirmative, the sum1 is compared to the sum of each square’s row and column, which are calculated using the methods “sumOfRows” and “sumOfColumns,” respectively.
  5. It is not a magic square if at least one of the row or square sums is not equal.

 

Code

import java.util.Scanner;
class Main {
    
    /* method to find the sum of rows */
    public static void sumOfRows(int rows[], int board[][]){
        for(int i=0;i<board.length;i++){
            rows[i]=0;
            for(int j=0;j<board[i].length;j++){
                rows[i]+=board[i][j];
            }
        }
    }
    
    /* method to find the sum of columns */
    public static void sumOfColumns(int columns[], int board[][]){
        for(int i=0;i<board.length;i++){
            columns[i]=0;
            for(int j=0;j<board[i].length;j++){
                columns[i]+=board[i][j];
            }
        }
    }
    
  public static void main(String[] args) {
    System.out.println("Welcome to Magic Square!");
    System.out.println("Please enter 16 numbers!");
    int board[][] = new int[4][4];
    Scanner reader = new Scanner(System.in);
    
    /* to ensure the user enters 16 unique numbers */
    int check[]=new int[101];
    for(int i=0;i<=100;i++)
    check[i]=0;
    for (int i = 0; i < board.length; i++) {
        for (int j = 0; j < board.length; j++) {
            board[i][j] = reader.nextInt();
            while(check[board[i][j]]==1){
                System.out.println("You have already entered the number"+board[i][j]);
                System.out.println("Please try again!!");
                board[i][j]=reader.nextInt();
            }
            check[board[i][j]]=1;
        }
    }
    System.out.println("\n[Sqaure]");
    for (int i = 0; i < board.length; i++) {
        for (int j = 0; j < board[i].length; j++) {
            System.out.printf("%-5d", board[i][j]);
        }
        System.out.println();
    }

    int sum1 = 0;
    int sum2 = 0;
    int sum3 = 0;
    int sum4 = 0;
    int flag = 0;

    for (int i   = 0; i < board.length; i++) {
        sum1 = sum1 + board[i][i]; 
    }

    for (int i = 0; i < board.length; i++){
        sum2 = sum2 + board[i][board.length-1-i]; 
    }

    if(sum1 != sum2){
        flag = 1;
    }
    
    /* Calling the sumOfRows method */
    int rows[]=new int[4];
    sumOfRows(rows, board);
    for (int i = 0; i < board.length; i++) { 
      if(rows[i] != sum1){
        flag = 1;
        break;
      }
    } 
    /* Calling the sumOfColumns method */
    int columns[]=new int[4];
    sumOfColumns(columns, board);
    for (int i = 0; i < board.length; i++) { 
      if(columns[i] != sum1){
        flag = 1;
        break;
      }
    }

    if(flag == 0){
        System.out.println("\nIt is a magic square.");
        System.exit(0);
    }else{
        System.out.println("\nIt is not a magic square.\n");
    }
  }
}

Output

 Magic Square 4 * 4 game

 

Also, read check the python code below that doesn’t work properly, adjust it, and explain?

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *