首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >需要帮助为用户功能添加一个困难选项

需要帮助为用户功能添加一个困难选项
EN

Stack Overflow用户
提问于 2014-11-21 08:36:16
回答 1查看 619关注 0票数 0

快速的问题,我已经开发了3A,我每个都有一个不同的深度。

目前,要选择您想要使用的A.I,您必须进入名为Main.java的java文件,并将其更改为您想要的任何文件。需要改变的是:

代码语言:javascript
复制
chessGame.setPlayer(Piece.COLOR_BLACK, ai3);//Here A.I is assigned 

我想让用户在游戏开始时有一个选项来选择A.I,我希望能在界面上得到一些帮助,我在想一些类似JOptionpane的东西可能会起作用。

(我只是不知道如何为A.I的选择做一个)

目前的A.I

ai1 ai2 ai3

代码语言:javascript
复制
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();
    }

}

谢谢你的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-21 08:40:01

您可能应该使用JComboBox来允许用户在可用的3种选项中进行选择。如果您使用这个JFrame创建了一个splash JComboBox,那么您可以在之后创建您的主要游戏框架,并从JComboBox中传递它的值。

例如,您可以让JComboBox给出设置简单、中等和硬的困难选项。使用JButton上的动作侦听器,从JComboBox中获取选定的值,并将其转换为适合您的minimax算法的int值。也就是说,容易通过1,中等通过2,硬通过3。

接下来,更改ai类,以便maxDepth位于构造函数中。然后,当您实例化您的ai时,只需给它从前一个框架中传递的值,您就可以在正确的困难设置下创建您所需的唯一ai。

编辑:

看起来你成功地得到了类似的工作,这是很棒的!如果它对你有帮助的话,我在下面给出了一个简单的例子,说明我会如何做到这一点。请注意,我还将其设置为您的SimpleAiPlayerHandler构造函数还接受一个int值来实例化maxDepth变量。你得把这个加进去。因为它使用的是我没有的类,所以我无法编译它。但是,如果其他人需要做类似的事情,只需删除DifficultyListener中的所有内容,除了print语句和从JComboBox中获得困难的行之外,您将看到它正常工作(并进行编译)。

代码语言:javascript
复制
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();
        }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27057132

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档