2017-03-27 11 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;別のアーリーリストから別のタイプの別のアライリストへ

 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

「キャプチャ」と「カード」の関係は何ですか? – shmosel

+0

リストには、キャプチャメンバー機能のみを追加できます。 – Omore

+0

ArrayList にカードの子孫を追加しますか?キャプチャの子孫もキャプチャしますか?そのようなオブジェクトをリストに追加することができるのは、一般的な祖先または共通のインタフェースを持つ正規リストにリスト項目タイプを追加するだけです。 –

答えて

0
capture.add(player.getCards().get(num)); 
このラインで

player.getCards().get(num) 

を完了していないがカードのオブジェクト参照を返しています。 しかし、あなたは

private ArrayList<Capture> capture; 

としてのArrayListをdeclearedので

capture.add(); 

このメソッドは、パラメータとしてキャプチャオブジェクト参照またはキャプチャと同じ親を持つかもしれないオブジェクト参照を取ることができます。あなたはそれにカードオブジェクトの参照を提供しています。そして、私はそれを仮定しています。カードは、キャプチャと同じインタフェースからの同じ祖先または実装を持っていません。それがあなたにそのエラーを与えている理由です。

+0

他のタイプも追加できます。リスト項目のタイプに一致する型(共通の祖先またはインタフェースを持つ)を入力する必要があります。 –

+0

彼は完全なコードを与えなかったので、カードとキャプチャには同じ祖先やインタフェースがないと仮定しています。彼が述べたように、 "ArrayList 型のメソッドadd(キャプチャ)は引数(カード)には適用されません"と私は彼らが同じ親を持っていないと思います –

+0

そうです。しかし、彼はタイプキャプチャのオブジェクトを追加することができるだけだと書いています。それは解決策からのドアを閉じています。 –

0

インターフェイスの作成CardAndCaptureと、カードとキャプチャクラスでこのインターフェイスを実装する必要があります。今すぐあなたのコードで、この行を変更してください。

キャプチャ=新しいArrayList();

捕捉=新しいArrayListを()内に

これでコードをコンパイルして実行する必要があります。あなたはカードと兄弟をここに作ります。したがって、CardCaptureインターフェイスと正しく統合するようにしてください。

0

キャプチャは、このようなインスタンス変数としてカードを持っている場合:彼らは一般的な使用を持っているだけであれば、共通のリストで

public class Capture{ 
    private Card card; 

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

使用

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

ストアオブジェクト。共通の使用法がある場合は、この機能をインタフェースに分割し、同じ方法で処理するクラスによってこのインタフェースを実装します。インターフェイス上でメソッド呼び出しを、このインターフェイスタイプと一般的なリストを作成し、実行します。

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

を使用すると、そのクラスに準拠していない一般的なリストにオブジェクト/インターフェースを追加したい場合は(それは能力をしていませんそのメソッドを呼び出すために)、コンパイラはエラーメッセージを記録します。

関連する問題