What does the for and what makes the brackets in an array?

5

I asked for help in some forums in a game of the old programmed in Java, I understood the logic, but I did not know how to apply it (what commands to use, and how to use it), and I saw the use of Arrays. After seeing a complete code, I understood everything, except for one part, that part here:

public boolean vitoria (int x){
        for (int i = 0; i < mat.length; i++){
            if(mat[i][0] == x && mat [i][1] == x && mat [i][2] == x){
                return true;
            }
            if(mat[0][i] == x && mat [1][i] == x && mat [2][i] == x){
                return true;
            }
        }
        if(mat[0][0] == x && mat [1][1] == x && mat [2][2] == x){
            return true;
        }
        if(mat[0][2] == x && mat[1][1] == x && mat [2][0] == x){
            return true;
        }
        return false;
    }

My question is: What does (int = 0;i <mat.lenght; i++) mean, and what each line means, such as

if(mat[i][0] == x && mat [i][1] == x && mat [i][2] == x){
                    return true;
                }

What does the square brackets mean? and what are they doing (what role)? Remember that in this old game, mat is the int variable that marks the position that each player has played. Here's the full code if needed:

import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;

public class Jogo extends JFrame {
    private static final long serialVersionUID = 1L;
    private JButton b1,b2,b3,b4,b5,b6,b7,b8,b9;
    int qtde;//verifica quantidade de jogadas da partida
    int jogador;// verifica o jogador da vez, sendo 1 = X e 2 = 0
    int mat[][] = new int [3][3];//marca a posição que cada jogador jogou
    JButton b[] = new JButton[9];//mapeia os botões
    String ganhador;//armazena nome do vencedor
    String jogador1;//armazena nome do jogador 1
    String jogador2;// armazena nome do jogador 2

    public Jogo() {
        setTitle("Jogo da Velha");
        setBounds(190,100,300,400);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        getContentPane().setBackground(new Color(197,197,197));
        setLayout(null);

        JMenuBar mBar = new JMenuBar();
        JMenu opcoes = new JMenu("Opções");
        JMenuItem nJogo = new JMenuItem("Novo Jogo");

        JMenuItem sair = new JMenuItem("Sair");

        mBar.add(opcoes);
        mBar.add(sair);
        opcoes.add(nJogo);

        setJMenuBar(mBar);

        b1 = new JButton();
        b1.setBounds(25,50,60,70);
        this.add(b1);

        b2 = new JButton();
        b2.setBounds(115,50,60,70);
        this.add(b2);

        b3 = new JButton();
        b3.setBounds(205,50,60,70);
        this.add(b3);

        b4 = new JButton();
        b4.setBounds(25,140,60,70);
        this.add(b4);

        b5 = new JButton();
        b5.setBounds(115,140,60,70);
        this.add(b5);

        b6 = new JButton();
        b6.setBounds(205,140,60,70);
        this.add(b6);

        b7 = new JButton();
        b7.setBounds(25,230,60,70);
        this.add(b7);

        b8 = new JButton();
        b8.setBounds(115,230,60,70);
        this.add(b8);

        b9 = new JButton();
        b9.setBounds(205,230,60,70);
        this.add(b9);

        qtde = 1;
        jogador = 1;

        b[0] = b1;
        b[1] = b2;
        b[2] = b3;
        b[3] = b4;
        b[4] = b5;
        b[5] = b6;
        b[6] = b7;
        b[7] = b8;
        b[8] = b9;

        b1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent evt){
                jogada(b1,0,0);
            }
        });
        b2.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent evt){
                jogada(b2,0,1);
            }
        });
        b3.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent evt){
                jogada(b3,0,2);
            }
        });
        b4.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent evt){
                jogada(b4,1,0);
            }
        });
        b5.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent evt){
                jogada(b5,1,1);
            }
        });
        b6.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent evt){
                jogada(b6,1,2);
            }
        });
        b7.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent evt){
                jogada(b7,2,0);
            }
        });
        b8.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent evt){
                jogada(b8,2,1);
            }
        });
        b9.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent evt){
                jogada(b9,2,2);
            }
        });
        nJogo.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent evt){
                for(int i = 0; i < 9; i++){
                        b[i].setEnabled(true);
                        b[i].setText("");
                    }
                    for(int x = 0; x < 3; x++){
                        for(int y = 0; y <3; y++){
                            mat[x][y] = 0;
                        }
                    }
                    jogador = 1;
                    jogador1="";
                    jogador2="";
                    ganhador="";
                }

        });

    }
    public static void main (String [] args){
        new Jogo().setVisible(true);
    }
    public void jogada (JButton b, int x, int y){
        b.setEnabled(false);
        if(jogador == 1){
            mat[x][y] = 1;
            b.setText("X");
            jogador = 2;
            ganhador = jogador1;
            checarjogada(1);
        } else {
            if(jogador == 2){
                mat[x][y] = 2;
                b.setText("O");
                jogador = 1;
                ganhador = jogador2;
                checarjogada(2);
            }
            qtde++;
        }
    }
    public void checarjogada(int x){
        if(vitoria (x) == true){
            JOptionPane.showMessageDialog(null,"Jogador:"+ganhador+"Venceu!","Vitória!",1);
            fimdojogo();
        }
    }
    public boolean vitoria (int x){
        for (int i = 0; i < mat.length; i++){
            if(mat[i][0] == x && mat [i][1] == x && mat [i][2] == x){
                return true;
            }
            if(mat[0][i] == x && mat [1][i] == x && mat [2][i] == x){
                return true;
            }
        }
        if(mat[0][0] == x && mat [1][1] == x && mat [2][2] == x){
            return true;
        }
        if(mat[0][2] == x && mat[1][1] == x && mat [2][0] == x){
            return true;
        }
        return false;
    }
    public void fimdojogo(){
        for(int i = 0; i < 9; i++){
            b[i].setEnabled(false);
        }
    }
}
    
