无法将项目添加到ArrayList

时间:2018-05-27 00:33:36

标签: java arraylist

为我的12年级计算机科学课打造Crazy Eights游戏。我想在Deck课程中添加一张卡片到Pile课程。添加卡和删除卡工作正常,但如何将该卡添加回Deck类?尝试执行deck.add(card);时,它不会将卡添加回Deck ArrayList。

这是我的代码:

Game.java:

public class game {

    public static void main(String[] args) {
        Deck deck = new Deck();

        deck.shuffle();

        Player player1 = new Player("Chris");
        Player player2 = new Player("Emily");


        for (int i = 0; i < 8; i++) {
            player1.deck.addCard(deck.removeCard());
            player2.deck.addCard(deck.removeCard());
        }

        player1.orderCardsInHandByValue();
        player1.orderCardsInHandBySuit();


        Pile pile = new Pile();

        pile.deck.addCard(deck.removeCard());
        System.out.println("************************");
        System.out.println(pile.getTopCard());

        System.out.println("************************");
        deck.showDeck();

        pile.deck.addCard(deck.removeCard());
        System.out.println("************************");
        System.out.println(pile.getTopCard());
    }

}

Card.java:

class Card {
    private int value = 0;
    private String suit = null;

    public Card(int value, String suit) {
        this.value = value;
        this.suit = suit;
    }

    int getValue() {
        return value;
    }

    String getSuit() {
        return suit;
    }

    public String toString()
    {
        return (this.getSuit() + " " + this.getValue());
    }
}

Player.java:

class Player {
    private String name = null;

    Deck deck = new Deck();

    public Player(String name) {
        this.name = name;
        this.deck.clear();
    }

    void orderCardsInHandByValue() {
        Card temp = null;
        for (int i = 1; i < 8; i++) {
            for (int j = i; j > 0; j--) {
                if(this.deck.get(j).getValue() < this.deck.get(j - 1).getValue()) {
                    temp = this.deck.get(j);
                    this.deck.set(j, this.deck.get(j - 1));
                    this.deck.set(j - 1, temp);
                }
            }
        }
    }

    void orderCardsInHandBySuit() {
        Card temp = null;
        for(int i = 1; i < 8; i++) {
            for(int j = i; j > 0; j--) {
                if(this.deck.get(j).getSuit().compareTo(this.deck.get(j - 1).getSuit()) < 0) {
                    temp = this.deck.get(j);
                    this.deck.set(j, this.deck.get(j - 1));
                    this.deck.set(j - 1, temp);
                }
            }
        }
    }
}

Pile.java(这是我在尝试将提供给Pile类的卡添加回Deck类时遇到问题的类。桩类用于抓住顶层卡片来自甲板。):

class Pile extends Deck {

    Deck deck = new Deck();
    Card card = null;

    public Pile() {
        this.deck.clear();
    }

    Card getTopCard() {
        card = this.deck.get(0);
        this.deck.remove(0);
        deck.addCard(card);
        return card;
    }
}

Deck.java:

class Deck extends ArrayList<Card> {
    private String[] suits = {"Clubs", "Diamonds", "Hearts", "Spades"};
    private String[] ranks = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "1"};

    public Deck() {
        for (int i = 0; i < ranks.length; i++) {
            for (int j = 0; j < suits.length; j++) {
                this.add(new Card(Integer.parseInt(ranks[i]), suits[j]));
            }
        }
    }

    void shuffle() {
        Random random = new Random();
        for (int i = 0; i < 52 - 1; i++) {
            int randomValue = i + random.nextInt(52 - i);
            Card card = this.get(i);
            this.set(i, this.get(randomValue));
            this.set(randomValue, card);
        }
    }

    void showDeck() {
        for (int i = 0; i < this.size(); i++) {
            System.out.println(this.get(i));
        }
    }

    void clearDeck()
    {
        this.clear();
    }

    Card addCard(Card cardToAdd) {
        this.add(cardToAdd);
        return cardToAdd;
    }

    Card removeCard() {
        Card card = (Card) this.get(0);
        this.remove(0);
        return card;
    }
}

我的代码没有很好的设计,主要是因为我必须遵循老师提出的某些要求。

1 个答案:

答案 0 :(得分:1)

