0

我有一个井字游戏,它将 X 和 O 打印到 JFrame 中的按钮上。那些 X 和 O 也放置在作为虚拟板的数组中。当我在虚拟板上呼叫支票赢家时,hasWon 变为 true。但是在 main 方法中,它没有更新。我该如何解决?我已经编辑了代码。现在,程序运行并在最后询问用户是否要再次播放。之后板子重置,但我点击一次后,再次弹出对话框说我赢了。

//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;
}
4

2 回答 2

0

您正在创建一个名为hasWoninsadeGame类的主要方法的新变量

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();
于 2013-10-31T11:29:01.970 回答
0

改变这一行:

boolean hasWon = board.getWin();

hasWon = board.getWin();

然后将静态添加到方法hasWon外声明的变量中main

于 2013-10-31T11:29:29.100 回答