asked by anonymous 03.06.2014 / 04:36

1 answer

10

Do you know what for?

For is a repetition structure, great for iterating through arrays and listas .

for (int i = 0; i <mat.lenght; i++)

This line above means:

For (for) a variable i of type inteiro (int) with initial value equal 0 , repeat all code that is inside the for block (that is, the block that is between the {} after for. Repeat until when? Repeat until i is less than the size of the two-dimensional array mat ; During each repetition increase% with% by plus 1.

As you can see on your for we have three statements separated by i . See:

for ( int i = 0; i < mat.length; i++ ) {
    O que está dentro dessas chaves será repetido.
}

Did you see the ; in the declaration of the for?

So the first part means this:

  • For (for) a variable ; of type i with initial value equal inteiro (int)
  • Repeat all code that is inside the block of 0 (that is, the block that is between the keys for after the.
  • Repeat until when? Repeat until {} is less than the size of the two-dimensional array i ;
  • During each repetition increment% with% by plus 1.
  • Did you understand? I tried not to complicate as much as possible, I hope I have helped.

    Regarding the use of array brackets, either one-dimensional or two-dimensional, think of the following:

    int[] mat = new int [3];
    

    The mat array has a size of 3, and since we have a bracket in its declaration, it is a one-dimensional array of integers. Imagine mat as three empty glasses. You have the cup 0, 1, 2 (Always starting from 0, but do not stop being three cups). If you want to add any number and whatever the glass, you have to say in which glass you want to add the number like this:

    mat[0] = 20; // Isso significa que o primeiro copo, representado pela POSIÇÃO 0, armazenará o elemento (número) 20.
    

    And for the second glass?

    mat[1] = 20; // Isso significa que o segundo copo, representado pela POSIÇÃO 1, armazenará o elemento (número) 20.
    

    And so on.

    Now let's go to the BIDIMENSIONAL array.

    int mat[][] = new int [3][3];//marca a posição que cada jogador jogou
    

    Since we have two brackets, the array is two-dimensional. mat We have an array that can store 3 elements. But each element of this actually contains another array that can then store 3 elements, so i .

    [Posição 0 / Elemento 1], [Posição 1 / Elemento 2], [Posição 0 / Elemento 3]
    

    But instead of being able to store an element inside each position, you have an array inside each position that can hold three elements. Within Position 0, you can store Element 1, Element 2, Element 3 as follows:

    mat[0][0] = 20; Na primeira posição do array que está dentro da posição 0.
    mat[0][1] = 30; Na segunda posição do array que está dentro da posição 0.
    mat[0][2] = 30; Na terceira posição do array que está dentro da posição 0.
    

    Ready we finalized the first array that was within position 0. We added only 3 elements in this array because ... Remember? [3] [3] (This second 3).

        
    03.06.2014 / 04:44