Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package magicsquare;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.MouseEvent;
- import java.util.Scanner;
- import javax.swing.*;
- public class Magicsquare {
- public static void main(String[] args) {
- gui g = new gui();
- JFrame f = new JFrame("Magic Square");
- JLabel label = new JLabel("");
- JLabel label2 = new JLabel("");
- JButton setbutton = new JButton("Set");
- JTextField button00 = new JTextField("");
- JTextField button01 = new JTextField("");
- JTextField button02 = new JTextField("");
- JTextField button10 = new JTextField("");
- JTextField button11 = new JTextField("");
- JTextField button12 = new JTextField("");
- JTextField button20 = new JTextField("");
- JTextField button21 = new JTextField("");
- JTextField button22 = new JTextField("");
- setbutton.setBounds(200,400,100,50);
- button00.setBounds(100, 100, 50, 50);
- label.setBounds(400,100,150,50);
- label2.setBounds(400,200,150,50);
- button01.setBounds(200,100,50,50);
- button02.setBounds(300,100,50,50);
- button10.setBounds(100,200,50,50);
- button11.setBounds(200,200,50,50);
- button12.setBounds(300,200,50,50);
- button20.setBounds(100, 300, 50, 50);
- button21.setBounds(200, 300, 50, 50);
- button22.setBounds(300, 300, 50, 50);
- label.setVisible(true);
- label2.setVisible(true);
- f.add(label);
- f.add(label2);
- f.add(setbutton);
- f.add(button00);
- f.add(button01);
- f.add(button02);
- f.add(button20);
- f.add(button21);
- f.add(button22);
- f.add(button10);
- f.add(button11);
- f.add(button12);
- f.setSize(650,550);
- f.setLayout(null);
- f.setVisible(true);
- setbutton.addActionListener(new ActionListener() {
- @Override public void actionPerformed(ActionEvent e) {
- String bb00 = "1";
- String bb01 = "2";
- String bb02 = "3";
- String bb10 = "4";
- String bb11 = "5";
- String bb12 = "6";
- String bb20 = "7";
- String bb21 = "8";
- String bb22 = "9";
- bb00 = button00.getText();
- bb01 = button01.getText();
- bb02 = button02.getText();
- bb10 = button10.getText();
- bb11 = button11.getText();
- bb12 = button12.getText();
- bb20 = button20.getText();
- bb21 = button21.getText();
- bb22 = button22.getText();
- int b00 = Integer.parseInt(bb00);
- int b01 = Integer.parseInt(bb01);
- int b02 = Integer.parseInt(bb02);
- int b10 = Integer.parseInt(bb10);
- int b11 = Integer.parseInt(bb11);
- int b12 = Integer.parseInt(bb12);
- int b20 = Integer.parseInt(bb20);
- int b21 = Integer.parseInt(bb21);
- int b22 = Integer.parseInt(bb22);
- g.setsquare(0, 0, b00);
- g.setsquare(0, 1, b01);
- g.setsquare(0, 2, b02);
- g.setsquare(1, 0, b10);
- g.setsquare(1, 1, b11);
- g.setsquare(1, 2, b12);
- g.setsquare(2, 0, b20);
- g.setsquare(2, 1, b21);
- g.setsquare(2, 2, b22);
- int check,check2;
- check = g.checksquare();
- if(check == 1){
- label.setText("It is a magic Square");
- }
- else{
- label.setText("It is not a magic Square");
- }
- check2 = g.checkrepeat();
- if(check2==1){
- label2.setText("There was a number repeated");
- }
- }
- });
- }
- }
- class gui{
- int multivalue[][] = {{1,2,3},{4,5,6},{7,8,9}};
- Magicsquare m = new Magicsquare();
- int interval = 0;
- public void setsquare(int row, int col, int value){
- multivalue[row][col] = value;
- interval ++;
- }
- public int checksquare(){
- int k;
- int j;
- int i = 0;
- int sum = 15;
- int sum1 = 15;
- int sum2 = 0;
- int sum3 = 0;
- int checkk = 0;
- int fail = 0;
- int success = 0;
- boolean checking = false;
- for(k = 0; k<3;k++){
- if(sum!=15){
- break;
- }
- sum = 0;
- for(j=0;j<3;j++){
- sum = sum+multivalue[k][j];
- }
- }
- for(k = 0; k<3;k++){
- if(sum1!=15){
- break;
- }
- sum1 = 0;
- for(j=0;j<3;j++){
- sum1 = sum1+multivalue[j][k];
- }
- }
- for(k=0; k<3;k++){
- sum2 = sum2+multivalue[k][k];
- }
- for(j=2; j>=0;j--){
- sum3 = sum3+multivalue[j][j];
- }
- if(sum == 15 && sum1 == 15 && sum2 == 15 && sum3 == 15){
- checking = true;
- System.out.println("won");
- }
- if(checking==true){
- return 1;
- }
- else{
- return 2;
- }
- }
- public int checkrepeat(){
- for(int i = 0; i < 3; i++){
- for(int x = 0 ; x< 3; x++){
- for(int y = 0 ; y<3; y++){
- for(int z = 0; z<3;z++){
- if(multivalue[i][x] == multivalue[y][z]){
- if(i==y && x == z){
- System.out.println("do nothing");
- }
- else{
- return 1;
- }
- }
- }
- }
- }
- }
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement