2014-11-21 117 views
1

快速問題,我開發了3個A.I,每個具有不同的深度。需要幫助爲用戶功能添加難度選項

當前選擇什麼A.I你想對你玩什麼必須進入名爲Main.java的Java文件並將其更改爲你想要的任何一個。要改變的路線是:

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

我想允許用戶在遊戲開始時有一個選項來選擇A.I.我希望對界面有所幫助,我在想像一個像JOptionpane這樣的東西可能會起作用。

(我只是不知道如何做一個對A.I選擇)的任何幫助

當前A.I的

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

} 

感謝。

+0

你可以使用一個'JComboBox',請參見[如何使用組合框(http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html)瞭解更多詳情,或您可以在'ButtonGroup'中使用'JRadioButton'或'JToggleButton',參見[如何使用按鈕,複選框和單選按鈕](http://docs.oracle.com/javase/tutorial/uiswing/components/ button.html)以獲取更多詳細信息 – MadProgrammer 2014-11-21 08:44:21

+0

它看起來像您在SimpleAiPlayerHandler中的maxDepth變量是公共的。你應該改變它爲private,並改爲使用一些訪問器或增變器方法。因爲你不太可能在創建對象後改變它,所以你甚至可以把它添加到構造函數中。 – sage88 2014-11-21 08:44:29

回答

1

您應該使用JComboBox來允許用戶從3個可用選項中進行選擇。如果您使用此JComboBox創建了一個啓動JFrame,那麼您可以在之後創建主遊戲幀,並將其從JComboBox傳遞給它。

例如,您可以讓JComboBox提供設置Easy,Medium和Hard的難度選項。在JButton上使用動作偵聽器從JComboBox中獲取選定的值,並將其轉換爲適合您的最小最大化算法的int值。也就是說,容易傳球1次,中速傳球2次,硬傳球3次。

接下來更改您的ai類,以便maxDepth在構造函數中。然後,當你實例化你的ai時,只需給它從前一幀向前傳遞的值,你就可以在正確的難度設置下創建唯一需要的ai。

編輯:

看起來你設法得到類似的東西的工作,這是偉大!如果它可以幫助你,我已經包含了一個簡單的例子,說明我將如何做到這一點。請注意,我也將其設置爲使您的SimpleAiPlayerHandler構造函數也接受一個int值來實例化maxDepth變量。你需要添加這個。由於它使用我沒有的類,所以我無法編譯它。但是,如果其他人需要做類似的事情,只需刪除DifficultyListener中的所有內容,但打印語句和從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(); 
     } 
    } 
} 
+0

你能告訴我那些代碼看起來像我看過一些例子,但不知道如何添加它? – ClarkPamler93 2014-11-21 11:18:38

+0

@ ClarkPamler93:引用了一個使用'enum'的完整示例[這裏](http://stackoverflow.com/a/9246664/230513)。 – trashgod 2014-11-21 12:12:59

+0

我已閱讀您給予我的鏈接,但我仍然不知道如何將其應用於我的代碼 – ClarkPamler93 2014-11-21 15:39:34