此...

class Pile extends Deck {

    Deck deck = new Deck();

这......

class Deck extends ArrayList<Card> {

让所有人感到困惑。

根本没有理由。 PileDeck的实例,因此您实际上不需要单独的类,只需创建Deck的实例并将其称为“桩”

扩展ArrayList是不明智的。你没有在类中添加任何“附加”功能(基类中尚不存在)并且它只会让你感到困惑,因为你一直在调用ArrayList的方法和新的类相同的功能(这就是为什么Pile如此搞砸了)

所以,如果相反,我们摆脱了Pile并使Deck成为ArrayList的容器,我们得到的内容更像......

import java.util.ArrayList;
import java.util.Collections;

public class Game {

    public static void main(String[] args) {
        Deck deck = new Deck();

        deck.shuffle();

        Player player1 = new Player("Chris");
        Player player2 = new Player("Emily");

        for (int i = 0; i < 8; i++) {
            player1.deck.addCard(deck.popToCard());
            player2.deck.addCard(deck.popToCard());
        }

        player1.orderCardsInHandByValue();
        player1.orderCardsInHandBySuit();

        Deck pile = new Deck();
        pile.clearDeck();

        for (int index = 0; index < 2; index++) {
            Card popped = deck.popToCard();
            pile.addCard(popped);
            System.out.println("Popped " + popped + " from deck, added to pile");
            System.out.println("Pile's top card = " + pile.getTopCard());

            System.out.println("Deck contains...");
            deck.showDeck();
            System.out.println("...Deck contains");

            System.out.println("Pile contains...");
            pile.showDeck();
            System.out.println("...Pile contains");
        }
    }

    static class Card {

        private int value = 0;
        private String suit = null;

        public Card(int value, String suit) {
            this.value = value;
            this.suit = suit;
        }

        int getValue() {
            return value;
        }

        String getSuit() {
            return suit;
        }

        public String toString() {
            return (this.getValue() + " of " + this.getSuit());
        }
    }

    static class Player {

        private String name = null;

        Deck deck = new Deck();

        public Player(String name) {
            this.name = name;
            this.deck.clearDeck();
        }

        void orderCardsInHandByValue() {
            Card temp = null;
            for (int i = 1; i < 8; i++) {
                for (int j = i; j > 0; j--) {
                    if (this.deck.get(j).getValue() < this.deck.get(j - 1).getValue()) {
                        temp = this.deck.get(j);
                        this.deck.set(j, this.deck.get(j - 1));
                        this.deck.set(j - 1, temp);
                    }
                }
            }
        }

        void orderCardsInHandBySuit() {
            Card temp = null;
            for (int i = 1; i < 8; i++) {
                for (int j = i; j > 0; j--) {
                    if (this.deck.get(j).getSuit().compareTo(this.deck.get(j - 1).getSuit()) < 0) {
                        temp = this.deck.get(j);
                        this.deck.set(j, this.deck.get(j - 1));
                        this.deck.set(j - 1, temp);
                    }
                }
            }
        }
    }

    static class Deck {

        private String[] suits = {"Clubs", "Diamonds", "Hearts", "Spades"};
        private String[] ranks = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "1"};

        private ArrayList<Card> cards;

        public Deck() {
            cards = new ArrayList<>(52);
            for (int i = 0; i < ranks.length; i++) {
                for (int j = 0; j < suits.length; j++) {
                    cards.add(new Card(Integer.parseInt(ranks[i]), suits[j]));
                }
            }
        }

        void shuffle() {
            Collections.shuffle(cards);
//          Random random = new Random();
//          for (int i = 0; i < 52 - 1; i++) {
//              int randomValue = i + random.nextInt(52 - i);
//              Card card = this.get(i);
//              this.set(i, this.get(randomValue));
//              this.set(randomValue, card);
//          }
        }

        void showDeck() {
            for (Card card : cards) {
                System.out.println("\t" + card);
            }
        }

        void clearDeck() {
            cards.clear();
        }

        Card addCard(Card cardToAdd) {
            cards.add(cardToAdd);
            return cardToAdd;
        }

        Card popToCard() {
            return cards.remove(0);
        }

        public Card get(int index) {
            return cards.get(index);
        }

