在Noughts and Crosses项目中实现SwingWorker

时间:2015-03-02 18:02:27

标签: java swing oop swingworker tic-tac-toe

我一直致力于一个项目,该项目是创建一个Noughts和Crosses游戏。我已经为游戏奠定了基础,我目前正在进一步开发它。但是,当我尝试运行程序时,我的GUI锁定了。我已经开始实施SwingWorker作为解决方案,但我很难让它实现所需的功能。

课堂游戏:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.*;
import java.util.*;
import javax.swing.*;
import javax.swing.text.DefaultCaret;
import java.lang.*;
import javax.swing.SwingWorker;




 @SuppressWarnings("serial")
 public class Game extends JFrame 


{

   public static boolean State = false , PlayerGame = false , AIGame = false;

   public static boolean victory = false;

   public static boolean Full = false, Draw = false , isEmpty = true;  

   public static int turn = 0;

   private static boolean FirstTurn = false;


   private int PlayerCounts = 0 , ComputerCounts = 0 , DrawCounts = 0 ;

   private Random sideRand = new Random();
   private int min1 = 1;
   private int max1 = 2;

   private Random FirstTurnRand = new Random();
   private int min2 = 1;
   private int max2 = 2;


   public static boolean playerO = false;
   public static boolean computerO = false;

   public static boolean playerX = false;
   public static boolean computerX = false;



    private javax.swing.JButton Button1;
    private javax.swing.JButton Button2;
    private javax.swing.JButton Button3;
    private javax.swing.JButton Button4;
    private javax.swing.JButton Button5;
    private javax.swing.JButton Button6;
    private javax.swing.JButton Button7;
    private javax.swing.JButton Button8;
    private javax.swing.JButton Button9;
    private javax.swing.JButton Exit;
    private javax.swing.JButton Reset;
    private javax.swing.JButton Start;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea1;
    public static javax.swing.JTextArea jTextArea2;



    public static JButton[][] Board = new JButton[3][3];
    {                                           

        Board[0][0] = Button1;
        Board[0][1] = Button2;
        Board[0][2] = Button3;
        Board[1][0] = Button4;
        Board[1][1] = Button5;
        Board[1][2] = Button6;
        Board[2][0] = Button7;
        Board[2][1] = Button8;
        Board[2][2] = Button9;

    }


    public static String ComputerMark ;
    public static String PlayerMark ;

    public static String Nought = "O";
    public static String Cross = "X";



    public void SideAssigner()
    {

        int ChooseSide = sideRand.nextInt((max1 - min1) + 1 ) + min1;
        int ChooseFirstTurn = FirstTurnRand.nextInt((max2 - min2) + 1 ) + min2;

        if ( ChooseSide == 1)
        {
            // Player goes as Crosses
            // Computer goes as Noughts
            playerX = true;

            PlayerMark = Cross;

            computerO = true;

            Nought = ComputerMark;

            FirstTurn = true;

            jTextArea2.append("You are playing as Crosses and Computer is playing as Noughts\n");   
        }

        if ( ChooseSide == 2)
        {
            // Player goes as Noughts 
            // Computer goes as Crosses
            playerO = true;

            PlayerMark = Nought;

            computerX = true;

            Cross = ComputerMark;

            FirstTurn = true;

            jTextArea2.append("You are playing as Noughts and Computer is playing as Crosses\n");
        }


        DialogBox dialogbox = new DialogBox(Game.this);


    }







    public void start()
    {
        SwingWorker<Boolean, Integer> worker = new SwingWorker<Boolean, Integer>() 
        {


            protected Boolean doInBackground() throws Exception {

                while ( State = true )
             {

                 WinValidator();

             }
                return rootPaneCheckingEnabled;

            }
    };











}   




        public void StartGame()
        {


         SideAssigner();
         State = true;

         if ( AIGame == true )
         {
             Computer.AI();

         }

         while ( State = true )
         {

             //WinValidator();

         }

         start();

    }




    public void WinValidator()
    {
        if ( isEmpty == false)
        {   
            CheckWinAndDraw.checkWinandDraw();

        }

        if ( victory == true && PlayerGame == true )
        {
            jTextArea2.append("Player won!");
            PlayerCounts++;
            State = false;
        }


        if ( victory == true && AIGame == true)
        {
            jTextArea2.append("Computer won!");
            ComputerCounts++;
            State = false;

        }

        if ( Draw == true && Full == true)
        {
            jTextArea2.append("Oh no! It is a tie!");
            DrawCounts++;
            State = false;
        }


        TurnChecker();



    }




