how to know how many won X or O in game 3 stripes or Cat?

0

This is my cat code or called 3 in a row. I would like to put a counter on how many wins X and O have, and then pass them to a database, but I have already tried and I can not.

Could someone please help me or at least explain to me?

public class Gato extends javax.swing.JFrame {




    public Gato() {
        initComponents();
    }
    public static String []atic = new String[9];
    String ch = "X";

    int cnt =0;


    public static void IniciarTablero(){



        //Limpia el arreglo para que no tenga ningun dato
        for(int i=0;i<9;i++){
            atic[i]="";
        }
    }




    public  boolean HasWinner(){

        return((atic[0] == atic[1]  && atic[0] == atic[2] && atic[2] != "") ||
            (atic[3] == atic[4]  && atic[3] == atic[5] && atic[3] != "") ||
        (atic[6] == atic[7]  && atic[6] == atic[8] && atic[6] != "") ||
        (atic[0] == atic[3]  && atic[0] == atic[6] && atic[0] != "") ||
        (atic[1] == atic[4]  && atic[1] == atic[7] && atic[1] != "") ||
        (atic[2] == atic[5]  && atic[2] == atic[8] && atic[2] != "") ||
        (atic[0] == atic[4]  && atic[0] == atic[8] && atic[0] != "") ||
        (atic[2] == atic[4]  && atic[2] == atic[6] && atic[2] != ""));
    }
    public void Move(int num){/
        cnt++;

        atic[num]=ch;
    if (HasWinner())

        {
    
asked by Angélica Garcia Pedraza 15.08.2016 в 04:07
source

1 answer

0

First question: Add a counter. To add the counter you need to create 2 whole variables that keep track of the times each one wins:

private int winsX = 0;
private int winsO = 0;

Since you have a message that indicates who is the winner, take advantage of that place to; Besides notifying who won, increase your winning counter.

In your Move method (you should not capitalize the name because it is a method, not a class) you can do the following:

if (HasWinner()) {
    JOptionPane.showMessageDialog(this, "El ganador es:" + ch, "Game Over", JOptionPane.YES_NO_OPTION);
    //inicio de codigo nuevo
    if("X".equals(ch))
        winsX++;
    else
        winsO++;

    jLabel4.setText(String.valueOf(winsX));
    jLabel5.setText(String.valueOf(winsO));
    //fin de codigo nuevo
}

In summary, if the winner is X you increase your counter, otherwise increase your rival O , so that the screen is updated then you place the sentence ...setText(String.valueOf(winsX)) and ...setText(String.valueOf(winsO))

Second question: Save it in a database I do not have a code at hand, but you can do it with a simple connection using JDBC, I leave this example that can help you. Obviously you must have a database created and a table where you will store the information you are interested in.

As general observations I indicate the following:

  • The ideal number of logical operators within a if is 3, you have more than 20. Try to optimize that area of your code as an additional consolidation exercise. But it works! Yes, but we must code as clearly and maintainable as possible.
  • Initial method names with lowercase letters, for example Move must be move
  • The jButton1.disable(); method is deprecated use jButton1.setEnabled(true); to disable components.
  • answered by 15.08.2016 / 22:21
    source