简体   繁体   中英

Avoid class from adding new objects on ArrayList

I have written a code for a simple card game. Every-time I draw the deck wont decrease and I don't how to avoid it. There are 24 cards on the deck. Every-time I answer Y it draw a card from the deck and remove it but it won't happen on my code.

This is my code:

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

You create a new Deck in each iteration :

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

This doesn't look right, since it would make the Deck full in each iteration.

Agreeing with the answer before me: Every iteration you make a new deck.

To solve this:

.... 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) { ....

What this will do is hold 1 deck in place all the time (gameDeck). If that deck does not exist (example: first time we run program), or if it is empty, it will create a new deck (see the if statement I added).

Why not initialize Deck deck = null; and if statement to avoid object creation in each iteration.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM