栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

需要帮助,为用户功能添加困难选项

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

需要帮助,为用户功能添加困难选项

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

例如,您可以让JComboBox提供困难设置Easy,Medium和Hard的选项。在JButton上使用动作侦听器,从JComboBox获取选定的值,并将其转换为适合于minimax算法的int值。也就是说,通过1代表轻松,2代表中等,3代表艰苦。

接下来,更改您的ai类,以便将maxDepth放入构造函数中。然后,当实例化ai时,只需为其提供从前一帧传递来的值,就可以在正确的难度设置下创建所需的唯一ai。

编辑:

看来您设法完成了类似的工作,这真是太好了!为了对您有所帮助,我在下面提供了一个简短的示例,说明了如何进行此操作。请注意,我还对它进行了设置,以使您的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();        }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/516278.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号