java - 井字游戏的开始菜单

标签 java tic-tac-toe

我是java新手,正在尝试创建一个简单的游戏。游戏本身已经完成并且可以运行,但是,我在创建标题屏幕时遇到了困难。我想要的只是一个 600 x 600 的标题屏幕,它在实际游戏之前打开,并且有一个“开始”按钮。我不太确定该怎么做,我可以获得一些帮助吗?

我的游戏代码如下

package tictactoemain;

import javax.swing.JFrame;

/**
 * TicTacToe Assignment
 * @author Ekin
 *
 */
public class TicTacToe 

{
    // Creating a (600, 600) window that can stop the game when he window is closed
    public static void main(String[] args) 
    {
        JFrame ticTacToe = new TicTacToeFrame();
        ticTacToe.setTitle("Ekin's Tic-Tac-Toe Game!");
        ticTacToe.setSize(600, 600);
        ticTacToe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ticTacToe.setLocationRelativeTo(null);
        ticTacToe.setVisible(true);
    }


}// end of TicTacToe
package tictactoemain;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
/**
* JFrame to hold TicTacToe board.
*/
public class TicTacToeFrame extends JFrame
{

    private static final long serialVersionUID = 1L;

// Indicate whose turn it is
   private char whoseTurn = 'X';
   private boolean gameOver = false;

   // Create cell grid using an Array
   private Cell[][] cells = new Cell[3][3];

   // Create a status label
   JLabel jlblStatus = new JLabel("X's turn to play");

   /**
    * No-argument Constructor
    * @return 
    */
   public TicTacToeFrame()
   {
       // Panel to hold cells
       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());

       panel.setBorder(new LineBorder(Color.red, 1));
       jlblStatus.setBorder(new LineBorder(Color.yellow, 1));

       add(panel, BorderLayout.CENTER);
       add(jlblStatus, BorderLayout.SOUTH);
   }
   // Determine if it's a tie
   /**
    * Determine if game board is full.
    * @return True, if game board is full. Otherwise, false.
    */
    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;
    }
    // Check to see if a player (Token) has won
   /**
    * Determines if a given token has won.
    * @param token Token to test for winning
    * @return True, if the token has won. Otherwise, false.
    */
   public boolean isWon(char token)
   {
       // check rows
       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;
           }

       // check columns
       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;
           }
       // check diagonals
       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;
   }

    /**
    * Defines a cell in a TicTacToe game board.
    */
    public class Cell extends JPanel
    {
       /**
         * 
         */
        private static final long serialVersionUID = 1L;
    // token of this cell
       private char token = ' ';

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

       /**
        * Gets the token of the cell.
        * @return The token value of the cell.
        */
       public char getToken()
       {
           return token;
       }

       /**
        * Sets the token of the cell.
        * @param c Character to use as token value.
        */
       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);
           }
       }
       // MouseListener to listen for a click to place a token
       private class MyMouseListener extends MouseAdapter
       {
           @Override
           public void mouseClicked(MouseEvent e)
           {
               if (gameOver)
                   return;

               // if the cell is empty and the game is not over
               if (token == ' ' && whoseTurn != ' ')
                   setToken(whoseTurn);

               // Check game status
               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 to play.");
               }
           }
       } // End class MyMouseListener
    } 
} // End class TicTacToeFrame

最佳答案

注意:我正在使用启动屏幕从我的绘画应用程序复制代码,请勿复制粘贴此代码。自己写一些吧! :)

我所做的就是向按钮添加一个 ActionListener,并告诉按钮在单击时关闭开始屏幕并打开主屏幕。这是代码示例(来 self 的绘画应用程序):

private class StartListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent arg0) {
        startFrame.setVisible(false); //close start window
        paintApp.go(); //the go function contains code for opening the 
                       //main window
                       //the paintApp is an object that contains both
                       //JFrames, starting and main 
    }
}

关于java - 井字游戏的开始菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44121693/

相关文章:

Java 不会停止读取输入

java - 将 grails 集成到我的 Java EE 应用程序中

Java:为什么我的程序只运行 9 次而不是 1000 次(for 和 while 循环)?

algorithm - 井字棋牌算法

c++ - 如何将 "A1"样式的行+列规范作为输入?

java - Java 中带有 GUI 的 TicTacToe

Java keytool/用java生成 key 的安全性(一般)

java - 开发模式服务器中的 GWT XML 配置解析错误

algorithm - 如何判断井字游戏是否结束?

java - 有人能解释一下 java 是如何仅在以下情况下按值传递的吗