避免类在ArrayList上添加新对象

时间:2014-08-24 20:42:44

标签: java arrays arraylist

我为一个简单的纸牌游戏编写了代码。每次我画甲板都不会减少,我不知道如何避免它。甲板上有24张牌。每当我回答Y时,它会从牌组中抽出一张牌并将其删除,但它不会在我的代码上发生。

这是我的代码:

Opportunity.java

package opportunity;
import java.io.*;

public class Opportunity {
    /*Player 1 */
    public int p1_money = 10000;
    public int p1_card_d = 40;
    public int p1_card_h = 0;
    /*Player 2 */
    public int p2_money = 10000;
    public int p2_card_d = 40;
    public int p2_card_h = 0;
    public int xyz = 0;

    /**
     * @param args the command line arguments
     * @throws java.io.IOException
     */
    public static void main(String[] args) throws IOException {
        System.out.print("***************Opportunity***************\n");
        while (true) {
            System.out.println("Do you want to draw a card? Y/N");
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String s = br.readLine();
            if ("Y".equals(s)) {
                Deck deck = new Deck();
            if (deck.card_deck.get(0).getEnum() == Card.CardType.EVENT) {
                System.out.println("Name: "+deck.card_deck.get(0).getName());
                System.out.println("Cost: "+deck.card_deck.get(0).getCost());
                System.out.println("Effect: "+deck.card_deck.get(0).getProperty("Effect"));
                deck.card_deck.remove(0);
                System.out.println(deck.card_deck.size());
            }
            else if(deck.card_deck.get(0).getEnum() == Card.CardType.ASSET) {
                System.out.println("Name: "+deck.card_deck.get(0).getName());
                System.out.println("Purchase Cost: "+deck.card_deck.get(0).getCost());
                System.out.println("Income: "+deck.card_deck.get(0).getProperty("income"));
                System.out.println("Start Selling Price: "+deck.card_deck.get(0).getProperty("ssp"));
                System.out.println("Selling Price Growth: "+deck.card_deck.get(0).getProperty("spg")+" per turn");
                deck.card_deck.remove(0);
                System.out.println(deck.card_deck.size());
            }
            } else {
               System.out.println("Goodbye!");
               break;
            }
        }
    }
}

Card.java:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package opportunity;

import java.util.*;


/**
 *
 * @author ASUS
 */
public class Card {
    public enum CardType {
        EVENT,
        PROPERTY,
        ASSET;
    }
    private final CardType cardType;
    private final String cardName;
    private final Double cardCost;
    private final Map<String, String> properties = new HashMap<>();

    Card(final CardType cardType, final String cardName, final Double cardCost) {
        this.cardType = cardType;
        this.cardName = cardName;
        this.cardCost = cardCost;
    }

    public Card setProperty(final String name, final String value) {
        properties.put(name, value);
        return this;
    }
    public String getName() {
        return cardName;
    }
    public Double getCost() {
        return cardCost;
    }
    public Enum getEnum() {
        return cardType;
    }
    public String getProperty(final String name) {
        return properties.get(name);
    }
}

Deck.java

package opportunity;

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


public class Deck {
public List<Card> card_deck;
    private int x;

    public Deck() {
        if(x == 0) {
            final Card card1 = new Card(Card.CardType.EVENT, "Get Tax Returns",0.00).
            setProperty("Effect", "Earn money equal to the\n"
                    + "maximum income each of your\n"
                    + "properties can give you,\n"
                    + "depending on their level.");
            final Card card2 = new Card(Card.CardType.EVENT, "BIR Hunting Begins",0.00).
            setProperty("cost", "0.00").
            setProperty("Effect", "An opponent loses\n"
                    + "money equal to 50% of the\n"
                    + "maximum income each of\n"
                    + "their properties can give him or her,\n"
                    + "depending on the level of the\n"
                    + "property.");
            final Card card3 = new Card(Card.CardType.EVENT, "Restore Balance",10000.00).
            setProperty("Effect", "The total income of all\n"
                    + "the players becomes equal to\n"
                    + "the income of the player\n"
                    + "with the lowest income.");
            final Card card4 = new Card(Card.CardType.ASSET, "Vacant Property Lot",7500.00).
                setProperty("income", "1000.00").
                setProperty("ssp", "3250.00").
                setProperty("spg", "5000.00");
            final Card card5 = new Card(Card.CardType.ASSET, "Stock Investment",9000.00).
                setProperty("income", "500.00").
                setProperty("ssp", "4500.00").
                setProperty("spg", "10000.00");
            final Card card6 = new Card(Card.CardType.ASSET, "Time Investment Account",15000.00).
                setProperty("income", "100.00").
                setProperty("ssp", "7500.00").
                setProperty("spg", "15000.00");

                card_deck = new ArrayList<>();
                for (int i=0; i<4; i++) {
                    card_deck.add(card1);
                    card_deck.add(card2);
                    card_deck.add(card3);
                    card_deck.add(card4);
                    card_deck.add(card5);
                    card_deck.add(card6);
                }
                Collections.shuffle(card_deck);
            }
    }
    public List getList(){
        x = 1;
        return card_deck;
    }
}

3 个答案:

答案 0 :(得分:2)

您在每次迭代中创建一个新的Deck:

        if ("Y".equals(s)) {
            Deck deck = new Deck();

这看起来并不正确,因为它会在每次迭代中使甲板变满。

答案 1 :(得分:0)

在我之前同意答案: 每次迭代你都会成为一个新的套牌。

解决这个问题:

....     public int xyz = 0;

public Deck gameDeck;
/**
 * @param args the command line arguments
 * @throws java.io.IOException
 */

....             if(“Y”.equals(s)){                 if(gameDeck == null || gameDeck.card_deck.size()== 0){

gameDeck = new Deck();}             if(deck.card_deck.get(0).getEnum()== Card.CardType.EVENT){ ....

这将做的是始终保持1个牌组(gameDeck)。如果那个牌组不存在(例如:我们第一次运行程序),或者如果它是空的,它将创建一个新的牌组(参见我添加的if语句)。

答案 2 :(得分:0)

为什么不初始化Deck deck = null;if语句以避免在每次迭代中创建对象。

public static void main(String[] args) throws IOException {
        System.out.print("***************Opportunity***************\n");
        int setDeck = 0;
        Deck deck = null;
        while (true) {
            System.out.println("Do you want to draw a card? Y/N");
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String s = br.readLine();
            if ("Y".equals(s)) {
                if (setDeck == 0) {
                    deck = new Deck();
                }
            if (deck.card_deck.get(0).getEnum() == Card.CardType.EVENT) {
                System.out.println("Name: "+deck.card_deck.get(0).getName());
                System.out.println("Cost: "+deck.card_deck.get(0).getCost());
                System.out.println("Effect: "+deck.card_deck.get(0).getProperty("Effect"));
                deck.card_deck.remove(0);
                System.out.println(deck.card_deck.size());
            }
            else if(deck.card_deck.get(0).getEnum() == Card.CardType.ASSET) {
                System.out.println("Name: "+deck.card_deck.get(0).getName());
                System.out.println("Purchase Cost: "+deck.card_deck.get(0).getCost());
                System.out.println("Income: "+deck.card_deck.get(0).getProperty("income"));
                System.out.println("Start Selling Price: "+deck.card_deck.get(0).getProperty("ssp"));
                System.out.println("Selling Price Growth: "+deck.card_deck.get(0).getProperty("spg")+" per turn");
                deck.card_deck.remove(0);
                System.out.println(deck.card_deck.size());
            }
            setDeck = 1;
            } else {
               System.out.println("Goodbye!");
               break;
            }
        }
    }