Magic Square on android

0

I wanted to make a magic square, where I put 9 numbers, and the sum of them has to be 15 (on the diagonals, horizontal and vertical) I'm doing in android studio. I only have this until then:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import static android.icu.lang.UCharacter.GraphemeClusterBreak.V;
import static java.lang.Double.parseDouble;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    final EditText ET1 = (EditText) findViewById(R.id.ET1);
    final EditText ET2 = (EditText) findViewById(R.id.ET2);
    final EditText ET3 = (EditText) findViewById(R.id.ET3);
    final EditText ET4 = (EditText) findViewById(R.id.ET4);
    final EditText ET5 = (EditText) findViewById(R.id.ET5);
    final EditText ET6 = (EditText) findViewById(R.id.ET6);
    final EditText ET7 = (EditText) findViewById(R.id.ET7);
    final EditText ET8 = (EditText) findViewById(R.id.ET8);
    final EditText ET9 = (EditText) findViewById(R.id.ET9);
    Button BT1 = (Button) findViewById(R.id.BT1);
    Button BT2 = (Button) findViewById(R.id.BT2);

    BT2.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_DOWN) {

                ET1.setText("");
                ET2.setText("");
                ET3.setText("");
                ET4.setText("");
                ET5.setText("");
                ET6.setText("");
                ET7.setText("");
                ET8.setText("");
                ET9.setText("");
            }
            return false;
        }
    });


}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.diego.teste.MainActivity">

<Button
    android:id="@+id/BT2"
    android:layout_width="118dp"
    android:layout_height="66dp"
    android:layout_marginBottom="20dp"
    android:layout_marginEnd="28dp"
    android:text="@string/limpa"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    tools:ignore="MissingConstraints"
    android:layout_marginRight="28dp" />


<Button
    android:id="@+id/BT1"
    android:layout_width="113dp"
    android:layout_height="66dp"
    android:layout_marginBottom="20dp"
    android:layout_marginStart="40dp"
    android:text="@string/verifica"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    tools:ignore="MissingConstraints"
    android:layout_marginLeft="40dp" />


<EditText
    android:id="@+id/ET1"
    android:layout_width="100dp"
    android:layout_height="70dp"
    android:layout_marginBottom="16dp"
    android:layout_marginEnd="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:ems="10"
    android:inputType="number"
    android:shadowColor="@android:color/background_light"
    app:layout_constraintBottom_toTopOf="@+id/ET4"
    app:layout_constraintEnd_toStartOf="@+id/ET2"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:ignore="MissingConstraints" />


<EditText
    android:id="@+id/ET2"
    android:layout_width="100dp"
    android:layout_height="70dp"
    android:layout_marginBottom="16dp"
    android:layout_marginEnd="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="8dp"
    android:ems="10"
    android:inputType="number"
    android:shadowColor="@android:color/background_light"
    app:layout_constraintBottom_toTopOf="@+id/ET5"
    app:layout_constraintEnd_toStartOf="@+id/ET3"
    app:layout_constraintStart_toEndOf="@+id/ET1"
    app:layout_constraintTop_toTopOf="parent"
    tools:ignore="MissingConstraints" />


<EditText
    android:id="@+id/ET3"
    android:layout_width="100dp"
    android:layout_height="70dp"
    android:layout_marginBottom="16dp"
    android:layout_marginEnd="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:ems="10"
    android:inputType="number"
    app:layout_constraintBottom_toTopOf="@+id/ET6"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/ET2"
    app:layout_constraintTop_toTopOf="parent"
    tools:ignore="MissingConstraints" />


<EditText
    android:id="@+id/ET4"
    android:layout_width="100dp"
    android:layout_height="70dp"
    android:layout_marginBottom="16dp"
    android:layout_marginEnd="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:ems="10"
    android:inputType="number"
    app:layout_constraintBottom_toTopOf="@+id/ET7"
    app:layout_constraintEnd_toStartOf="@+id/ET5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/ET1"
    tools:ignore="MissingConstraints" />


<EditText
    android:id="@+id/ET5"
    android:layout_width="100dp"
    android:layout_height="70dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:ems="10"
    android:inputType="number"
    app:layout_constraintBottom_toTopOf="@+id/ET8"
    app:layout_constraintEnd_toStartOf="@+id/ET6"
    app:layout_constraintHorizontal_bias="0.526"
    app:layout_constraintStart_toEndOf="@+id/ET4"
    app:layout_constraintTop_toBottomOf="@+id/ET2"
    app:layout_constraintVertical_bias="0.529"
    tools:ignore="MissingConstraints" />


