所以我有以下代码,我正在努力浓缩我创造的if else海。我不知道如果有可能帮助我这个:p。如果我需要提供更多信息,请询问。
public class Pvp extends JPanel implements ActionListener {
JButton[] gridButtons;
int count = 0;
int sign = 0;
char[] grid;
char winner;
public Pvp() {
setBackground(new Color(255, 0, 51));
setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
setLayout(new BorderLayout(0, 0));
JLabel lblPvpGamemode = new JLabel("PvP Gamemode");
lblPvpGamemode.setHorizontalAlignment(SwingConstants.CENTER);
lblPvpGamemode.setFont(new Font("Timber", Font.PLAIN, 50));
add(lblPvpGamemode, BorderLayout.NORTH);
JPanel gamePanel = new JPanel();
gamePanel.setBackground(new Color(255, 102, 51));
add(gamePanel, BorderLayout.CENTER);
gamePanel.setLayout(new GridLayout(3, 3, 0, 0));
gamePanel.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
gridButtons = new JButton[9];
grid = new char[9];
for (int cntr = 0; cntr < 9; cntr++) {
gridButtons[cntr] = new JButton();
gamePanel.add(gridButtons[cntr]);
gridButtons[cntr].setEnabled(true);
gridButtons[cntr].addActionListener(this);
gridButtons[cntr].setBackground(Color.BLACK);
grid[cntr] = ' ';
}
}
public void actionPerformed(ActionEvent e) {
count++;
for (int cntr = 0; cntr <= 8; cntr++) {
if (gridButtons[cntr] == e.getSource()) {
if (sign % 2 == 0) {
gridButtons[cntr]
.setFont(new Font("Timber", Font.PLAIN, 80));
gridButtons[cntr].setText("X");
gridButtons[cntr].setEnabled(false);
gridButtons[cntr].setForeground(new Color(255, 102, 51));
grid[cntr] = 'X';
} else {
gridButtons[cntr]
.setFont(new Font("Timber", Font.PLAIN, 80));
gridButtons[cntr].setText("O");
gridButtons[cntr].setEnabled(false);
gridButtons[cntr].setForeground(Color.BLACK);
grid[cntr] = 'O';
}
if (grid[0] == 'X' && grid[0] == grid[1] && grid[1] == grid[2]) {
int yes;
yes = JOptionPane
.showOptionDialog(
null,
"The game is over! Xs have won! Do you want to play again?",
"Game over", JOptionPane.YES_NO_OPTION,
JOptionPane.YES_NO_OPTION, null, null, null);
if (yes == JOptionPane.YES_OPTION) {
for (int cntr2 = 0; cntr2 < 9; cntr2++) {
gridButtons[cntr2].setText("");
gridButtons[cntr2].setEnabled(true);
grid[cntr2] = ' ';
}
count = 0;
sign = 0;
FormPanel formPanel = new FormPanel();
formPanel.firePvp();
return;
}
} else if (grid[0] == 'O' && grid[0] == grid[1]
&& grid[1] == grid[2]) {
int yes;
yes = JOptionPane
.showOptionDialog(
null,
"The game is over! Os have won! Do you want to play again?",
"Game over", JOptionPane.YES_NO_OPTION,
JOptionPane.YES_NO_OPTION, null, null, null);
if (yes == JOptionPane.YES_OPTION) {
for (int cntr2 = 0; cntr2 < 9; cntr2++) {
gridButtons[cntr2].setText("");
gridButtons[cntr2].setEnabled(true);
grid[cntr2] = ' ';
}
count = 0;
sign = 0;
FormPanel formPanel = new FormPanel();
formPanel.firePvp();
return;
}
} else if (grid[0] == 'X' && grid[0] == grid[3]
&& grid[3] == grid[6]) {
winner = 'X';
int yes;
yes = JOptionPane
.showOptionDialog(
null,
"The game is over! Os have won! Do you want to play again?",
"Game over", JOptionPane.YES_NO_OPTION,
JOptionPane.YES_NO_OPTION, null, null, null);
if (yes == JOptionPane.YES_OPTION) {
for (int cntr2 = 0; cntr2 < 9; cntr2++) {
gridButtons[cntr2].setText("");
gridButtons[cntr2].setEnabled(true);
grid[cntr2] = ' ';
}
count = 0;
sign = 0;
FormPanel formPanel = new FormPanel();
formPanel.firePvp();
return;
}
}
else if (grid[0] == 'O' && grid[0] == grid[3]
&& grid[3] == grid[6]) {
winner = 'O';
int yes;
yes = JOptionPane
.showOptionDialog(
null,
"The game is over! Os have won! Do you want to play again?",
"Game over", JOptionPane.YES_NO_OPTION,
JOptionPane.YES_NO_OPTION, null, null, null);
if (yes == JOptionPane.YES_OPTION) {
for (int cntr2 = 0; cntr2 < 9; cntr2++) {
gridButtons[cntr2].setText("");
gridButtons[cntr2].setEnabled(true);
grid[cntr2] = ' ';
}
count = 0;
sign = 0;
FormPanel formPanel = new FormPanel();
formPanel.firePvp();
return;
}
}
else if (grid[6] == 'X' && grid[6] == grid[7]
&& grid[7] == grid[8]) {
winner = 'X';
int yes;
yes = JOptionPane
.showOptionDialog(
null,
"The game is over! Xs have won! Do you want to play again?",
"Game over", JOptionPane.YES_NO_OPTION,
JOptionPane.YES_NO_OPTION, null, null, null);
if (yes == JOptionPane.YES_OPTION) {
for (int cntr2 = 0; cntr2 < 9; cntr2++) {
gridButtons[cntr2].setText("");
gridButtons[cntr2].setEnabled(true);
grid[cntr2] = ' ';
}
count = 0;
sign = 0;
FormPanel formPanel = new FormPanel();
formPanel.firePvp();
return;
}
} else if (grid[6] == 'O' && grid[6] == grid[7]
&& grid[7] == grid[8]) {
winner = 'O';
int yes;
yes = JOptionPane
.showOptionDialog(
null,
"The game is over! Os have won! Do you want to play again?",
"Game over", JOptionPane.YES_NO_OPTION,
JOptionPane.YES_NO_OPTION, null, null, null);
if (yes == JOptionPane.YES_OPTION) {
for (int cntr2 = 0; cntr2 < 9; cntr2++) {
gridButtons[cntr2].setText("");
gridButtons[cntr2].setEnabled(true);
grid[cntr2] = ' ';
}
count = 0;
sign = 0;
FormPanel formPanel = new FormPanel();
formPanel.firePvp();
return;
}
} else if (grid[2] == 'X' && grid[2] == grid[5]
&& grid[5] == grid[8]) {
winner = 'X';
int yes;
yes = JOptionPane
.showOptionDialog(
null,
"The game is over! Xs have won! Do you want to play again?",
"Game over", JOptionPane.YES_NO_OPTION,
JOptionPane.YES_NO_OPTION, null, null, null);
if (yes == JOptionPane.YES_OPTION) {
for (int cntr2 = 0; cntr2 < 9; cntr2++) {
gridButtons[cntr2].setText("");
gridButtons[cntr2].setEnabled(true);
grid[cntr2] = ' ';
}
count = 0;
sign = 0;
FormPanel formPanel = new FormPanel();
formPanel.firePvp();
return;
}
} else if (grid[2] == 'O' && grid[2] == grid[5]
&& grid[5] == grid[8]) {
winner = 'O';
int yes;
yes = JOptionPane
.showOptionDialog(
null,
"The game is over! Os have won! Do you want to play again?",
"Game over", JOptionPane.YES_NO_OPTION,
JOptionPane.YES_NO_OPTION, null, null, null);
if (yes == JOptionPane.YES_OPTION) {
for (int cntr2 = 0; cntr2 < 9; cntr2++) {
gridButtons[cntr2].setText("");
gridButtons[cntr2].setEnabled(true);
grid[cntr2] = ' ';
}
count = 0;
sign = 0;
FormPanel formPanel = new FormPanel();
formPanel.firePvp();
return;
}
} else if (grid[3] == 'X' && grid[3] == grid[4]
&& grid[4] == grid[5]) {
winner = 'X';
int yes;
yes = JOptionPane
.showOptionDialog(
null,
"The game is over! Xs have won! Do you want to play again?",
"Game over", JOptionPane.YES_NO_OPTION,
JOptionPane.YES_NO_OPTION, null, null, null);
if (yes == JOptionPane.YES_OPTION) {
for (int cntr2 = 0; cntr2 < 9; cntr2++) {
gridButtons[cntr2].setText("");
gridButtons[cntr2].setEnabled(true);
grid[cntr2] = ' ';
}
count = 0;
sign = 0;
FormPanel formPanel = new FormPanel();
formPanel.firePvp();
return;
}
} else if (grid[3] == 'O' && grid[3] == grid[4]
&& grid[4] == grid[5]) {
winner = 'O';
int yes;
yes = JOptionPane
.showOptionDialog(
null,
"The game is over! Os have won! Do you want to play again?",
"Game over", JOptionPane.YES_NO_OPTION,
JOptionPane.YES_NO_OPTION, null, null, null);
if (yes == JOptionPane.YES_OPTION) {
for (int cntr2 = 0; cntr2 < 9; cntr2++) {
gridButtons[cntr2].setText("");
gridButtons[cntr2].setEnabled(true);
grid[cntr2] = ' ';
}
count = 0;
sign = 0;
FormPanel formPanel = new FormPanel();
formPanel.firePvp();
return;
}
} else if (grid[0] == 'X' && grid[0] == grid[4]
&& grid[4] == grid[8]) {
winner = 'X';
int yes;
yes = JOptionPane
.showOptionDialog(
null,
"The game is over! Xs have won! Do you want to play again?",
"Game over", JOptionPane.YES_NO_OPTION,
JOptionPane.YES_NO_OPTION, null, null, null);
if (yes == JOptionPane.YES_OPTION) {
for (int cntr2 = 0; cntr2 < 9; cntr2++) {
gridButtons[cntr2].setText("");
gridButtons[cntr2].setEnabled(true);
grid[cntr2] = ' ';
}
count = 0;
sign = 0;
FormPanel formPanel = new FormPanel();
formPanel.firePvp();
return;
}
} else if (grid[2] == 'O' && grid[0] == grid[4]
&& grid[4] == grid[8]) {
winner = 'O';
int yes;
yes = JOptionPane
.showOptionDialog(
null,
"The game is over! Os have won! Do you want to play again?",
"Game over", JOptionPane.YES_NO_OPTION,
JOptionPane.YES_NO_OPTION, null, null, null);
if (yes == JOptionPane.YES_OPTION) {
for (int cntr2 = 0; cntr2 < 9; cntr2++) {
gridButtons[cntr2].setText("");
gridButtons[cntr2].setEnabled(true);
grid[cntr2] = ' ';
}
count = 0;
sign = 0;
FormPanel formPanel = new FormPanel();
formPanel.firePvp();
return;
}
} else if (grid[2] == 'X' && grid[2] == grid[4]
&& grid[4] == grid[6]) {
winner = 'X';
int yes;
yes = JOptionPane
.showOptionDialog(
null,
"The game is over! Xs have won! Do you want to play again?",
"Game over", JOptionPane.YES_NO_OPTION,
JOptionPane.YES_NO_OPTION, null, null, null);
if (yes == JOptionPane.YES_OPTION) {
for (int cntr2 = 0; cntr2 < 9; cntr2++) {
gridButtons[cntr2].setText("");
gridButtons[cntr2].setEnabled(true);
grid[cntr2] = ' ';
}
count = 0;
sign = 0;
FormPanel formPanel = new FormPanel();
formPanel.firePvp();
return;
}
} else if (grid[6] == 'O' && grid[2] == grid[4]
&& grid[4] == grid[6]) {
winner = 'O';
int yes;
yes = JOptionPane
.showOptionDialog(
null,
"The game is over! Os have won! Do you want to play again?",
"Game over", JOptionPane.YES_NO_OPTION,
JOptionPane.YES_NO_OPTION, null, null, null);
if (yes == JOptionPane.YES_OPTION) {
for (int cntr2 = 0; cntr2 < 9; cntr2++) {
gridButtons[cntr2].setText("");
gridButtons[cntr2].setEnabled(true);
grid[cntr2] = ' ';
}
count = 0;
sign = 0;
FormPanel formPanel = new FormPanel();
formPanel.firePvp();
return;
}
}else if (grid[1] == 'X' && grid[1] == grid[4]
&& grid[4] == grid[7]) {
winner = 'X';
int yes;
yes = JOptionPane
.showOptionDialog(
null,
"The game is over! Xs have won! Do you want to play again?",
"Game over", JOptionPane.YES_NO_OPTION,
JOptionPane.YES_NO_OPTION, null, null, null);
if (yes == JOptionPane.YES_OPTION) {
for (int cntr2 = 0; cntr2 < 9; cntr2++) {
gridButtons[cntr2].setText("");
gridButtons[cntr2].setEnabled(true);
grid[cntr2] = ' ';
}
count = 0;
sign = 0;
FormPanel formPanel = new FormPanel();
formPanel.firePvp();
return;
}
} else if (grid[1] == 'O' && grid[1] == grid[4]
&& grid[4] == grid[7]) {
winner = 'O';
int yes;
yes = JOptionPane
.showOptionDialog(
null,
"The game is over! Os have won! Do you want to play again?",
"Game over", JOptionPane.YES_NO_OPTION,
JOptionPane.YES_NO_OPTION, null, null, null);
if (yes == JOptionPane.YES_OPTION) {
for (int cntr2 = 0; cntr2 < 9; cntr2++) {
gridButtons[cntr2].setText("");
gridButtons[cntr2].setEnabled(true);
grid[cntr2] = ' ';
}
count = 0;
sign = 0;
FormPanel formPanel = new FormPanel();
formPanel.firePvp();
return;
}
}
}
}
if (count >= 9) {
int yes;
yes = JOptionPane.showOptionDialog(null,
"The game is over! Do you want to play again?",
"Game over", JOptionPane.YES_NO_OPTION,
JOptionPane.YES_NO_OPTION, null, null, null);
if (yes == JOptionPane.YES_OPTION) {
for (int cntr2 = 0; cntr2 < 9; cntr2++) {
gridButtons[cntr2].setText(" ");
gridButtons[cntr2].setEnabled(true);
grid[cntr2] = ' ';
}
count = 0;
sign = 0;
FormPanel formPanel = new FormPanel();
formPanel.firePvp();
return;
}
}
sign++;
}
答案 0 :(得分:0)
我同意azurefrog。在您拥有重复代码的任何地方,尝试将该代码放入要调用的函数/方法中。您的代码将更短且可读。放入方法
的代码示例if (yes == JOptionPane.YES_OPTION) {
for (int cntr2 = 0; cntr2 < 9; cntr2++) {
gridButtons[cntr2].setText("");
gridButtons[cntr2].setEnabled(true);
grid[cntr2] = ' ';
}
count = 0;
sign = 0;
FormPanel formPanel = new FormPanel();
formPanel.firePvp();
return;
}
在2个或更多地方看到这个确切的代码。