gpt4 book ai didi

java - 将编写的代码分为两个类(JAVA)

转载 作者:行者123 更新时间:2023-12-02 00:42:50 24 4
gpt4 key购买 nike

我正在为我的 Java 编写一个 tic-tac-toe 游戏,到目前为止我真的很糟糕,但是让它与他在类里面给我们的例子一起工作。我现在遇到的问题是我意识到希望我们为这个程序至少开设两个类(class)。我不知道他的意思是什么,也不知道我如何将已经放在一起的代码转换为“两个类”。从说明来看,他似乎希望棋盘在一个类中,而游戏在另一类中。

有没有办法将其分成两个类,而无需完全重写整个内容?

/* Standard applet template
*/

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


public class TicTacToeGame implements ActionListener {
/*Instance Variables*/
private JFrame window = new JFrame("Tic-Tac-Toe Game");
private JButton btn1 = new JButton("");
private JButton btn2 = new JButton("");
private JButton btn3 = new JButton("");
private JButton btn4 = new JButton("");
private JButton btn5 = new JButton("");
private JButton btn6 = new JButton("");
private JButton btn7 = new JButton("");
private JButton btn8 = new JButton("");
private JButton btn9 = new JButton("");
private JLabel lblTitle = new JLabel("Tic Tac Toe Game");
private JLabel lblBlank = new JLabel(" ");
private String letter = "";
private int count = 0;
private boolean win = false;

public TicTacToeGame(){
/*Create Window*/
window.setSize(400,300);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridLayout(3,3));

/*Add Buttons To The Window*/
window.add(btn1);
window.add(btn2);
window.add(btn3);
window.add(btn4);
window.add(btn5);
window.add(btn6);
window.add(btn7);
window.add(btn8);
window.add(btn9);

/*Add The Action Listener To The Buttons*/
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
btn5.addActionListener(this);
btn6.addActionListener(this);
btn7.addActionListener(this);
btn8.addActionListener(this);
btn9.addActionListener(this);

/*Make The Window Visible*/
window.setVisible(true);
}

public void actionPerformed(ActionEvent a) {
count++;

/*Calculate Who's Turn It Is*/
if(count == 1 || count == 3 || count == 5 || count == 7 || count == 9){
letter = "<HTML><font color=blue>X</font></HTML>";
} else if(count == 2 || count == 4 || count == 6 || count == 8 || count == 10){
letter = "<HTML><font color=red>O</font></HTML>";
}

/*Display X's or O's on the buttons*/
if(a.getSource() == btn1){
btn1.setText(letter);
btn1.setEnabled(false);
} else if(a.getSource() == btn2){
btn2.setText(letter);
btn2.setEnabled(false);
} else if(a.getSource() == btn3){
btn3.setText(letter);
btn3.setEnabled(false);
} else if(a.getSource() == btn4){
btn4.setText(letter);
btn4.setEnabled(false);
} else if(a.getSource() == btn5){
btn5.setText(letter);
btn5.setEnabled(false);
} else if(a.getSource() == btn6){
btn6.setText(letter);
btn6.setEnabled(false);
} else if(a.getSource() == btn7){
btn7.setText(letter);
btn7.setEnabled(false);
} else if(a.getSource() == btn8){
btn8.setText(letter);
btn8.setEnabled(false);
} else if(a.getSource() == btn9){
btn9.setText(letter);
btn9.setEnabled(false);
}

/*Checks to See Who Won*/
//horizontal win
if( btn1.getText() == btn2.getText() && btn2.getText() == btn3.getText() && btn1.getText() != ""){
win = true;
}
else if(btn4.getText() == btn5.getText() && btn5.getText() == btn6.getText() && btn4.getText() != ""){
win = true;
}
else if(btn7.getText() == btn8.getText() && btn8.getText() == btn9.getText() && btn7.getText() != ""){
win = true;
}

//virticle win
else if(btn1.getText() == btn4.getText() && btn4.getText() == btn7.getText() && btn1.getText() != ""){
win = true;
}
else if(btn2.getText() == btn5.getText() && btn5.getText() == btn8.getText() && btn2.getText() != ""){
win = true;
}
else if(btn3.getText() == btn6.getText() && btn6.getText() == btn9.getText() && btn3.getText() != ""){
win = true;
}

//diagonal wins
else if(btn1.getText() == btn5.getText() && btn5.getText() == btn9.getText() && btn1.getText() != ""){
win = true;
}
else if(btn3.getText() == btn5.getText() && btn5.getText() == btn7.getText() && btn3.getText() != ""){
win = true;
}
else {
win = false;
}

/*Show a dialog if someone wins or the game is tie*/
if(win == true){
JOptionPane.showMessageDialog(null, letter + " WINS!");
} else if(count == 9 && win == false){
JOptionPane.showMessageDialog(null, "Tie Game!");
}
}

public static void main(String[] args){
new TicTacToeGame();
}
}

最佳答案

有多种方法可以拆分此类,但由于它不是很大,因此大多数方法最终都会重写大部分内容。

最干净的方法是将逻辑和 UI 分成(至少)两个单独的类:

  • TicTacToeGame将实现逻辑:它有一个方法来查询轮到谁了,一些方法来查询比赛 field 的状态,一个方法来进入下一步,一个方法来查询游戏的状态(进行中、结束),一种查询谁赢了的方法,也可能是一种注册一些监听器的方法
  • Class TicTacToeUI 只是获取另一个类的实例信息并显示它。单击一个按钮时,它还会调用适当的方法。

最终效果可能会是更多代码(因为两个类之间的交互需要一些代码),但代码会更加简洁,并且游戏类可以独立于 UI 进行测试。

关于java - 将编写的代码分为两个类(JAVA),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5853801/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com