首页 > 解决方案 > java - 如何计算我在java中的程序的每个X和O的获胜次数?

问题描述

因此,我一直在尝试找出一种方法来计算每次有人在我制作的井字游戏 GUI 游戏中获胜的次数。我已经制定了方法来检查井字棋盘中是否存在 X 或 O 的任何获胜组合。我只是对每次调用 checkWinsX 和 checkWinsO 时如何计数感到困惑。我试图制作另一种方法来为 X 或 O 添加一个点,但我有点失败了,哈哈。

public class ticTacToeFrame extends javax.swing.JFrame {
int turn = 0; //Deterines the turn
int winsX; //Tracks total wins for X
int winsO; //Tracks total wins for O

public ticTacToeFrame() {
    initComponents();
}

public void clearAll() {
    ulBUT.setText("");
    umBUT.setText("");
    urBUT.setText("");
    mlBUT.setText("");
    centerBUT.setText("");
    mrBUT.setText("");
    llBUT.setText("");
    lmBUT.setText("");
    lrBUT.setText("");
    statusTF.setText("");
}

////////////////////////////////////////////////////////////////////////
// I tried to make something of this sort multiple times here to count the checkWins, and so far this is the one that would seem to start off the best in my head
////////////////////////////////////////////////////////////////////////
public static int wins() {
    int winsX = 0;
    int winsO = 0;


}

public void checkWinX(int wins) {
    //Top row horizontal check for winning
    if (ulBUT.getText().equals("X") &&  umBUT.getText().equals("X") && urBUT.getText().equals("X")) {
        statusTF.setText("X Wins Horizontally In The Top Row!");
    }
    //Middle row horizontal check for winning
    else if (mlBUT.getText().equals("X") &&  centerBUT.getText().equals("X") && mrBUT.getText().equals("X")) {
        statusTF.setText("X Wins Horizontally In The Middle Row!");
    }
    //Bottom row horizontal check for winning
    else if (llBUT.getText().equals("X") &&  lmBUT.getText().equals("X") && lrBUT.getText().equals("X")) {
        statusTF.setText("X Wins Horizontally In The Bottom Row!");
    }
    //Diagonal check for winning
    else if (ulBUT.getText().equals("X") &&  centerBUT.getText().equals("X") && lrBUT.getText().equals("X")) {
        statusTF.setText("X Wins Diagonnaly!");
    }
    else if (urBUT.getText().equals("X") &&  centerBUT.getText().equals("X") && llBUT.getText().equals("X")) {
        statusTF.setText("X Wins Diagonnaly!");
    }
}

public void checkWinO() {
    //Top row horizontal check for winning
    if (ulBUT.getText().equals("O") &&  umBUT.getText().equals("O") && urBUT.getText().equals("O")) {
        statusTF.setText("O Wins Horizontally In The Top Row!");
    }
    //Middle row horizontal check for winning
    else if (mlBUT.getText().equals("O") &&  centerBUT.getText().equals("O") && mrBUT.getText().equals("O")) {
        statusTF.setText("O Wins Horizontally In The Middle Row!");
    }
    //Bottom row horizontal check for winning
    else if (llBUT.getText().equals("O") &&  lmBUT.getText().equals("O") && lrBUT.getText().equals("O")) {
        statusTF.setText("O Wins Horizontally In The Bottom Row!");
    }
    //Diagonal check for winning
    else if (ulBUT.getText().equals("O") &&  centerBUT.getText().equals("O") && lrBUT.getText().equals("O")) {
        statusTF.setText("O Wins Diagonnaly!");
    }
    else if (urBUT.getText().equals("O") &&  centerBUT.getText().equals("O") && llBUT.getText().equals("O")) {
        statusTF.setText("O Wins Diagonnaly!");
    }
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    ulPAN = new javax.swing.JPanel();
    ulBUT = new javax.swing.JButton();
    umBUT = new javax.swing.JButton();
    urBUT = new javax.swing.JButton();
    mlBUT = new javax.swing.JButton();
    centerBUT = new javax.swing.JButton();
    mrBUT = new javax.swing.JButton();
    llBUT = new javax.swing.JButton();
    lmBUT = new javax.swing.JButton();
    lrBUT = new javax.swing.JButton();
    statusTF = new javax.swing.JTextField();
    oWinsTF = new javax.swing.JTextField();
    xWinsTF = new javax.swing.JTextField();
    resetGameBUT = new javax.swing.JButton();
    turnTF = new javax.swing.JTextField();
    tiesTF1 = new javax.swing.JTextField();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("Tic Tac Toe | John Gironda");
    setBackground(new java.awt.Color(87, 87, 87));

    ulPAN.setLayout(new java.awt.GridLayout(3, 3));

    ulBUT.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            ulBUTActionPerformed(evt);
        }
    });
    ulPAN.add(ulBUT);

    umBUT.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            umBUTActionPerformed(evt);
        }
    });
    ulPAN.add(umBUT);

    urBUT.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            urBUTActionPerformed(evt);
        }
    });
    ulPAN.add(urBUT);

    mlBUT.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            mlBUTActionPerformed(evt);
        }
    });
    ulPAN.add(mlBUT);

    centerBUT.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            centerBUTActionPerformed(evt);
        }
    });
    ulPAN.add(centerBUT);

    mrBUT.setToolTipText("");
    mrBUT.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            mrBUTActionPerformed(evt);
        }
    });
    ulPAN.add(mrBUT);

    llBUT.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            llBUTActionPerformed(evt);
        }
    });
    ulPAN.add(llBUT);

    lmBUT.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            lmBUTActionPerformed(evt);
        }
    });
    ulPAN.add(lmBUT);

    lrBUT.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            lrBUTActionPerformed(evt);
        }
    });
    ulPAN.add(lrBUT);

    statusTF.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            statusTFActionPerformed(evt);
        }
    });

    oWinsTF.setText("Total O Wins: 0");
    oWinsTF.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            oWinsTFActionPerformed(evt);
        }
    });

    xWinsTF.setText("Total X Wins: 0");
    xWinsTF.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            xWinsTFActionPerformed(evt);
        }
    });

    resetGameBUT.setText("RESET");
    resetGameBUT.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            resetGameBUTActionPerformed(evt);
        }
    });

    turnTF.setText("Turn: X");
    turnTF.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            turnTFActionPerformed(evt);
        }
    });

    tiesTF1.setText("Total Ties: 0");
    tiesTF1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            tiesTF1ActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap(16, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                .addComponent(ulPAN, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(statusTF)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(xWinsTF, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createSequentialGroup()
                    .addComponent(turnTF, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(tiesTF1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(oWinsTF, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE))))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(resetGameBUT, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(16, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(16, 16, 16)
                    .addComponent(ulPAN, javax.swing.GroupLayout.PREFERRED_SIZE, 350, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createSequentialGroup()
                    .addGap(154, 154, 154)
                    .addComponent(resetGameBUT, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(xWinsTF, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(statusTF, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(oWinsTF, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(tiesTF1, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createSequentialGroup()
                    .addGap(30, 30, 30)
                    .addComponent(turnTF, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addContainerGap(16, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>                        

private void ulBUTActionPerformed(java.awt.event.ActionEvent evt) {                                      
    if (ulBUT.getText().equals("")) {
        if (turn % 2 == 0) {
        ulBUT.setText("X");
        } else {
            ulBUT.setText("O");
        }
        turn++;
        checkWinX();
        checkWinO();
    }
}                                     

private void umBUTActionPerformed(java.awt.event.ActionEvent evt) {                                      
    if (umBUT.getText().equals("")) {
        if (turn % 2 == 0) {
        umBUT.setText("X");
        } else {
            umBUT.setText("O");
        }
        turn++;
        checkWinX();
        checkWinO();
    }
}                                     

private void urBUTActionPerformed(java.awt.event.ActionEvent evt) {                                      
    if (urBUT.getText().equals("")) {
        if (turn % 2 == 0) {
        urBUT.setText("X");
        } else {
            urBUT.setText("O");
        }
        turn++;
        checkWinX();
        checkWinO();
    }
}                                     

private void mlBUTActionPerformed(java.awt.event.ActionEvent evt) {                                      
    if (mlBUT.getText().equals("")) {
        if (turn % 2 == 0) {
        mlBUT.setText("X");
        } else {
            mlBUT.setText("O");
        }
        turn++;
        checkWinX();
        checkWinO();
    }
}                                     

private void centerBUTActionPerformed(java.awt.event.ActionEvent evt) {                                          
    if (centerBUT.getText().equals("")) {
        if (turn % 2 == 0) {
        centerBUT.setText("X");
        } else {
            centerBUT.setText("O");
        }
        turn++;
        checkWinX();
        checkWinO();
    }
}                                         

private void mrBUTActionPerformed(java.awt.event.ActionEvent evt) {                                      
    if (mrBUT.getText().equals("")) {
        if (turn % 2 == 0) {
        mrBUT.setText("X");
        } else {
            mrBUT.setText("O");
        }
        turn++;
        checkWinX();
        checkWinO();
    }
}                                     

private void llBUTActionPerformed(java.awt.event.ActionEvent evt) {                                      
    if (llBUT.getText().equals("")) {
        if (turn % 2 == 0) {
        llBUT.setText("X");
        } else {
            llBUT.setText("O");
        }
        turn++;
        checkWinX();
        checkWinO();
    }
}                                     

private void lmBUTActionPerformed(java.awt.event.ActionEvent evt) {                                      
    if (lmBUT.getText().equals("")) {
        if (turn % 2 == 0) {
        lmBUT.setText("X");
        } else {
            lmBUT.setText("O");
        }
        turn++;
        checkWinX();
        checkWinO();
    }
}                                     

private void lrBUTActionPerformed(java.awt.event.ActionEvent evt) {                                      
    if (lrBUT.getText().equals("")) {
        if (turn % 2 == 0) {
        lrBUT.setText("X");
        } else {
            lrBUT.setText("O");
        }
        turn++;
        checkWinX();
        checkWinO();
    }
}                                     

private void resetGameBUTActionPerformed(java.awt.event.ActionEvent evt) {                                             
    clearAll(); 
}                                            

private void statusTFActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
}                                        

private void xWinsTFActionPerformed(java.awt.event.ActionEvent evt) {                                        
    // TODO add your handling code here:
}                                       

private void oWinsTFActionPerformed(java.awt.event.ActionEvent evt) {                                        
    // TODO add your handling code here:
}                                       

private void turnTFActionPerformed(java.awt.event.ActionEvent evt) {                                       
    // TODO add your handling code here:
}                                      

private void tiesTF1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
    // TODO add your handling code here:
}                                       

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(ticTacToeFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(ticTacToeFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(ticTacToeFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(ticTacToeFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new ticTacToeFrame().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton centerBUT;
private javax.swing.JButton llBUT;
private javax.swing.JButton lmBUT;
private javax.swing.JButton lrBUT;
private javax.swing.JButton mlBUT;
private javax.swing.JButton mrBUT;
private javax.swing.JTextField oWinsTF;
private javax.swing.JButton resetGameBUT;
private javax.swing.JTextField statusTF;
private javax.swing.JTextField tiesTF1;
private javax.swing.JTextField turnTF;
private javax.swing.JButton ulBUT;
private javax.swing.JPanel ulPAN;
private javax.swing.JButton umBUT;
private javax.swing.JButton urBUT;
private javax.swing.JTextField xWinsTF;
// End of variables declaration                   

}

标签: javatic-tac-toe

解决方案


在您的 wins() 方法中,您可以传入一个指示 X 或 O 的参数并增加相应的计数器变量。

例如:

public int win(int player) {
    if(player == 0) {
        winsX++;
    }
    else {
        winsY++;
    }
}

然后在每次检查并发现玩家获胜时调用它。


推荐阅读