Verifying user input for a magic square game/Comparing arrays (user-defined&programmer-defined)

4.6k views Asked by At

I am trying to program a very simple 3x3 magic square game using arrays.

My program will ask the user thrice to input three numbers each time (for the top row, middle row, bottom row) and then the values will be displayed. The problem is I'm trying to get my program to verify if the user entered the correct values.

I'm not sure how I will do it; I've thought of simply declaring fixed values and just make comparisons/use conditional statements to compare the user's input to those constant values (explains arrays d, e, & f in my code below), but I also thought that that's not rather completely correct since there are many possible values that will be able to make up a magic square, aside from the values I'm going to declare, and of course, I would not want to be that redundant in my code.

I'm not that familiar with arrays yet, I'm a beginner in Java. I'm not sure if it's okay to ask that question on here, but I hope someone will still respond. This is rather important since it's a school project.

I know that the numbers should have a total of 15 horizontally, vertically and diagonally. And I have no idea how to do that.

Although, I guess I would just stick to simply making conditional statements (if no one will answer my question above) since I just found a source that has all the 3x3 magic squares, so if someone could just teach me how to compare user-defined arrays & programmer-defined arrays, that would be nice.

I have written some codes to store the user input.

import java.util.*;
class arrayy
{
public static void main(String args[])
{
    int[] a=new int[3];
    int[] b=new int[3];
    int[] c=new int[3];
    int[] d={8,1,6};
    int[] e={3,5,7};
    int[] f={4,9,2};

    Scanner sc=new Scanner(System.in);

    System.out.println("Please enter the 3 numbers for the top row: ");
    for(int j=0;j<3;j++)
        a[j]=sc.nextInt();


        System.out.println();

        System.out.println("Please enter the 3 numbers for the middle row: ");
    for(int j=0;j<3;j++)
        b[j]=sc.nextInt();

                System.out.println();
        System.out.println("Please enter the 3 numbers for the bottom row: ");
    for(int j=0;j<3;j++)
        c[j]=sc.nextInt();


           System.out.println("Your entry: ");

    for (int i=0;i<a.length;i++)
        System.out.print(a[i]+" ");
    System.out.println();
            for (int i=0;i<a.length;i++)
        System.out.print(b[i]+" ");
    System.out.println();
    for (int i=0;i<a.length;i++)
        System.out.print(c[i]+" ");

        System.out.println();
        System.out.println();


}
}
2

There are 2 answers

0
Thirumalai Parthasarathi On

assuming that you have read the values into the array the following code will work for all the combinations that could solve the magic square

    boolean flag = true;
    int sum = 0;
    int i,j;
    for(i=0;i<3;i++){
      //for the row wise check
      sum=0;
      for(j=0;j<3;j++) {
         sum+=arr[i][j];
      }
      if(sum!=15) {
        flag=false;
        break;
      }

      //for the column wise check
      sum=0;
      for(j=0;j<3;j++) {
        sum+=arr[j][i];
      }
      if(sum!=15) {
         flag=false;
         break;
      }

   }

   //for the diagonal check
   sum=0;
   for(j=0;j<3;j++) {
      sum+=arr[j][j];
   }
   if(sum!=15) {
      flag=false;
   }

   sum=0;
   for(i=0,j=2;j>=0;i++,j--) {
      sum+=arr[i][j];
   }
   if(sum!=15) {
      flag=false;
   }

   if(flag) {
      System.out.println("you have cracked it..!!");
   } else {
      System.out.println("better luck next time..!!");
   }
0
sanbhat On

IMO you should maintain a multi-dimensional array for better code readability

that is int[3][3] instead of 3 int[3]'s. Below is my try

public static void main(String [] args) { 
    int[][] input=new int[3][3];
    int[][] defined={{8,1,6}, {3,5,7}, {4,9,2}};

    Scanner sc=new Scanner(System.in);

    //Take input
    //Outer loop
    for(int i=0; i<3; i++) {
        System.out.println("Enter value for Row number - "+(i+1));
        //Inner loop
        for(int j=0; j<3; j++) {
            input[i][j] = sc.nextInt();
        }
    }

    //Compare
    boolean matches = true;
    for(int i=0; i<3; i++) {
        //Inner loop
        for(int j=0; j<3; j++) {
            if(input[i][j] != defined[i][j]) {
                matches = false;
                break;
            }
        }
    }

    if(matches) {
        System.out.println("All matches");
    }
    else {
        System.out.println("No match");
    }
}