TicTacToe GUI应用程序重置按钮

时间:2015-11-30 01:39:05

标签: java arrays swing jframe

我已经完成了我的tictactoe游戏,但我无法弄清楚如何让我的重置按钮启动游戏结束。我还希望当有人获胜或有平局时重置游戏。任何帮助将不胜感激。

这是我的代码(很抱歉有这么多):

package tictactoe;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
 public class TicTacToe 
 {


    public static void main(String[] args) 
    {
       JFrame ticTacToe = new TicTacToeFrame();
        ticTacToe.setTitle("Phantom TicTacToe Game!");
        ticTacToe.setSize(600, 600);
        ticTacToe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ticTacToe.setLocationRelativeTo(null);
        ticTacToe.setVisible(true);  
    }

}
 class TicTacToeFrame extends JFrame implements ActionListener 
{

   private char whoseTurn = 'X';
   private boolean gameOver = false;


   private Cell[][] cells = new Cell[3][3];


   JLabel jlblStatus = new JLabel("X's turn to play");


   public TicTacToeFrame()
   {
       JButton btn = new JButton("Reset");
       JPanel panel = new JPanel(new GridLayout(3, 3, 0, 0));
       for (int i = 0; i < 3; i++)
           for (int j = 0; j < 3; j++)
               panel.add(cells[i][j] = new Cell());


       add(btn, BorderLayout.SOUTH);
       add(panel, BorderLayout.CENTER);
       add(jlblStatus, BorderLayout.NORTH);
       jlblStatus.setOpaque(true);
       jlblStatus.setBackground(Color.YELLOW);
   }


    public boolean isFull()
    {
       for (int i = 0; i < 3; i++)
           for (int j = 0; j < 3; j++)
               if (cells[i][j].getToken() == ' ')
                   return false;
       return true;
    }


   public boolean isWon(char token)
   {

       for (int i = 0; i < 3; i++)
           if ((cells[i][0].getToken() == token)
                   && (cells[i][1].getToken() == token)
                   && (cells[i][2].getToken() == token)) 
           {
               return true;
           }


       for (int j = 0; j < 3; j++)
           if ((cells[0][j].getToken() == token)
               && (cells[1][j].getToken() == token)
               && (cells[2][j].getToken() == token))
           {
               return true;
           }

       if ((cells[0][0].getToken() == token)
               && (cells[1][1].getToken() == token)
               && (cells[2][2].getToken() == token))
           {
               return true;
           }

       if ((cells[0][2].getToken() == token)
               && (cells[1][1].getToken() == token)
               && (cells[2][0].getToken() == token))
           {
               return true;
           }

       return false;
   }

    @Override
    public void actionPerformed(ActionEvent e) 
    {
        JButton bttn = (JButton) e.getSource();
        if(bttn == btn)
        {

        }
    }


    public class Cell extends JPanel
    {

       private char token = ' ';


       public Cell()
       {
           setBorder(new LineBorder(Color.black, 1));
           addMouseListener(new MyMouseListener());
       }


       public char getToken()
       {
           return token;
       }


       public void setToken(char c)
       {
           token = c;
           repaint();
       }

       @Override
       protected void paintComponent(Graphics g)
       {
           super.paintComponent(g);

           if (token == 'X')
           {
               g.drawLine(10, 10, getWidth() - 10, getHeight() - 10);
               g.drawLine(getWidth() - 10, 10, 10, getHeight() - 10);
           }

           else if (token == 'O')
           {
               g.drawOval(10, 10, getWidth() - 20, getHeight() - 20);
           }
       }

       private class MyMouseListener extends MouseAdapter
       {
           @Override
           public void mouseClicked(MouseEvent e)
           {
               if (gameOver)
                   return;


               if (token == ' ' && whoseTurn != ' ')
                   setToken(whoseTurn);


               if (isWon(whoseTurn))
               {
                   jlblStatus.setText(whoseTurn + " won! Game over!");
                   whoseTurn = ' ';
                   gameOver = true;
               }
               else if (isFull())
               {
                   jlblStatus.setText("Tie game! Game over!");
                   whoseTurn = ' ';
                   gameOver = true;
               }
               else
               {
                   whoseTurn = (whoseTurn == 'X') ? 'O' : 'X';
                   jlblStatus.setText(whoseTurn + "'s turn.");
               }
           }

       } 
    } 
} 

2 个答案:

答案 0 :(得分:0)

您需要重置程序的状态,包括重新创建或重置某些用户界面(标签和需要更改九个单元格的面板)。

要考虑的问题:

  • TicTacToeFrame有一个actionPerformed方法并实现了ActionListener接口,但重置按钮还没有动作监听器;
  • 用于重置:您可以将whoseTurngameOver字段设置为初始值,在jlblStatus标签中设置文字,然后
    • 选项1:删除panel中的所有单元格并添加新单元格(就像您在构造函数中已做的那样 - 这可能是initializeCells方法)或
    • 选项2(如果您更喜欢回收):向清除reset字段的Cell类添加token方法,为所有单元格调用此重置方法,并告诉{ {1}}重新绘制。)

要记住的小事:

  • 如果您想在构造函数之外使用panelpanel组件,请将它们更改为字段(而不是局部变量);
  • button方法中的if语句使用actionPerformed运算符而不是==方法 - 您确定吗? (提示:见What is the difference between == vs equals() in Java?)。

答案 1 :(得分:0)

您已有代码。

    JFrame ticTacToe = new TicTacToeFrame();
    ticTacToe.setTitle("Phantom TicTacToe Game!");
    ticTacToe.setSize(600, 600);
    ticTacToe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ticTacToe.setLocationRelativeTo(null);
    ticTacToe.setVisible(true);  

只需将其置于startNewGame()方法中并在游戏开始时和赢得游戏时调用它;)