Need Help adding a difficulty option for User functionality

前端 未结 1 601
忘了有多久
忘了有多久 2021-01-24 12:44

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

Currently to choose what A.I you want to play against you have to go into the java file called Mai

1条回答
  •  清酒与你
    2021-01-24 13:25

    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.

    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.

    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.

    EDIT:

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

    0 讨论(0)
提交回复
热议问题