[英]How to add a score record system in tic tac toe Java game
我正在使用 java netbeans 在 gui 中開發井字游戲,需要幫助添加多少個勝利“x”和“o”。 以下代碼是我目前用於該項目的唯一文件。 代碼本身的rest沒有問題,游戲按預期運行,只需要添加一個評分系統。 有什么想法可以實施評分系統嗎?
package game;
/**
*
* @author Naqash
*/
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
public class Game extends javax.swing.JFrame {
public Game() {
initComponents();
addAction();
}
public void winEffect(JButton b1, JButton b2, JButton b3){
b1.setBackground(Color.RED);
b2.setBackground(Color.RED);
b3.setBackground(Color.RED);
b1.setForeground(Color.BLACK);
b2.setForeground(Color.BLACK);
b3.setForeground(Color.BLACK);
String msg = b1.getText() + " Won!";
jLabelMSG.setText(msg);
}
boolean win = false;
public void getTheWinner(){
if(!jButton1.getText().equals("") && jButton1.getText().equals(jButton2.getText()) && jButton1.getText().equals(jButton3.getText())){
winEffect(jButton1, jButton2, jButton3);
win = true;
}
if(!jButton4.getText().equals("") && jButton4.getText().equals(jButton5.getText()) && jButton4.getText().equals(jButton6.getText())){
winEffect(jButton4, jButton5, jButton6);
win = true;
}
if(!jButton7.getText().equals("") && jButton7.getText().equals(jButton8.getText()) && jButton7.getText().equals(jButton9.getText())){
winEffect(jButton7, jButton8, jButton9);
win = true;
}
if(!jButton1.getText().equals("") && jButton1.getText().equals(jButton4.getText()) && jButton1.getText().equals(jButton7.getText())){
winEffect(jButton1, jButton4, jButton7);
win = true;
}
if(!jButton2.getText().equals("") && jButton2.getText().equals(jButton5.getText()) && jButton2.getText().equals(jButton8.getText())){
winEffect(jButton2, jButton5, jButton8);
win = true;
}
if(!jButton3.getText().equals("") && jButton3.getText().equals(jButton6.getText()) && jButton3.getText().equals(jButton9.getText())){
winEffect(jButton3, jButton6, jButton9);
win = true;
}
if(!jButton1.getText().equals("") && jButton1.getText().equals(jButton5.getText()) && jButton1.getText().equals(jButton9.getText())){
winEffect(jButton1, jButton5, jButton9);
win = true;
}
if(!jButton3.getText().equals("") && jButton3.getText().equals(jButton5.getText()) && jButton3.getText().equals(jButton7.getText())){
winEffect(jButton3, jButton5, jButton7);
win = true;
}
else if(allButtonsTextLength() == 9 && win == false){
jLabelMSG.setText("Draw!");
}
}
public int allButtonsTextLength(){
String txt = "";
Component[] comps = jPanel2.getComponents();
for(Component comp : comps){
if(comp instanceof JButton){
JButton button = (JButton)comp;
txt = txt + button.getText();
}
}
return txt.length();
}
int x_or_o = 0;
public ActionListener createAction(JButton button){
ActionListener al = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(button.getText().equals("")){
if((x_or_o % 2) == 0){
button.setText("X");
button.setForeground(Color.red);
jLabelMSG.setText("O Turn");
getTheWinner();
}else{
button.setText("O");
button.setForeground(Color.blue);
jLabelMSG.setText("X Turn");
getTheWinner();
}
x_or_o ++;
}
}
};
return al;
}
public void addAction(){
Component[] comps = jPanel2.getComponents();
for(Component comp : comps){
if(comp instanceof JButton){
JButton button = (JButton)comp;
button.addActionListener(createAction(button));
}
}
}
@SuppressWarnings("unchecked")
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabelMSG = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jButton8 = new javax.swing.JButton();
jButton9 = new javax.swing.JButton();
jButtonReplay = new javax.swing.JButton();
jButtonEnd = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBackground(new java.awt.Color(242, 38, 19));
jLabelMSG.setFont(new java.awt.Font("Tahoma", 1, 24));
jLabelMSG.setForeground(new java.awt.Color(255, 255, 255));
jLabelMSG.setText("Start");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabelMSG, javax.swing.GroupLayout.PREFERRED_SIZE, 235, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabelMSG, javax.swing.GroupLayout.DEFAULT_SIZE, 67, Short.MAX_VALUE)
);
jButton1.setBackground(new java.awt.Color(255, 255, 255));
jButton1.setFont(new java.awt.Font("Tahoma", 1, 30));
jButton2.setBackground(new java.awt.Color(255, 255, 255));
jButton2.setFont(new java.awt.Font("Tahoma", 1, 30));
jButton3.setBackground(new java.awt.Color(255, 255, 255));
jButton3.setFont(new java.awt.Font("Tahoma", 1, 30));
jButton4.setBackground(new java.awt.Color(255, 255, 255));
jButton4.setFont(new java.awt.Font("Tahoma", 1, 30));
jButton5.setBackground(new java.awt.Color(255, 255, 255));
jButton5.setFont(new java.awt.Font("Tahoma", 1, 30));
jButton6.setBackground(new java.awt.Color(255, 255, 255));
jButton6.setFont(new java.awt.Font("Tahoma", 1, 30));
jButton7.setBackground(new java.awt.Color(255, 255, 255));
jButton7.setFont(new java.awt.Font("Tahoma", 1, 30));
jButton8.setBackground(new java.awt.Color(255, 255, 255));
jButton8.setFont(new java.awt.Font("Tahoma", 1, 30));
jButton9.setBackground(new java.awt.Color(255, 255, 255));
jButton9.setFont(new java.awt.Font("Tahoma", 1, 30));
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(0, 0, Short.MAX_VALUE))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)))
);
jButtonReplay.setBackground(new java.awt.Color(255,0,0));
jButtonReplay.setFont(new java.awt.Font("Tahoma", 1, 36));
jButtonReplay.setForeground(new java.awt.Color(255, 255, 255));
jButtonReplay.setText("Restart");
jButtonReplay.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonReplayActionPerformed(evt);
}
});
jButtonEnd.setBackground(new java.awt.Color(255,0,0));
jButtonEnd.setFont(new java.awt.Font("Tahoma", 1, 36));
jButtonEnd.setForeground(new java.awt.Color(255, 255, 255));
jButtonEnd.setText("End Game");
jButtonEnd.addActionListener(new CloseListener());
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE)))
.addContainerGap())
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButtonReplay, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButtonEnd, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButtonReplay, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addComponent(jButtonEnd, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(20, 20, 20))
);
pack();
}
private void jButtonReplayActionPerformed(java.awt.event.ActionEvent evt) {
win = false;
Component[] comps = jPanel2.getComponents();
for(Component comp : comps){
if(comp instanceof JButton){
JButton button = (JButton)comp;
button.setText("");
button.setBackground(Color.WHITE);
jLabelMSG.setText("Start");
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
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(Game.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Game.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Game.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Game.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Game().setVisible(true);
}
});
}
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JButton jButton8;
private javax.swing.JButton jButton9;
private javax.swing.JButton jButtonReplay;
private javax.swing.JLabel jLabelMSG;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JButton jButtonEnd;
void keyPressed(KeyEvent evt) {
throw new UnsupportedOperationException("Not supported yet.");
}
void keyReleased(KeyEvent evt) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
為此,您可以使用兩個整數來保存每個玩家的得分並以某種方式將它們顯示在屏幕上,為此我只是在控制台中打印了得分,但您可以做任何您喜歡的事情。 生成的打印消息是:
Current score:
X = 1 VS O = 0
繼承人的代碼:
//Add the two integers to hold them, in my IDE this shows line 14
int xScore = 0, oScore = 0;
然后在檢測到勝利時增加整數的值,我這樣做是在winEffect()
方法中添加以下代碼行。 我還禁用了調用勝利時的按鈕,這樣即使游戲結束后你也不能繼續添加 Xs 或 Os,這也可以防止再次觸發該方法,這會再次增加分數。 我是我的 IDE 這顯示第 29 行,這是 winEffect 的結尾:
if (b1.getText().equals("X")) xScore++;
else if (b1.getText().equals("O")) oScore++;
for (Component comp : jPanel2.getComponents())
if (comp instanceof JButton)
comp.setEnabled(false);
System.out.printf("Current score:\n X = %d VS O = %d\n\n", xScore, oScore);
之后,您需要在按下重新啟動按鈕時重新啟用按鈕,因此在您的jButtonReplayActionPerformed()
方法中,我使用以下命令重新啟用它們,在我的 IDE 中顯示第 309 行:
for(Component comp : comps){
if(comp instanceof JButton){
JButton button = (JButton)comp;
button.setText("");
button.setBackground(Color.WHITE);
jLabelMSG.setText("Start");
button.setEnabled(true);
}
}
您可以通過JLabel
和支持獲勝者邏輯的方法來執行此操作。
int xWinCount = 0;
int oWinCount = 0;
private void incrementWinner( String winner )
{
if( "X".equals( winner ) )
{
xWinCount += 1;
}
else
{
oWinCount += 1;
}
}
然后在你的winEffect
方法中調用它,如下所示。
incrementWinner( b1.getText() );
jLabelWinner.setText( "X wins : " + xWinCount + " | O wins : " + oWinCount );
jLabelWinner
在initComponent
方法中定義如下
jLabelWinner = new javax.swing.JLabel();
jLabelWinner.setFont( new java.awt.Font( "Tahoma", 1, 18 ) );
jLabelWinner.setText( "Win Status" );
然后,將其添加到布局中。
layout.setHorizontalGroup(
layout.createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING )
.addGroup( layout.createSequentialGroup()
.addContainerGap()
.addGroup( layout.createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING )
.addComponent( jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE )
.addGroup( layout.createSequentialGroup()
.addComponent( jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE )
.addGap( 0, 0, Short.MAX_VALUE ) ) )
.addContainerGap() )
.addGroup( layout.createParallelGroup( javax.swing.GroupLayout.Alignment.TRAILING )
.addComponent( jButtonReplay, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE )
.addComponent( jButtonEnd, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE ) )
.addComponent( jLabelWinner, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE )
);
layout.setVerticalGroup(
layout.createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING )
.addGroup( layout.createSequentialGroup()
.addContainerGap()
.addComponent( jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE )
.addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED )
.addComponent( jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE )
.addGap( 18, 18, 18 )
.addComponent( jButtonReplay, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE )
.addGap( 18, 18, 18 )
.addComponent( jButtonEnd, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE )
.addGap( 18, 18, 18 )
.addComponent( jLabelWinner, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE )
.addGap( 20, 20, 20 ) )
);
樣式和重置由您決定。 這是做你想做的事情的一種方法
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.