java tic tac toe电脑播放器

时间:2014-03-28 15:27:53

标签: java user-interface tic-tac-toe

我正在制作一个tictactoe游戏,但它需要能够让计算机移动,我已经尝试制作另一种方法computerTurn()如果(playerturn == false)会使按钮==" O&#34 ;然后使用嵌套的if语句来检查玩家放置的位置" X"并且基于那个做出新的动作,但我尝试的第一个没有做任何我认为它可能是因为按钮都在公共空间但是我有点难过这个任何人都有任何想法, 生病了我的代码,但是非常多,所以生病只发布按钮和移动制作的部分。如果你需要更多的代码来解决它让我知道并尽快发布它。 "我也有2个班级,tictactoe类拥有按钮和GUI设置,主要类主要是winchecks和设置为布尔" TICTACTOE CLASS

    package game;

import static game.main.playerturn;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.GroupLayout;
import javax.swing.LayoutStyle;
import javax.swing.JOptionPane;

public class tictactoe extends JFrame 
{
    main Main = new main();

        public tictactoe() {
                initComponents();
        }
    public void computerTurn()
    {
        tictactoe Tictactoe = new tictactoe();

        if( main.playerturn == false)
        {
            button5.setText("O");
            main.wincheck();
            main.playerturn = true;
        }
    }

        public void button1ActionPerformed(ActionEvent e)
        {
            if (button1.getText().equals(""))
            {
//               computerTurn(); 
               if (main.playerturn == true)
               {
                   button1.setText("X");
                   main.wincheck();
                   main.playerturn = false;
               }
               else
               {
                   button1.setText("O");
                   main.wincheck();
                   main.playerturn = true;
               }
            }
        }

        public void button2ActionPerformed(ActionEvent e) 
        {
            if (button2.getText().equals(""))
            {
               if (main.playerturn == true)
               {
                   button2.setText("X");
                   main.wincheck();
                   main.playerturn = false;
               }
               else
               {
                   button2.setText("O");
                   main.wincheck();
                   main.playerturn = true;
               }
            }
        }

        public void button3ActionPerformed(ActionEvent e)
        {
            if (button3.getText().equals(""))
            {
               if (main.playerturn == true)
               {
                   button3.setText("X");
                   main.wincheck();
                   main.playerturn = false;
               }
               else
               {
                   button3.setText("O");
                   main.wincheck();
                   main.playerturn = true;
               }
            }
        }

        public void button4ActionPerformed(ActionEvent e)
        {
            if (button4.getText().equals(""))
            {
               if (main.playerturn == true)
               {
                   button4.setText("X");
                   main.wincheck();
                   main.playerturn = false;
               }
               else
               {
                   button4.setText("O");
                   main.wincheck();
                   main.playerturn = true;
               }
            }
        }

        public void button5ActionPerformed(ActionEvent e)
        {
            if (button5.getText().equals(""))
            {
               if (main.playerturn == true)
               {
                   button5.setText("X");
                   main.wincheck();
                   main.playerturn = false;
               }
               else
               {
                   button5.setText("O");
                   main.wincheck();
                   main.playerturn = true;
               }
            }
        }

        public void button6ActionPerformed(ActionEvent e)
        {
            if (button6.getText().equals(""))
            {
               if (main.playerturn == true)
               {
                   button6.setText("X");
                   main.wincheck();
                   main.playerturn = false;
               }
               else
               {
                   button6.setText("O");
                   main.wincheck();
                   main.playerturn = true;
               }
            }

        }

        public void button7ActionPerformed(ActionEvent e)
        {
            if (button7.getText().equals(""))
            {
               if (main.playerturn == true)
               {
                   button7.setText("X");
                   main.wincheck();
                   main.playerturn = false;
               }
               else
               {
                   button7.setText("O");
                   main.wincheck();
                   main.playerturn = true;
               }
            }
        }

        public void button8ActionPerformed(ActionEvent e) 
        {
            if (button8.getText().equals(""))
            {
               if (main.playerturn == true)
               {
                   button8.setText("X");
                   main.wincheck();
                   main.playerturn = false;
               }
               else
               {
                   button8.setText("O");
                   main.wincheck();
                   main.playerturn = true;
               }
            }
        }

        public void button9ActionPerformed(ActionEvent e)
        {
            if (button9.getText().equals(""))
            {
               if (main.playerturn == true)
               {
                   button9.setText("X");
                   main.wincheck();
                   main.playerturn = false;
               }
               else
               {
                   button9.setText("O");
                   main.wincheck();
                   main.playerturn = true;
               }
            }
        }
        //computer turn*****
//    public void computerTurn()
//    {
//        tictactoe Tictactoe = new tictactoe();
//                
//        if( playerturn == false)
//        {
//            button5.setText("O");
//            main.wincheck();
//            main.playerturn = true;
//        }
//        
//        
//    }