<EditText
    android:id="@+id/ET6"
    android:layout_width="100dp"
    android:layout_height="70dp"
    android:layout_marginBottom="16dp"
    android:layout_marginEnd="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:ems="10"
    android:inputType="number"
    app:layout_constraintBottom_toTopOf="@+id/ET9"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/ET5"
    app:layout_constraintTop_toBottomOf="@+id/ET3"
    app:layout_constraintVertical_bias="0.428"
    tools:ignore="MissingConstraints" />


<EditText
    android:id="@+id/ET7"
    android:layout_width="100dp"
    android:layout_height="70dp"
    android:layout_marginBottom="194dp"
    android:layout_marginEnd="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:ems="10"
    android:inputType="number"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/ET8"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/ET4"
    app:layout_constraintVertical_bias="1.0"
    tools:ignore="MissingConstraints" />


<EditText
    android:id="@+id/ET8"
    android:layout_width="100dp"
    android:layout_height="70dp"
    android:layout_marginBottom="190dp"
    android:layout_marginEnd="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:ems="10"
    android:inputType="number"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/ET9"
    app:layout_constraintStart_toEndOf="@+id/ET7"
    app:layout_constraintTop_toBottomOf="@+id/ET5"
    tools:ignore="MissingConstraints" />

<EditText
    android:id="@+id/ET9"
    android:layout_width="100dp"
    android:layout_height="70dp"
    android:layout_marginBottom="194dp"
    android:layout_marginEnd="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:ems="10"
    android:inputType="number"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toEndOf="@+id/ET8"
    app:layout_constraintTop_toBottomOf="@+id/ET6"
    tools:ignore="MissingConstraints" />

Can anyone help me, in the number removal part of the variables, sum and check?

    
asked by anonymous 19.12.2017 / 23:33

1 answer

1

A flexible and organized way of finding the sums is by using two-dimensional arrays representing a grid. For this you will need to pick up each value of the EdiText and interpret as integer with Integer.parseInt and put in the respective home. Then with fors you can go through each line, column and diagonal and confirm if the total is expected:

public boolean verificarQuadrado(int total){
    EditText ET1 = (EditText) findViewById(R.id.ET1);
    EditText ET2 = (EditText) findViewById(R.id.ET2);
    EditText ET3 = (EditText) findViewById(R.id.ET3);
    EditText ET4 = (EditText) findViewById(R.id.ET4);
    EditText ET5 = (EditText) findViewById(R.id.ET5);
    EditText ET6 = (EditText) findViewById(R.id.ET6);
    EditText ET7 = (EditText) findViewById(R.id.ET7);
    EditText ET8 = (EditText) findViewById(R.id.ET8);
    EditText ET9 = (EditText) findViewById(R.id.ET9);

    int[][] numeros = {
        { 
            Integer.parseInt(ET1.getText().toString()),
            Integer.parseInt(ET2.getText().toString()),
            Integer.parseInt(ET3.getText().toString())
        },
        { 
            Integer.parseInt(ET4.getText().toString()),
            Integer.parseInt(ET5.getText().toString()),
            Integer.parseInt(ET6.getText().toString())
        },
        { 
            Integer.parseInt(ET7.getText().toString()),
            Integer.parseInt(ET8.getText().toString()),
            Integer.parseInt(ET9.getText().toString())
        }   
    };

    for (int  i = 0; i < numeros.length; ++i){
        int linha = 0, coluna = 0;

        for (int j = 0; j < numeros[i].length; ++j){
            linha += numeros[i][j];
            coluna += numeros[j][i];
        }

        if (linha != total || coluna != total)
            return false;
    }

    int diag1 = 0, diag2 = 0;

    for (int i = 0; i < numeros.length; ++i){
        diag1 += numeros[i][i];
        diag2 += numeros[i][numeros.length-i-1];
    }

    return diag1 == total && diag2 == total;
}

This method verificarQuadrado returns true if the magic square has the desired total or false otherwise. So you just have to use it like this:

if (verificarQuadrado(15)) { //ou if(verificarQuadrado(15) == true)
    //código para quando o usuário ganhou
}

Note that I assumed that EditText is positioned as follows:

-------------------
| ET1 | ET2 | ET3 |
-------------------
| ET4 | ET5 | ET6 |
-------------------
| ET7 | ET8 | ET9 |
-------------------

In addition, the verification algorithm does not use any fixed size, so it will work for a 3x3, 4x4 or larger magic square, as long as you start the two-dimensional array appropriately.

Note: From the API 26 you do not need the conversion when you do findViewById , so when you get an element to the layout:

EditText ET1 = (EditText) findViewById(R.id.ET1);

You can do this as:

EditText ET1 = findViewById(R.id.ET1);

Which is simpler and less annoying to write.

    
20.12.2017 / 00:20