2014-11-22 29 views
-1

快速問題,我開發了3個A.I,每個都有不同的深度。如何將JCombo Box添加到我的java代碼

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

chessGame.setPlayer(Piece.COLOR_BLACK, ai3);//Here is the line where the A.I is assigned cureently it is ai3 

我想允許用戶在遊戲開始時有一個選項來選擇A.I.我希望一些幫助與接口,

一個Jcombo將是完美的,但我需要誒實施幫助任何幫助將是巨大的

(我只是不知道如何做一個讓AI選擇)

目前AI的

AI1 AI2 AI3

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

你的最終代碼會是這樣。

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

     //////////// JCombobox 

     String[] comboTypes = { "ai1", "ai2", "ai3" }; 
     // Create the combo box, and set 2nd item as Default 
     JComboBox comboTypesList = new JComboBox(comboTypes); 
     comboTypesList.setSelectedIndex(2); 
     comboTypesList.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) { 
       JComboBox jcmbType = (JComboBox) e.getSource(); 
       String cmbType = (String) jcmbType.getSelectedItem(); 

       if(cmbType.equals("ai1")) 
        chessGame.setPlayer(Piece.COLOR_BLACK, ai1); 
       else if(cmbType.equals("ai2")) 
        chessGame.setPlayer(Piece.COLOR_BLACK, ai2); 
       else 
        chessGame.setPlayer(Piece.COLOR_BLACK, ai3); 
       startGame(chessGame); 
      } 
     }); 




    } 

    public void startGame(ChessGame chessGame){ 
     new Thread(chessGame).start(); 
    } 

} 

但要記住這一點,你應該添加JComboBoxJFrameJPanel,使GUI正常工作。

+0

我在哪裏添加我的當前代碼? – ClarkPamler93 2014-11-22 19:28:26

+0

我已經更新了我的答案,請檢查它。如果您發現我的答案有幫助,請將其標記爲正確。謝謝 – Junaid 2014-11-22 19:41:04

+0

但請記住,你**應該**添加'JComboBox'到'JFrame'和'JPanel',以便GUI工作正常。 – Junaid 2014-11-22 19:44:08

相關問題