2017-03-27 101 views
1
public class Game { 

    private ArrayList<Player> players; 
    private ArrayList<Card> deck; 
    private ArrayList<Card> pool; 
    **private ArrayList<Capture> capture;** 
    private int currentPlayer; 
    private int playDirection; 
    int index; 
    int count = 0; 
    int passNo = 0; 

    Scanner input = new Scanner(System.in); 
    Scanner input2 = new Scanner(System.in); 
    Scanner input3 = new Scanner(System.in); 

    public Game() 
    { 
     deck = new ArrayList<Card>(); 
     pool = new ArrayList<Card>(); 
     capture = new ArrayList<Capture>(); 

     for(int c=0; c<4; c++){ 
      for(int i=0; i<13; i++){ 
       deck.add(new Card(c,i)); 
      } 
      Collections.shuffle(deck); 
     } 

    }  

    public Player play() { 
     Player player = getCurrentPlayer(); 
     int pass = -1; 
     int option =0; 
     int count =0; 
     boolean play = false; 
     boolean check = true; 

     capture = new ArrayList<Capture>(); 

     System.out.println(); 
     System.out.println(player + ":" + player.getCards()); 

     do{ 
      System.out.println("Please select an option: ((1) Capture (2) Discard a card)"); 
      option = input2.nextInt(); 


      play=player.isPlayable(); 


      switch(option) 
      { 
      case 1: 
       if(play == true){ 

        System.out.print("HandCard:" + player.getCards()); 
        System.out.print(" Choose a Number from 0 to " + (player.getCards().size()-1)+ " : "); 
        int num = input.nextInt(); 
        player.getCards().remove(num); 
        //after prompting user for entering the cards they wanted 
        //the following sentence has following error               
        **capture.add(player.getCards().get(num));** 
        //"The method add(Capture) in the type ArrayList<Capture> is 
        //not applicable for the arguments (Card)" 
        System.out.print("How many card you want capture from pool: (Choose 1 number from 1 to " + pool.size()+ ")" + " : "); 
        option = input.nextInt(); 
        System.out.println("Please choose your card in the pool:"); 
        System.out.println("Pool"); 
        for(int j=0; j<pool.size(); j++) 
        { 
         System.out.print("(" + j + ")" + pool.get(j) + " "); 
        } 

        for(int i=0; i<option;i++) 
        { 

          count = input.nextInt(); 
          System.out.print(pool.get(count) + " "); 
          pool.remove(count); 
          //same problem as the above error 
          **capture.add(pool.get(count));** 
        } 

        System.out.print(player.getCards().get(num) + " is selected"); 
        System.out.println(); 
        System.out.println("================================================="); 
        check=false; 
       } 
       else 
        System.out.println("Invalid Capture, Please choose either (1) Capture or (2) Discard a Card"); 
       break; 

      case 2: 
       if(play == true){ 
        Card discard = player.cardDiscard(); 
        System.out.println(discard + " has been added to pool"); 
        pool.add(discard); 
        player.removeCard(discard); 
        check=false; 
       } 
       else 
        System.out.println("Invalid Capture Please choose either (1) Capture or (2) Discard a Card"); 
       break; 

       default: 
        System.out.println("Invalid option choose"); 

      } 
     }while(check); 

     if(pass==currentPlayer) 
     { 
      passNo++; 
     } 
     else{ 
      passNo = 0; 
     } 

     if(player.getCards().size() == 0){ 
      int i = 1; 
      int [] point = new int[players.size()+1]; 
      point[0] = 100; 
      int lowest = 0; 
      System.out.println(); 

      for(Player p : players){ 
       System.out.println(p + ":" + p.getTotalScores() + " points"); 
       point[i] = p.getTotalScores(); 

       if(point[i] < point[i-1]){ 
        lowest = point[i]; 
       } 
       i++; 
      } 
      for(Player p:players){ 
       if(p.getTotalScores()==lowest) 
       { 
        player = p; 
       } 
      } 
      return player; 
      } 

     goToNextPlayer(); 
     System.out.println(); 
     System.out.println("========================================================================="); 
     System.out.println("Pool"); 
     for(int i=0; i<pool.size(); i++) 
      System.out.print("(" + i + ")" + pool.get(i) + " "); 
      return null; 
     } 

//捕獲類 import java.util.ArrayList;另一個arraylist到另一個不同類型的arraylist