        public void initComponents() 
        {
                button1 = new JButton();
                button2 = new JButton();
                button3 = new JButton();
                button4 = new JButton();
                button5 = new JButton();
                button6 = new JButton();
                button7 = new JButton();
                button8 = new JButton();
                button9 = new JButton();

                setTitle("Tic Tac Toe");
                Container contentPane = getContentPane();

                //---- button1 ----
                button1.setText("");
                button1.addActionListener(new ActionListener() 
                {
                        @Override
                        public void actionPerformed(ActionEvent e)
                        {
                                button1ActionPerformed(e);
                        }
                });

                //---- button2 ----
                button2.setText("");
                button2.addActionListener(new ActionListener()
                {
                        @Override
                        public void actionPerformed(ActionEvent e)
                        {
                                button2ActionPerformed(e);
                        }
                });

                //---- button3 ----
                button3.setText("");
                button3.addActionListener(new ActionListener() 
                {
                        @Override
                        public void actionPerformed(ActionEvent e) 
                        {
                                button3ActionPerformed(e);
                        }
                });

                //---- button4 ----
                button4.setText("");
                button4.addActionListener(new ActionListener() 
                {
                        @Override
                        public void actionPerformed(ActionEvent e) 
                        {
                                button4ActionPerformed(e);
                        }
                });

                //---- button5 ----
                button5.setText("");
                button5.addActionListener(new ActionListener() 
                {
                        @Override
                        public void actionPerformed(ActionEvent e)
                        {
                                button5ActionPerformed(e);
                        }
                });

                //---- button6 ----
                button6.setText("");
                button6.addActionListener(new ActionListener()
                {
                        @Override
                        public void actionPerformed(ActionEvent e) 
                        {
                                button6ActionPerformed(e);
                        }
                });

                //---- button7 ----
                button7.setText("");
                button7.addActionListener(new ActionListener()
                {
                        @Override
                        public void actionPerformed(ActionEvent e) 
                        {
                                button7ActionPerformed(e);
                        }
                });

                //---- button8 ----
                button8.setText("");
                button8.addActionListener(new ActionListener()
                {
                        @Override
                        public void actionPerformed(ActionEvent e)
                        {
                                button8ActionPerformed(e);
                        }
                });

                //---- button9 ----
                button9.setText("");
                button9.addActionListener(new ActionListener() 
                {
                        @Override
                        public void actionPerformed(ActionEvent e)
                        {
                                button9ActionPerformed(e);
                        }
                });

                GroupLayout contentPaneLayout = new GroupLayout(contentPane);
                contentPane.setLayout(contentPaneLayout);
                contentPaneLayout.setHorizontalGroup(
                        contentPaneLayout.createParallelGroup()
                                .addGroup(contentPaneLayout.createSequentialGroup()
                                        .addContainerGap()
                                        .addGroup(contentPaneLayout.createParallelGroup()
                                                .addGroup(contentPaneLayout.createSequentialGroup()
                                                        .addComponent(button1, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE)
                                                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(button2, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE)
                                                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(button3, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE))
                                                .addGroup(contentPaneLayout.createSequentialGroup()
                                                        .addComponent(button4, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE)
                                                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(button5, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE)
                                                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(button6, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE))
                                                .addGroup(contentPaneLayout.createSequentialGroup()
                                                        .addComponent(button7, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE)
                                                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(button8, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE)
                                                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(button9, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE)))
                                        .addContainerGap(11, Short.MAX_VALUE))
                );
                contentPaneLayout.setVerticalGroup(
                        contentPaneLayout.createParallelGroup()
                                .addGroup(contentPaneLayout.createSequentialGroup()
                                        .addContainerGap()
                                        .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                                .addComponent(button1, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE)
                                                .addComponent(button2, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE)
                                                .addComponent(button3, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE))
                                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                                .addComponent(button4, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE)
                                                .addComponent(button5, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE)
                                                .addComponent(button6, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE))
                                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                                .addComponent(button7, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE)
                                                .addComponent(button8, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE)
                                                .addComponent(button9, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE))
                                        .addContainerGap(15, Short.MAX_VALUE))
                );
                pack();
                setLocationRelativeTo(getOwner());
        }

        public JButton button1;
        public JButton button2;
        public JButton button3;
        public JButton button4;
        public JButton button5;
        public JButton button6;
        public JButton button7;
        public JButton button8;
        public JButton button9;
}

MAIN CLASS

package game;

import javax.swing.JOptionPane;

public class main
{
    public static boolean playerturn = false;
    public static boolean computerturn = true;
    public static boolean playerwon = false;
    public static boolean computerwon = false;

    public static tictactoe board = new tictactoe();

