java - 另一个数组列表到另一个不同类型的数组列表

标签 java oop arraylist

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

//捕获类 导入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中列表,但无法删除此列表,因为它正在另一个类中用于检测其他子类使用的其他功能。如何解决该错误并将数组列表捕获添加到新类型数组列表中?
**已编辑,Capture 类已添加但尚未完成

最佳答案

创建一个接口(interface),假设 CardAndCapture 和您的 Card 和 Capture 类应该实现此接口(interface)。现在在您的代码中,更改此行

捕获=新的ArrayList();

进入

捕获=新的ArrayList();

现在您的代码应该可以编译并运行。你在这里让 Card 和 Capture 成为 sibling 。因此,请确保与 CardCapture 接口(interface)正确集成。

关于java - 另一个数组列表到另一个不同类型的数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43055088/

相关文章:

Android Studio Recyclerview 使用 Retrofit

java - Junit Mockito 初始化错误

java - TableRow 不使用 setOnKeyPressed

java - 在 Spring MVC 中重命名返回到 ResponseBody 的 JSON 对象

java - 如何设置嵌套表格无边框?

php - 类似于 CLOS 的 PHP 对象模型

java - 如何从静态嵌套类修改外部类字段?

language-agnostic - 序列图能否以与代码相同的深度真实地捕捉您的逻辑?

java - 与 ArrayList 的合并排序

java - ArrayList 对象分配给 LSt 对象内部工作