public List<Card> getPayment(int pValue) { List<Card> lBankCardList = mBankArea.getCardList(); // Order cards by high to low Collections.sort(lBankCardList, Collections.reverseOrder()); // Quick search for exact value and then return that for (Card lCard : lBankCardList) { if (lCard.getMonetaryValue() == pValue) { lBankCardList.remove(lCard); return new ArrayList<>(Arrays.asList(lCard)); } } // TODO AT - if we have 10 million here and asking for 5, the 10 will be used when there could // be a 5 underneath it this needs to be more intelligent // Otherwise return high to low in that order List<Card> lPaymentCards = new ArrayList<>(); for (Card lCard : lBankCardList) { lPaymentCards.add(lCard); lBankCardList.remove(lCard); // Decrement rent required by how much the card was worth pValue = -lCard.getMonetaryValue(); if (pValue < 0) { return lPaymentCards; } } // If rent is still due which should always be the case if (pValue > 0) { // Just look sequentially for now and sell properties until rent is paid for (List<Card> lPropertyList : mPropertyArea.values()) { for (Card lCard : lPropertyList) { lPaymentCards.add(lCard); lPropertyList.remove(lCard); // Decrement rent required by how much the card was worth pValue = -lCard.getMonetaryValue(); if (pValue < 0) { return lPaymentCards; } } } } // If you run past this loop, there is no more money to give so just return the cards sold for // payment // and ignore the rest as the rules of the game state return lPaymentCards; }