 public abstract class Capture { 
       private static ArrayList<Capture> capture; 
       private static ArrayList<Card> card; 
       boolean result = false; 


public Capture() 
{ 
    // leave bank 
} 


// this part is the functions that hv to implement in pair , run , combo class 
public abstract boolean formCapture(ArrayList<Capture> c); 
public abstract double getScore(); 


public abstract void showMessage();} 

對不起,長職位,但我的問題就出在部分評論中,出現錯誤讓我無法給玩家手牌和游泳池卡添加到ArrayList<Capture> Capture名單,但不能刪除這個名單,因爲它是被另一個類用於檢測其他子類使用的其他特徵。我如何解決錯誤並將ArrayList捕獲添加到新的類型數組列表中?
**編輯,採集類已被添加,但還

+0

'Capture'和'Card'之間的關係是什麼? – shmosel

+0

您只能添加帶有Capture成員函數的列表。 – Omore

+0

你想添加Card descendats到ArrayList ?捕獲Capture的後代嗎?因爲您可以將這樣的對象添加到類型符合的泛型列表(它們具有共同的祖先或通用接口)到列表項類型中。 –

回答

0
capture.add(player.getCards().get(num)); 

無法完成在這一行

player.getCards().get(num) 

返回一個卡對象的引用。 但你decleared ArrayList中爲

private ArrayList<Capture> capture; 

所以

capture.add(); 

這種方法只能採取捕獲物的引用作爲參數,或者可能具有相同的父爲捕捉任何對象引用。因爲您正在爲它提供一個卡片對象參考並且我假定它沒有與Capture相同的界面具有相同的祖先或實現。這就是爲什麼它會給你這個錯誤。

+0

您也可以添加其他類型。只是他們應該是類型符合(他們有共同的祖先或接口)到列表項類型 –

+0

因爲他沒有給出他的完整代碼,所以我假設卡和捕獲沒有相同的祖先或接口。正如他提到的錯誤顯示「方法添加(捕獲)類型ArrayList 不適用於參數(卡)」我認爲他們沒有相同的父母 –

+0

是的它是真的。但是你寫了他可以添加Capture類型的對象。它是解決方案中的關門。 –

0

讓接口讓我們說CardAndCapture和你的卡和Capture類應該實現這個接口。現在在您的代碼中,更改此行

capture = new ArrayList();

分成

capture = new ArrayList();

現在你的代碼應該編譯並運行。你在這裏製作卡片和俘虜兄弟姐妹。因此,請確保與CardCapture接口正確集成。

0

如果Capture有卡作爲實例變量是這樣的:

public class Capture{ 
    private Card card; 

    public Capture(Card card){ 
     this.card = card; 
    } 
    ... 
} 

用法在短短如果他們有共同使用一個共同的列表

capture.add(new Capture(player.getCards().get(num))); 
0

Store對象。如果它們有共同的用法,那麼將這個功能劃分爲一個接口,並通過用於以相同方式處理的類來實現此接口。在接口上創建與此接口類型泛型列表,並做方法調用:

public interface ICommonUsage 
{ 
    public void foo(); 
    public void bar(); 
} 

public class Class1 implements ICommonUsage 
{ 
    //... 
    public void foo() {} 
    public void bar() {} 
    //... 
} 

public class Class2 implements ICommonUsage 
{ 
    //... 
    public void foo() {} 
    public void bar() {} 
    //... 
} 

public class Class3 
{ 
    //... 
    private List<ICommonUsage> commonUsedObjects; 

    public void commonUsage() 
    { 
    for (ICommonUsage item : commonUsedObjects) 
    { 
     item.foo(); 
     item.bar(); 
    } 
    } 
    //... 
} 

如果你想一個對象/接口添加到不與同級車符合通用的清單(它有沒有能力調用它的方法)編譯器會記錄一條錯誤消息。

相關問題