        public Card getTopCard() {
            return cards.get(0);
        }

        public void set(int index, Card card) {
            cards.set(index, card);
        }
    }

}

输出类似......

Popped 4 of Spades from deck, added to pile
Pile's top card = 4 of Spades
Deck contains...
    10 of Diamonds
    1 of Spades
    // Lots of cards, removed for brevity 
...Deck contains
Pile contains...
    4 of Spades
...Pile contains
Popped 10 of Diamonds from deck, added to pile
Pile's top card = 4 of Spades
Deck contains...
    1 of Spades
    // Lots of cards, removed for brevity 
...Deck contains
Pile contains...
    4 of Spades
    10 of Diamonds
...Pile contains
  

我最初在不扩展ArrayList的情况下编写了Deck类,并创建了一个私有的ArrayList变量。但是,我的老师告诉我按他的方式去做,这就是我在这里给你看的。甲板构造函数,由我的老师完成,而不是我。我不能丢弃桩类,因为它是我的老师告诉我必须遵循的设计地图的一部分。设计图有五个类。 Deck是父类,其次是Pile作为子类。 Card类和Player类不会从Deck或Pile扩展/实现/继承任何东西。游戏类是具有主要功能的类。

我支持我先前的陈述,但由于我们别无选择,因为与老师争论的方式就像是与没有技术专长的客户争论

public class Game {

    public static void main(String[] args) {
        new Game();
    }

    public Game() {
        Deck deck = new Deck();

        deck.shuffle();

        Player player1 = new Player("Chris");
        Player player2 = new Player("Emily");

        for (int i = 0; i < 8; i++) {
            player1.deck.add(deck.popTopCard());
            player2.deck.add(deck.popTopCard());
        }

        player1.orderCardsInHandByValue();
        player1.orderCardsInHandBySuit();

        Pile pile = new Pile();

        for (int index = 0; index < 4; index++) {
            System.out.println("Deck:");
            deck.showDeck();
            System.out.println("Pile:");
            pile.showDeck();

            Card popped = deck.popTopCard();
            pile.add(popped);
            System.out.println("\nPopped " + popped + " from deck, added to pile");
        }
        System.out.println("Deck:");
        deck.showDeck();
        System.out.println("Pile:");
        pile.showDeck();
    }

    class Deck extends ArrayList<Card> {

        private String[] suits = {"Clubs", "Diamonds", "Hearts", "Spades"};
        private String[] ranks = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "1"};

        public Deck() {
            for (int i = 0; i < ranks.length; i++) {
                for (int j = 0; j < suits.length; j++) {
                    this.add(new Card(Integer.parseInt(ranks[i]), suits[j]));
                }
            }
        }

        void shuffle() {
            Collections.shuffle(this);
        }

        void showDeck() {
            for (int i = 0; i < this.size(); i++) {
                System.out.println("\t" + this.get(i));
            }
        }

        public Card popTopCard() {
            if (isEmpty()) {
                return null;
            }
            return remove(0);
        }

        public Card peekTopCard() {
            if (isEmpty()) {
                return null;
            }
            return get(0);
        }

    }

    class Pile extends Deck {

        public Pile() {
            this.clear();
        }
    }

    class Card {

        private int value = 0;
        private String suit = null;

        public Card(int value, String suit) {
            this.value = value;
            this.suit = suit;
        }

        int getValue() {
            return value;
        }

        String getSuit() {
            return suit;
        }

        public String toString() {
            return (this.getSuit() + " " + this.getValue());
        }
    }

    class Player {

        private String name = null;

        Deck deck = new Deck();

        public Player(String name) {
            this.name = name;
            this.deck.clear();
        }

        void orderCardsInHandByValue() {
            Card temp = null;
            for (int i = 1; i < 8; i++) {
                for (int j = i; j > 0; j--) {
                    if (this.deck.get(j).getValue() < this.deck.get(j - 1).getValue()) {
                        temp = this.deck.get(j);
                        this.deck.set(j, this.deck.get(j - 1));
                        this.deck.set(j - 1, temp);
                    }
                }
            }
        }

        void orderCardsInHandBySuit() {
            Card temp = null;
            for (int i = 1; i < 8; i++) {
                for (int j = i; j > 0; j--) {
                    if (this.deck.get(j).getSuit().compareTo(this.deck.get(j - 1).getSuit()) < 0) {
                        temp = this.deck.get(j);
                        this.deck.set(j, this.deck.get(j - 1));
                        this.deck.set(j - 1, temp);
                    }
                }
            }
        }
    }
}

其中输出类似......

Deck:
    Clubs 4
    Diamonds 3
    Hearts 4
    Clubs 12
    Hearts 2
    Hearts 9
    Spades 12
    Clubs 6
    Spades 4
    Hearts 6
    Clubs 7
    Diamonds 8
    Spades 8
    Diamonds 7
    Hearts 7
    Hearts 10
    Diamonds 11
    Spades 7
    Clubs 3
    Spades 10
    Clubs 1
    Spades 3
    Clubs 5
    Spades 1
    Diamonds 9
    Clubs 10
    Spades 13
    Hearts 11
    Clubs 13
    Clubs 9
    Spades 11
    Hearts 13
    Hearts 5
    Hearts 12
    Spades 2
    Clubs 11
Pile:
Popped Clubs 4 from deck, added to pile
Deck:
    Diamonds 3
    Hearts 4
    Clubs 12
    Hearts 2
    Hearts 9
    Spades 12
    Clubs 6
    Spades 4
    Hearts 6
    Clubs 7
    Diamonds 8
    Spades 8
    Diamonds 7
    Hearts 7
    Hearts 10
    Diamonds 11
    Spades 7
    Clubs 3
    Spades 10
    Clubs 1
    Spades 3
    Clubs 5
    Spades 1
    Diamonds 9
    Clubs 10
    Spades 13
    Hearts 11
    Clubs 13
    Clubs 9
    Spades 11
    Hearts 13
    Hearts 5
    Hearts 12
    Spades 2
    Clubs 11
Pile:
    Clubs 4
Popped Diamonds 3 from deck, added to pile
Deck:
    Hearts 4
    Clubs 12
    Hearts 2
    Hearts 9
    Spades 12
    Clubs 6
    Spades 4
    Hearts 6
    Clubs 7
    Diamonds 8
    Spades 8
    Diamonds 7
    Hearts 7
    Hearts 10
    Diamonds 11
    Spades 7
    Clubs 3
    Spades 10
    Clubs 1
    Spades 3
    Clubs 5
    Spades 1
    Diamonds 9
    Clubs 10
    Spades 13
    Hearts 11
    Clubs 13
    Clubs 9
    Spades 11
    Hearts 13
    Hearts 5
    Hearts 12
    Spades 2
    Clubs 11
Pile:
    Clubs 4
    Diamonds 3
Popped Hearts 4 from deck, added to pile
Deck:
    Clubs 12
    Hearts 2
    Hearts 9
    Spades 12
    Clubs 6
    Spades 4
    Hearts 6
    Clubs 7
    Diamonds 8
    Spades 8
    Diamonds 7
    Hearts 7
    Hearts 10
    Diamonds 11
    Spades 7
    Clubs 3
    Spades 10
    Clubs 1
    Spades 3
    Clubs 5
    Spades 1
    Diamonds 9
    Clubs 10
    Spades 13
    Hearts 11
    Clubs 13
    Clubs 9
    Spades 11
    Hearts 13
    Hearts 5
    Hearts 12
    Spades 2
    Clubs 11
Pile:
    Clubs 4
    Diamonds 3
    Hearts 4
Popped Clubs 12 from deck, added to pile
Deck:
    Hearts 2
    Hearts 9
    Spades 12
    Clubs 6
    Spades 4
    Hearts 6
    Clubs 7
    Diamonds 8
    Spades 8
    Diamonds 7
    Hearts 7
    Hearts 10
    Diamonds 11
    Spades 7
    Clubs 3
    Spades 10
    Clubs 1
    Spades 3
    Clubs 5
    Spades 1
    Diamonds 9
    Clubs 10
    Spades 13
    Hearts 11
    Clubs 13
    Clubs 9
    Spades 11
    Hearts 13
    Hearts 5
    Hearts 12
    Spades 2
    Clubs 11
Pile:
    Clubs 4
    Diamonds 3
    Hearts 4
    Clubs 12
相关问题