    public static void main(String[] args)
    {
        if (board.isVisible() == false)
        {
            board.setVisible(true);
        }

    }

//    public static void computerTurn()
//    {
//        tictactoe Tictactoe = new tictactoe();
//                
//        if( playerturn == false)
//        {
//            button5.setText("O");
//            main.wincheck();
//            main.playerturn = true;
//        }
//    }
    public static void wincheck()
    {
        if(board.button1.getText().equals("X"))
        {
            if(board.button4.getText().equals("X"))
            {
                if(board.button7.getText().equals("X"))
                {
                    playerwon = true;
                    computerwon = false;
                    JOptionPane.showMessageDialog(null, "Player wins, good for you.");
                }
            }
        }
        if(board.button1.getText().equals("X"))
        {
            if(board.button5.getText().equals("X"))
            {
                if(board.button9.getText().equals("X"))
                {
                    playerwon = true;
                    computerwon = false;
                    JOptionPane.showMessageDialog(null, "Player wins, good for you.");
                }
            }
        }
        if(board.button1.getText().equals("X"))
        {
            if(board.button2.getText().equals("X"))
            {
                if(board.button3.getText().equals("X"))
                {
                    playerwon = true;
                    computerwon = false;
                    JOptionPane.showMessageDialog(null, "Player wins, good for you.");
                }
            }
        }
        if(board.button3.getText().equals("X"))
        {
            if(board.button5.getText().equals("X"))
            {
                if(board.button7.getText().equals("X"))
                {
                    playerwon = true;
                    computerwon = false;
                    JOptionPane.showMessageDialog(null, "Player wins, good for you.");
                }
            }
        }
        if(board.button3.getText().equals("X"))
        {
            if(board.button6.getText().equals("X"))
            {
                if(board.button9.getText().equals("X"))
                {
                    playerwon = true;
                    computerwon = false;
                    JOptionPane.showMessageDialog(null, "Player wins, good for you.");
                }
            }
        }
        if(board.button7.getText().equals("X"))
        {
            if(board.button8.getText().equals("X"))
            {
                if(board.button9.getText().equals("X"))
                {
                    playerwon = true;
                    computerwon = false;
                    JOptionPane.showMessageDialog(null, "Player wins, good for you.");
                }
            }
        }
        if(board.button4.getText().equals("X"))
        {
            if(board.button5.getText().equals("X"))
            {
                if(board.button6.getText().equals("X"))
                {
                    playerwon = true;
                    computerwon = false;
                    JOptionPane.showMessageDialog(null, "Player wins, good for you.");
                }
            }
        }
        if(board.button2.getText().equals("X"))
        {
            if(board.button5.getText().equals("X"))
            {
                if(board.button8.getText().equals("X"))
                {
                    playerwon = true;
                    computerwon = false;
                    JOptionPane.showMessageDialog(null, "Player wins, good for you.");
                }
            }
        }
        if(board.button1.getText().equals("O"))
        {
            if(board.button4.getText().equals("O"))
            {
                if(board.button7.getText().equals("O"))
                {
                    playerwon = false;
                    computerwon = true;
                    JOptionPane.showMessageDialog(null, "Computer wins, you suck.");
                }
            }
        }
        if(board.button1.getText().equals("O"))
        {
            if(board.button5.getText().equals("O"))
            {
                if(board.button9.getText().equals("O"))
                {
                    playerwon = false;
                    computerwon = true;
                    JOptionPane.showMessageDialog(null, "Computer wins, you suck.");
                }
            }
        }
        if(board.button1.getText().equals("O"))
        {
            if(board.button2.getText().equals("O"))
            {
                if(board.button3.getText().equals("O"))
                {
                    playerwon = false;
                    computerwon = true;
                    JOptionPane.showMessageDialog(null, "Computer wins, you suck.");
                }
            }
        }
        if(board.button3.getText().equals("O"))
        {
            if(board.button5.getText().equals("O"))
            {
                if(board.button7.getText().equals("O"))
                {
                    playerwon = false;
                    computerwon = true;
                    JOptionPane.showMessageDialog(null, "Computer wins, you suck.");
                }
            }
        }
        if(board.button3.getText().equals("O"))
        {
            if(board.button6.getText().equals("O"))
            {
                if(board.button9.getText().equals("O"))
                {
                    playerwon = false;
                    computerwon = true;
                    JOptionPane.showMessageDialog(null, "Computer wins, you suck.");
                }
            }
        }
        if(board.button7.getText().equals("O"))
        {
            if(board.button8.getText().equals("O"))
            {
                if(board.button9.getText().equals("O"))
                {
                    playerwon = false;
                    computerwon = true;
                    JOptionPane.showMessageDialog(null, "Computer wins, you suck.");
                }
            }
        }
        if(board.button4.getText().equals("O"))
        {
            if(board.button5.getText().equals("O"))
            {
                if(board.button6.getText().equals("O"))
                {
                    playerwon = false;
                    computerwon = true;
                    JOptionPane.showMessageDialog(null, "Computer wins, you suck.");
                }
            }
        }
        if(board.button2.getText().equals("O"))
        {
            if(board.button5.getText().equals("O"))
            {
                if(board.button8.getText().equals("O"))
                {
                    playerwon = false;
                    computerwon = true;
                    JOptionPane.showMessageDialog(null, "Computer wins, you suck.");
                }
            }
        }
        if (playerwon == true || computerwon == true)
        {
            board.setVisible(false);
            JOptionPane.showMessageDialog(null, "play again");
//            board.setVisible(true);
        }
    }
}

0 个答案:

没有答案