    public void TurnChecker()
    {
        // Switches between PlayerGame and AIGame
        // append text to jTextArea 1 to display whose turn is it

        if ( PlayerGame == true )
        {
            PlayerGame = false;
            AIGame = true;
            turn++;
        }

        else if ( AIGame == true )
        {
            AIGame = false;
            PlayerGame = true;
            turn++;
        }


    }





    public Game() 
    {
        initComponents();
    }



    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {



        jPanel1 = new javax.swing.JPanel();
        Button1 = new javax.swing.JButton();
        Button2 = new javax.swing.JButton();
        Button3 = new javax.swing.JButton();
        Button4 = new javax.swing.JButton();
        Button5 = new javax.swing.JButton();
        Button6 = new javax.swing.JButton();
        Button7 = new javax.swing.JButton();
        Button8 = new javax.swing.JButton();
        Button9 = new javax.swing.JButton();
        Start = new javax.swing.JButton();
        Reset = new javax.swing.JButton();
        Exit = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea2 = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Noughts and Crosses");

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel1.setName(""); // NOI18N

        jTextArea1.append("Total Player Wins : " + PlayerCounts + "\n");
        jTextArea1.append("Total Computer Wins : " + ComputerCounts + "\n");
        jTextArea1.append("Total Draws : " + DrawCounts + "\n");


        jTextArea2.append(" Welcome to Noughts and Crosses!\n Please press Start to play!\n");

        DefaultCaret caret = (DefaultCaret)jTextArea2.getCaret();
        caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
        jTextArea2.setLineWrap(true);

// --------------------------------------------- -------------------------------------------------- ------------------------

        Button1.setText("");

        Button1.setEnabled(false);
        Button1.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                if ( State == true && playerO == true && PlayerGame == true )
                {
                    isEmpty = false;
                    Button1.setText("O");
                    // WinValidator
                    // TurnChecker ??
                }

                else if ( State == true && playerX == true && PlayerGame == true )
                {
                    isEmpty = false;
                    Button1.setText("X");
                    // WinValidator
                    // TurnChecker
                }

                else if ( State == true && computerO == true && AIGame == true)
                {
                    isEmpty = false;
                    // Call Computer Class
                    // WinValidator
                    // TurnChecker
                }

                else if ( State == true && computerX == true && AIGame == true)
                {
                    isEmpty = false;
                    // Call Computer Class
                    // WinValidator
                    // TurnChecker
                }

            }

        });


        Button2.setText("");
        Button2.setEnabled(false);

        Button2.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                if ( State == true && playerO == true && PlayerGame == true )
                {
                    isEmpty = false;
                    Button2.setText("O");
                    // WinValidator
                }

                else if ( State == true && playerX == true && PlayerGame == true )
                {
                    isEmpty = false;
                    Button2.setText("X");
                    // WinValidator
                    // TurnChecker
                }

                else if ( State == true && computerO == true && AIGame == true)
                {
                    isEmpty = false;
                    // Call Computer Class
                }

                else if ( State == true && computerX == true && AIGame == true)
                {
                    isEmpty = false;
                    // Call Computer Class
                }

            }

        });


        Button3.setText("");
        Button3.setEnabled(false);
        Button3.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                if ( State == true && playerO == true && PlayerGame == true )
                {
                    isEmpty = false;
                    Button3.setText("O");
                }

                else if ( State == true && playerX == true && PlayerGame == true )
                {
                    isEmpty = false;
                    Button3.setText("X");
                }

                else if ( State == true && computerO == true && AIGame == true)
                {
                    isEmpty = false;
                    // Call Computer Class
                }

                else if ( State == true && computerX == true && AIGame == true)
                {
                    isEmpty = false;
                    // Call Computer Class
                }

            }

        });

        Button4.setText("");
        Button4.setEnabled(false);
        Button4.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                if ( State == true && playerO == true && PlayerGame == true )
                {
                    isEmpty = false;
                    Button4.setText("O");
                }

                else if ( State == true && playerX == true && PlayerGame == true )
                {
                    isEmpty = false;
                    Button4.setText("X");
                }

                else if ( State == true && computerO == true && AIGame == true)
                {
                    isEmpty = false;
                    // Call Computer Class
                }

                else if ( State == true && computerX == true && AIGame == true)
                {
                    isEmpty = false;
                    // Call Computer Class
                }

            }

        });

        Button5.setText("");
        Button5.setEnabled(false);
        Button5.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                if ( State == true && playerO == true && PlayerGame == true )
                {
                    isEmpty = false;
                    Button5.setText("O");
                }

                else if ( State == true && playerX == true && PlayerGame == true )
                {
                    isEmpty = false;
                    Button5.setText("X");
                }

                else if ( State == true && computerO == true && AIGame == true)
                {
                    isEmpty = false;
                    // Call Computer Class
                }

                else if ( State == true && computerX == true && AIGame == true)
                {
                    isEmpty = false;
                    // Call Computer Class
                }

            }

        });

        Button6.setText("");
        Button6.setEnabled(false);
        Button6.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                if ( State == true && playerO == true && PlayerGame == true )
                {
                    isEmpty = false;
                    Button6.setText("O");
                    // WinValidator
                }

                else if ( State == true && playerX == true && PlayerGame == true )
                {
                    isEmpty = false;
                    Button6.setText("X");
                    //WinValidator
                }

                else if ( State == true && computerO == true && AIGame == true)
                {
                    isEmpty = false;
                    // Call Computer Class
                }

                else if ( State == true && computerX == true && AIGame == true)
                {
                    isEmpty = false;
                    // Call Computer Class
                }

            }

        });

        Button7.setText("");
        Button7.setEnabled(false);
        Button7.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                if ( State == true && playerO == true && PlayerGame == true )
                {
                    isEmpty = false;
                    Button7.setText("O");
                }

                else if ( State == true && playerX == true && PlayerGame == true )
                {
                    isEmpty = false;
                    Button7.setText("X");
                }

                else if ( State == true && computerO == true && AIGame == true)
                {
                    isEmpty = false;
                    // Call Computer Class
                }

                else if ( State == true && computerX == true && AIGame == true)
                {
                    isEmpty = false;
                    // Call Computer Class
                }

            }

        });

        Button8.setText("");
        Button8.setEnabled(false);
        Button8.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                if ( State == true && playerO == true && PlayerGame == true )
                {
                    isEmpty = false;
                    Button8.setText("O");
                }

                else if ( State == true && playerX == true && PlayerGame == true )
                {
                    isEmpty = false;
                    Button8.setText("X");
                }

                else if ( State == true && computerO == true && AIGame == true)
                {
                    isEmpty = false;
                    // Call Computer Class
                }

                else if ( State == true && computerX == true && AIGame == true)
                {
                    isEmpty = false;
                    // Call Computer Class
                }

            }

        });

        Button9.setText("");
        Button9.setEnabled(false);
        Button9.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                if ( State == true && playerO == true && PlayerGame == true )
                {
                    isEmpty = false;
                    Button9.setText("O");
                }

                else if ( State == true && playerX == true && PlayerGame == true )
                {
                    isEmpty = false;
                    Button9.setText("X");
                }

                else if ( State == true && computerO == true && AIGame == true)
                {
                    isEmpty = false;
                    // Call Computer Class
                }

                else if ( State == true && computerX == true && AIGame == true)
                {
                    isEmpty = false;
                    // Call Computer Class
                }

            }

        });

