需要帮助为用户功能添加难度选项 [英] Need Help adding a difficulty option for User functionality

查看:76
本文介绍了需要帮助为用户功能添加难度选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Quick question, I have developed 3 A.I's each with a different depth.

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

Currently to choose what A.I you want to play against you have to go into the java file called Main.java and change it to whichever one you want. The line to change is:

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

我想让用户在游戏开始时有一个选项来选择AI我希望得到一些关于界面的帮助,我在想像JOptionpane这样的东西可能有用。

I want to allow the user to have an option at the start of the game to choose the A.I. I was hoping for some help with the interface, I was thinking something something like a JOptionpane might work.

(我只是不知道如何为AI做一个选择)

(I'm just not sure how to do one for the A.I selection)

当前A.I's

ai1
ai2
ai3

ai1 ai2 ai3

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传递它。

You should probably use a JComboBox to allow the user to select among the 3 options available. If you make a splash JFrame with this JComboBox you can then create your main game frame afterward and pass it the value from the JComboBox.

例如,你可以拥有JComboBox提供设置Easy,Medium和Hard的难度选项。在JButton上使用动作侦听器从JComboBox获取选定的值并将其转换为适合您的minimax算法的int值。也就是说,传递1表示简单,2表示中等,3表示硬。

For example, you could have the JComboBox give options of difficulty setting Easy, Medium, and Hard. Using an action listener on a JButton get the selected value from the JComboBox and convert it to int value appropriate for your minimax algorithm. That is, pass 1 for easy, 2 for medium, and 3 for hard.

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

Next change your ai class so that maxDepth is in the constructor. Then when you instantiate your ai, just give it the value that was passed forward from the previous frame and you will have created the only ai you need at the right difficulty setting.

编辑:

看起来你设法得到类似的工作,这很棒!如果它对你有所帮助,我已经在下面列举了一个简短的例子,说明我将如何做到这一点。请注意,我还将其设置为使得SimpleAiPlayerHandler构造函数还采用int值来实例化maxDepth变量。你需要添加它。因为它使用我没有的类,所以我无法编译它。但是,如果其他人需要做类似的事情,只需删除DifficultyListener中的所有内容,除了print语句和从JComboBox获得困难的行,你会看到它工作(和编译)。

It looks like you managed to get something similar working which is great! In case it helps you, I have included a brief example of how I would have done this below. Note that I also set it up such that your SimpleAiPlayerHandler constructor also takes an int value for instantiating the maxDepth variable. You'll need to add this. Since it uses classes I don't have, I can't compile it. However, if anyone else needs to do something similar, just remove everything in the DifficultyListener except the print statement and the line that get's the difficulty from the JComboBox and you'll see it working (and compiling).

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();
        }
    }
}

这篇关于需要帮助为用户功能添加难度选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