java - 需要帮助添加用户功能的难度选项

标签 java swing joptionpane

问个问题,我开发了 3 个人工智能,每个都有不同的深度。

目前,要选择您想要对抗的 A.I,您必须进入名为 Main.java 的 java 文件并将其更改为您想要的任何一个。要更改的行是:

chessGame.setPlayer(Piece.COLOR_BLACK, ai3);//Here A.I is assigned 

我想让用户在游戏开始时可以选择人工智能。我希望在界面方面得到一些帮助,我想像 JOptionpane 这样的东西可能会起作用。

(我只是不知道如何为人工智能选择做一个)

当前的人工智能

ai1 艾2 人工智能3

package chess;
import chess.ai.SimpleAiPlayerHandler;

import chess.gui.ChessGui;
import chess.logic.ChessGame;
import chess.logic.Piece;


public class Main {

    public static void main(String[] args) {

        // Creating the Game 
        ChessGame chessGame = new ChessGame();

        // Creating the Human Player 
        //Human Player is the Object chessGui
        ChessGui chessGui = new ChessGui(chessGame);
        //Creating the A.I's
        SimpleAiPlayerHandler ai1 = new SimpleAiPlayerHandler(chessGame);//Super Dumb
        SimpleAiPlayerHandler ai2 = new SimpleAiPlayerHandler(chessGame);//Dumb
        SimpleAiPlayerHandler ai3 = new SimpleAiPlayerHandler(chessGame);//Not So Dumb

        // Set strength of AI, how far they can see ahead 
        ai1.maxDepth = 1;
        ai1.maxDepth = 2;
        ai3.maxDepth = 3;

        //Assign the Human to White 
        chessGame.setPlayer(Piece.COLOR_WHITE, chessGui);
        //Assign the not so dumb A.I to black 
        chessGame.setPlayer(Piece.COLOR_BLACK, ai3);

        // in the end we start the game
        new Thread(chessGame).start();
    }

}

感谢您的帮助。

最佳答案

您可能应该使用 JComboBox 来允许用户在 3 个可用选项中进行选择。如果您使用此 JComboBox 制作启动 JFrame,则可以随后创建主游戏框架并将 JComboBox 中的值传递给它。

例如,您可以让 JComboBox 提供难度设置“简单”、“中等”和“困难”的选项。在 JButton 上使用操作监听器从 JComboBox 获取所选值,并将其转换为适合您的 minimax 算法的 int 值。即,通过 1 表示简单,通过 2 表示中等,通过 3 表示困难。

接下来更改你的 ai 类,以便 maxDepth 位于构造函数中。然后,当你实例化你的人工智能时,只需给它从前一帧传递的值,你就可以在正确的难度设置下创建你需要的唯一人工智能。

编辑:

看起来你已经成功地得到了类似的东西,这太棒了!如果它对您有帮助,我在下面提供了一个简短的示例来说明我将如何做到这一点。请注意,我还对其进行了设置,以便您的 SimpleAiPlayerHandler 构造函数也采用 int 值来实例化 maxDepth 变量。你需要添加这个。由于它使用我没有的类,因此我无法编译它。但是,如果其他人需要做类似的事情,只需删除 DifficultyListener 中的所有内容(除了 print 语句和从 JComboBox 获取难度的行),您就会看到它正在工作(并正在编译)。

import javax.swing.*;
import java.awt.event.*;

public class ChessSplash extends JFrame {
    private final JComboBox<Difficulty> difficultySetting;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                ChessSplash gui = new ChessSplash();
                gui.setVisible(true);
            }
        });
    }

    public enum Difficulty {
        EASY(1, "Easy"), MEDIUM(2, "Medium"), HARD(3, "Hard");

        private final int intValue;
        private final String stringValue;

        private Difficulty(int intValue, String stringValue) {
            this.intValue = intValue;
            this.stringValue = stringValue;
        }

        @Override
        public String toString() {
            return stringValue;
        }
    };

    public ChessSplash() {
        super("Chess Game");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        difficultySetting = new JComboBox<>(Difficulty.values());
        JButton startButton = new JButton("Start Game");
        startButton.addActionListener(new DifficultyListener());
        JPanel mainPanel = new JPanel();
        add(mainPanel);
        mainPanel.add(difficultySetting);
        mainPanel.add(startButton);
        pack();
    }

    private class DifficultyListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            //Declare AI
            SimpleAiPlayerHandler ai;

            //Declare and Instantiate Chess Game
            ChessGame chessGame = new ChessGame();

            //Human Player is the Object chessGui
            ChessGui chessGui = new ChessGui(chessGame);
            //Assign Human Player to White
            chessGame.setPlayer(Piece.COLOR_WHITE, chessGui);

            //Get the selected difficulty setting
            Difficulty difficulty = (Difficulty)difficultySetting.getSelectedItem();

            //Instantiate Computer AI pass it the maxDepth for use in the constructor
            ai = new SimpleAiPlayerHandler(difficulty.intValue, chessGame);
            //Assign Computer Player to Black
            chessGame.setPlayer(Piece.COLOR_BLACK, ai);
            //Demonstrate the enum combobox works
            System.out.println(difficulty.intValue);

            //Dispose of the splash JFrame
            ChessSplash.this.dispose();

            //Start your game thread (I would probably do something to move this
            //onto the EDT if you're doing this is swing personally
            new Thread(chessGame).start();
        }
    }
}

关于java - 需要帮助添加用户功能的难度选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27057132/

相关文章:

java - 使用 Eclipse 的 GAE 中的 NoClassDefFoundError

java - Selenium HtmlUnitDriver 在随机位置随机挂起

java - 如何在 TextField Java 中限制字符和数字值

java - 没有按钮的 JOptionPane

java - 在 Java Swing 中,当我将 JScrollPane 添加到我的面板时,什么也没有出现

java - 从 PHP 脚本调用 Java 时出现问题

java - Java 游戏中的滚动 JFrame

java - 扩展JTable裁剪问题

java - JOptionPane 的小问题

java - 如何将搜索结果全部集中在一个框中?而不是(JOptionPane单独的框)