Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Name: Ian Mahern-Macias
- // Class: 1400-002
- // Program #: 17
- // Due Date: 11/25/14
- //
- // Honor Pledge: On my honor as a student of the University
- // of Nebraska at Omaha, I have neither given nor received
- // unauthorized help on this homework assignment.
- //
- // NAME: Ian Mahern-Macias
- // EMAIL: Ianmahern@gmail.com
- // NUID: 061
- // Colleagues:
- //
- // This program makes "magic boxes" that are 4x4 and determines
- // if what the user entered is is a true magic box or not.
- import java.util.Scanner;
- public class imahernmacias_Magic
- {
- public static void main (String [] args)
- {
- Scanner input = new Scanner (System.in);
- int [][] theSquare = new int [4][4];
- System.out.println("The magic value for your square is 34, which means that every row, \n column and diagonal of your square must add up to that number.");
- System.out.print("Please enter the 4 values for row 0, separated by spaces:");
- theSquare[0][0] = input.nextInt();
- theSquare[0][1] = input.nextInt();
- theSquare[0][2] = input.nextInt();
- theSquare[0][3] = input.nextInt();
- //System.out.println("");
- System.out.print("Please enter the 4 values for row 1, separated by spaces:");
- theSquare[1][0] = input.nextInt();
- theSquare[1][1] = input.nextInt();
- theSquare[1][2] = input.nextInt();
- theSquare[1][3] = input.nextInt();
- //System.out.println("");
- System.out.print("Please enter the 4 values for row 2, separated by spaces:");
- theSquare[2][0] = input.nextInt();
- theSquare[2][1] = input.nextInt();
- theSquare[2][2] = input.nextInt();
- theSquare[2][3] = input.nextInt();
- //System.out.println("");
- System.out.print("Please enter the 4 values for row 3, separated by spaces:");
- theSquare[3][0] = input.nextInt();
- theSquare[3][1] = input.nextInt();
- theSquare[3][2] = input.nextInt();
- theSquare[3][3] = input.nextInt();
- System.out.println("Checking square for problems:");
- boolean valid = true;
- valid = checkDiagonals(theSquare);
- valid = checkRows(theSquare);
- valid = checkColumns(theSquare);
- valid = checkRange(theSquare);
- //fucking check magic here, asshole. also print
- System.out.print(" MAGIC:");
- if ( valid == true)
- {
- System.out.println("VALID");
- }
- if (valid == false)
- {
- System.out.println("NO");
- }
- }
- public static boolean checkRows (int theSquare[][])
- {
- System.out.print(" ROWS: ");
- boolean valid = true;
- if (theSquare[0][0] + theSquare[0][1] + theSquare[0][2] + theSquare[0][3] != 34)
- {
- valid = false;
- System.out.print("0 ");
- }
- if (theSquare[1][0] + theSquare[1][1] + theSquare[1][2] + theSquare[1][3] != 34)
- {
- valid = false;
- System.out.print("1 ");
- }
- if (theSquare[2][0] + theSquare[2][1] + theSquare[2][2] + theSquare[2][3] != 34)
- {
- valid = false;
- System.out.print("2 ");
- }
- if (theSquare[3][0] + theSquare[3][1] + theSquare[3][2] + theSquare[3][3] != 34)
- {
- valid = false;
- System.out.print("3 ");
- }
- if ( valid == true)
- {
- System.out.print("VALID");
- }
- System.out.println("");
- return valid;
- }
- public static boolean checkColumns (int theSquare[][])
- {
- System.out.print(" COLS: ");
- boolean valid = true;
- if (theSquare[0][0] + theSquare[1][0] + theSquare[2][0] + theSquare[3][0] != 34)
- {
- valid = false;
- System.out.print("0 ");
- }
- if (theSquare[0][1] + theSquare[1][1] + theSquare[2][1] + theSquare[3][1] != 34)
- {
- valid = false;
- System.out.print("1 ");
- }
- if (theSquare[0][2] + theSquare[1][2] + theSquare[2][2] + theSquare[3][2] != 34)
- {
- valid = false;
- System.out.print("2 ");
- }
- if (theSquare[0][3] + theSquare[1][3] + theSquare[2][3] + theSquare[3][3] != 34)
- {
- valid = false;
- System.out.print("3 ");
- }
- if ( valid == true)
- {
- System.out.print("VALID");
- }
- System.out.println("");
- return valid;
- }
- public static boolean checkDiagonals (int theSquare[] [])
- {
- System.out.print(" DIAG: ");
- boolean valid = true;
- if (theSquare[0][0] + theSquare[1][1] + theSquare[2][2] + theSquare[3][3] != 34)
- {
- valid = false;
- System.out.print("0 ");
- }
- if (theSquare[3][0] + theSquare[2][1] + theSquare[1][2] + theSquare[0][3] != 34)
- {
- valid = false;
- System.out.print("1 ");
- }
- if ( valid == true)
- {
- System.out.print("VALID");
- }
- System.out.println("");
- return valid;
- }
- public static boolean checkRange (int theSquare[][])
- {
- System.out.print(" RANG: ");
- boolean booleanArray[] = new boolean [17];
- for (int i = 0; i < 17; i++)
- {
- booleanArray[i] = false;
- }
- boolean valid = true;
- for (int r = 0; r < theSquare.length; r++)
- {
- for (int c = 0; c < theSquare[r].length; c++)
- {
- if (theSquare[r][c] < 17 && theSquare[r][c] > 0 && booleanArray[theSquare[r][c]] == false)
- {
- booleanArray[theSquare[r][c]] = true;
- }
- else
- {
- System.out.printf("%d ", theSquare[r][c]);
- valid = false;
- }
- }
- }
- if (valid == true)
- {
- System.out.print("VALID");
- }
- System.out.println("");
- return valid;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement