我有一个tic tac toe游戏,它将X和O打印到JFrame中的按钮。此外,X和O也被放置在作为虚拟板的阵列中。当我在虚拟主板上调用支票获胜者时,hasWon会变为true。但在主要方法中,尚未更新。我该如何解决这个问题?我编辑了代码。现在,程序运行,最后它询问用户是否要再次播放。在那之后电路板重置,但是在我点击一次之后,再次弹出对话框,说我已经赢了。
//TicTacToe Class
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.util.Scanner;
public class TicTacToe implements ActionListener
{
JFrame window = new JFrame("Tic Tac Toe");
JButton button[] = new JButton[10];
int count = 0;
public static String letter;
JPanel board = new JPanel();
JMenuBar jmb;
JMenuBar status;
JMenu game;
JMenuItem nGame, notes, exit;
boolean won = false;
public String[] vBoard = new String[10];
String letterWin;
boolean keepPlaying = true;
JLabel label1;
Players player;
String winner;
public TicTacToe()
{
Scanner in = new Scanner(System.in);
System.out.println("Player 1 Enter your name:");
String name1 = in.next();
System.out.println("Player 2 Enter your name:");
String name2 = in.next();
player = new Players(name1, name2);
window.setSize(500, 500);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new BorderLayout());
jmb = new JMenuBar();
status = new JMenuBar();
window.setJMenuBar(jmb);
game = new JMenu("Game Options");
jmb.add(game);
JMenuItem notes = new JMenuItem("Instructions");
notes.addActionListener(this);
game.add(notes);
game.addSeparator();
JMenuItem exit = new JMenuItem("Exit");
exit.addActionListener(this);
game.add(exit);
game.addSeparator();
board.setLayout(new GridLayout(3, 3));
board.setBackground(Color.black);
for (int k = 0; k < 9; k++)
{
button[k] = new JButton();
window.add(button[k]);
button[k].setBackground(Color.blue);
button[k].addActionListener(this);
board.add(button[k]);
}
window.add(board, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e)
{
{ count++;
if (count % 2 == 1)
letter = "X";
else letter = "O";
for ( int i = 0; i <= 8; i++)
{
if (e.getSource() == button[i])
{ if(letter.equals("X"))
button[i].setBackground(Color.red);
else if(letter.equals("O"))
button[i].setBackground(Color.white);
button[i].setText(letter);
button[i].setForeground(Color.black);
button[i].setEnabled(false);
vBoard[i] = letter;
{
if(vBoard[0].equals(vBoard[1]) && vBoard[1].equals(vBoard[2]) && (vBoard[2] != ""))
won = true;
else if(vBoard[3].equals(vBoard[4]) && vBoard[4].equals(vBoard[5]) && (vBoard[5] != ""))
won = true;
else if(vBoard[6].equals(vBoard[7]) && vBoard[7].equals(vBoard[8]) && (vBoard[8] != ""))
won = true;
else if(vBoard[0].equals(vBoard[3]) && vBoard[3].equals(vBoard[6]) && (vBoard[6] != ""))
won = true;
else if(vBoard[1].equals(vBoard[4]) && vBoard[4].equals(vBoard[7]) && (vBoard[7] != ""))
won = true;
else if(vBoard[2].equals(vBoard[5]) && vBoard[5].equals(vBoard[6]) && (vBoard[6] != ""))
won = true;
else if(vBoard[0].equals(vBoard[4]) && vBoard[4].equals(vBoard[8]) && (vBoard[8] != ""))
won = true;
else if(vBoard[2].equals(vBoard[4]) && vBoard[4].equals(vBoard[6]) && (vBoard[6] != ""))
won = true;
if(won == true && count <=9){
keepPlaying = false;
if(letter == "X")
winner = player.getPlayer1();
else winner = player.getPlayer2();
}
else if(won == false && count == 9)
winner = "No One";
{ int option = JOptionPane.showConfirmDialog(null, winner + " has won!" + "\nDo you want to start a new game?",
"Winner", JOptionPane.YES_NO_OPTION);
if(option == JOptionPane.YES_OPTION)
{
keepPlaying = true;
count = 0;
won = false;
for(int l = 0; l <=9; l++)
{ button[l].setEnabled(true);
button[l].setText("");
button[l].setBackground(Color.blue);
}}
else if(option == JOptionPane.NO_OPTION)
System.exit(0);
}
}
}
else if (e.getSource().equals("Exit"))
{ int option = JOptionPane.showConfirmDialog(null, "Do you want to exit?",
"Exit Game", JOptionPane.OK_CANCEL_OPTION);
if(option == JOptionPane.OK_OPTION)
System.exit(0);
else if(option == JOptionPane.CANCEL_OPTION)
{
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.add(board, BorderLayout.CENTER);
window.setJMenuBar(jmb);
}
}
if(keepPlaying == false)
for(int k = 0; k <=8; k++)
button[k].setEnabled(false);
}
}
}
}
//Players Class
public class Players
{
public Players(String name1, String name2)
{
player1 = name1;
player2 = name2;
wins = 0;
wins2 = 0;
}
public String getPlayer1()
{
return player1;
}
public String getPlayer2()
{
return player2;
}
public void addWin1()
{
wins++;
}
public void addWin2()
{
wins2++;
}
public int getP1Wins()
{
return wins;
}
public int getP2Wins()
{
return wins2;
}
public String player1, player2;
public static int wins, wins2;
}
//Main Class
import javax.swing.JOptionPane;
import java.util.Scanner;
public class Game extends TicTacToe
{
public static void main(String[] args)
{
TicTacToe board = new TicTacToe();
}
public static String name1, name2;
}
答案 0 :(得分:0)
您正在hasWon
类的主要方法
Game
的新变量
public class Game extends TicTacToe
{
public static void main(String[] args)
{
//.....
//Here you are creating new method level variable with same name as `hasWon`
boolean hasWon = board.getWin();
//.....
}
public static String name1, name2;
// This variable is never updated
boolean hasWon;
}
您可能想要
Game game = new Game();
game.hasWon = board.getWin();
答案 1 :(得分:0)
更改此行:
boolean hasWon = board.getWin();
到
hasWon = board.getWin();
然后将静态添加到hasWon
方法
main
变量