// --------------------------------------------- -------------------------------------------------- ---------------------------

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(Button7, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Button8, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Button9, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(Button4, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(Button5, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(12, 12, 12)
                                .addComponent(Button1, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(Button2, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(Button3, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(Button6, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addComponent(Button2, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(Button1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(Button3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Button4, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Button5, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Button6, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Button9, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Button8, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Button7, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );










        Start.setText("Start");
        Start.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {           

                Start.setEnabled(false);
                Button1.setEnabled(true);
                Button2.setEnabled(true);
                Button3.setEnabled(true);
                Button4.setEnabled(true);
                Button5.setEnabled(true);
                Button6.setEnabled(true);
                Button7.setEnabled(true);
                Button8.setEnabled(true);
                Button9.setEnabled(true);

                jTextArea2.setText("");

                StartGame();








            }

        });


        Reset.setText("Reset");
        Reset.setEnabled(false);





        Exit.setText("Exit");
        Exit.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                System.exit(0);
            }

        });








        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jTextArea2.setColumns(20);
        jTextArea2.setRows(5);
        jScrollPane2.setViewportView(jTextArea2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 283, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(Start, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(35, 35, 35)
                        .addComponent(Reset, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(33, 33, 33)
                        .addComponent(Exit, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(67, 67, 67)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 283, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(34, 34, 34)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(Start, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(Reset, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(Exit, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 79, Short.MAX_VALUE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>                          

}

0 个答案:

没有答案