我正在尝试使用挥杆创建一个井字游戏,但是我在检查获胜者方面遇到了麻烦。我设置了三个类,一个用于胜利检查,一个用于游戏本身,一个用于每个按钮。如果有人可以提供帮助,我不知道如何让胜利检查工作。我非常感激。
Tic Tac Toe课程:
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Button extends JButton implements ActionListener{
//The two options aside from empty
ImageIcon X,O;
public String x = "X";
public String o = "O";
public Button()
{
X = new ImageIcon(this.getClass().getResource("x.jpg"));
O = new ImageIcon(this.getClass().getResource("o.jpg"));
this.addActionListener(this);
}
public void actionPerformed(ActionEvent event)
{
switch(TicTacToe.turn)
{
case 0: //turn is 0
setIcon(X);
setText(x);
System.out.println("printing X");
TicTacToe.turn++;
System.out.println("switching turn");
removeActionListener(this);
break;
case 1: //turn is 1 and then reduces back to case 0
setIcon(O);
setText(o);
System.out.println("printing O");
TicTacToe.turn--;
System.out.println("switching turn");
removeActionListener(this);
break;
}
}
}
按钮类:
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Victory extends TicTacToe {
JFrame frame = new JFrame();
public static boolean gameWon = false;
public void checkWin()
{
if (button[0].getText() == button[1].getText()
&& button[1] == button[2] && button[0].getText() != "") //top row
[x][x][x]
{
System.out.println("Victory condition met: top row");
gameWon = true;
Win();
}
else if (button[3].getText() == button[4].getText()
&& button[4].getText() == button[5].getText() && button[3].getText() != "") //mid row [x][x][x]
{
System.out.println("Victory condition met: mid row");
gameWon = true;
Win();
}
else if (button[6].getText() == button[7].getText()
&& button[7].getText() == button[8].getText() && button[6].getText() != "") //bot row [x][x][x]
{
System.out.println("Victory condition met: bot row");
gameWon = true;
Win();
}
else if (button[0].getText() == button[3].getText()
&& button[3].getText() == button[6].getText() && button[0].getText() != "") //first column [x][x][x]
{
System.out.println("Victory condition met: first col");
gameWon = true;
Win();
}
else if (button[1].getText() == button[4].getText()
&& button[4].getText() == button[7].getText() && button[1].getText() != "") //second column [x][x][x]
{
System.out.println("Victory condition met: second col");
gameWon = true;
Win();
}
else if (button[2].getText() == button[5].getText()
&& button[5].getText() == button[8].getText() && button[2].getText() != "") //last column [x][x][x]
{
System.out.println("Victory condition met: last col");
gameWon = true;
Win();
}
else if (button[0].getText() == button[4].getText()
&& button[4].getText() == button[8].getText() && button[0].getText() != "") //diag 1 [x][x][x]
{
System.out.println("Victory condition met: diag 1");
gameWon = true;
Win();
}
else if (button[2].getText() == button[4].getText()
&& button[4].getText() == button[6].getText() && button[2].getText() != "") //diag 2 [x][x][x]
{
System.out.println("Victory condition met: diag 2");
gameWon = true;
Win();
}
else if (turn == 9)
{
System.out.println("Victory conditions not met");
gameWon = false;
Win();
}
}
private void Win()
{
System.out.println("Win Called");
if(button[0].getText() == "O")
{
JOptionPane.showMessageDialog(frame, "O wins!");
System.out.println("O wins");
}
if(button[0].getText() == "X")
{
JOptionPane.showMessageDialog(frame, button[0].getText() + " wins!");
System.out.println("X wins");
}
else if (turn == 9 && gameWon == false)
{
JOptionPane.showMessageDialog(null, "Tie!");
System.out.println("Tie");
}
}
}
胜利等级:
perf report -D
我认为我应该在主要创建TicTacToe之后调用胜利但是这会导致创建两个TicTacToe板。我该怎么办?
答案 0 :(得分:1)
一旦你解决了这个问题,你应该把它带到Code Review exchange,因为它有很多错误。但至于你关于vic.checkWin();
的问题:
你的问题是你只调用一次。用户关闭游戏窗口后。每次移动后都需要调用胜利检查。