1

I'm trying to detect if one of 2 characteristics between 2 objects (cards) in my arraylist are the same, and the replace one of the objects with the other.

For example the last 3 objects in the ArrayList are as follows:

King of Diamonds
Ace of Clubs
Ace of Spades

The common factor between the Ace of Clubs and the Ace of Spades is the Ace value. In the real world you would then move the Ace of Spades onto the Ace of Clubs, never showing the Ace of Clubs again during the game.

In terms of the ArrayList, this is achievable by replacing the Ace of Clubs with the Ace of Spades at the position in the arraylist and removing the Ace of Clubs entirely, leaving the ArrayList with a size of 51 (52 cards minus the one we just removed.

For some context, here is how the Card class looks, which holds the characteristics of each card:

package uk.ac.aber.dcs.cs12320.cards;

import java.util.ArrayList;


public class Card {

protected String value;
protected String suit;
 ArrayList<Card> cardsList = new ArrayList<>();

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

public Card() {

}

public  Card[] getAll(){
    Card[] brb = new Card[cardsList.size()];
    int tempCount = -1;
    for(Card c : cardsList){
        tempCount++;
        brb[tempCount] = c;
    }
    return brb;
}

public  void deleteAll(){
    cardsList.clear();
}

public  String getValue() {
    return value;
}

public void setValue(String value) {
    this.value = value;
}

public  String getSuit() {
    return suit;
}

public void setSuit(String suit) {
    this.suit = suit;
}

public void addCard(Card card){
    cardsList.add(card);
}

}

Here is my shuffle method (takes all of the cards that have been read in from the .txt file "cards.txt" and randomizes their order):

    private void dealCard(){
    //TODO
    int totalLeftOver = 0; // used to count the cards left in the shuffled-but-not-dealt pack
    Card topCard = shuffledPack.get(0);
    shuffledPack.remove(0);
    theFlop.add(topCard);
    System.out.print("Cards on the flop: ");
    for(Card dealt : theFlop){
        String definitelyDealt = dealt.getValue() + dealt.getSuit() + " ";
        System.out.print(definitelyDealt);
    }
    System.out.println("\n");
    for(Card card : shuffledPack){ // for loop to count how cards haven't been dealt
        totalLeftOver++;
    }
    System.out.println("Total number of cards left to deal: " + totalLeftOver); // show how many cards haven't been dealt to the player
}

Here's the method I use to remove one card if the previously dealt one has a matching value (suit or value):

    private void makeMovePreviousPile(){
    int lastDealtCardPos = theFlop.size() - 1; //allows us to see how many cards have been dealt, are you even trying to challenge us Chris?
    int previouslyDealtCardPos = lastDealtCardPos - 1;

    if(lastDealtCardPos != 0){ // check that the deck has been shuffled and at least 1 card has been dealt.

        String lastDealtCardValue = theFlop.get(lastDealtCardPos).getValue(); // fetches the value of the last dealt card
        String lastDealtCardSuit = theFlop.get(lastDealtCardPos).getSuit(); // fetches the suit of the last dealt card
        String previouslyDealtCardValue = theFlop.get(previouslyDealtCardPos).getValue(); // fetches the 2nd to last dealt card's value
        String previouslyDealtCardSuit = theFlop.get(previouslyDealtCardPos).getSuit(); // fetches the 2nd to last dealt card's suit

        if(lastDealtCardValue.equals(previouslyDealtCardValue)){
            theFlop.remove(previouslyDealtCardPos);
        }
        else if(lastDealtCardSuit.equals(previouslyDealtCardSuit)) {
            theFlop.remove(previouslyDealtCardPos);
        }
        else {
            System.out.println("Cannot make a move. Are you sure you know the rules?");
        }
        System.out.println("\n");
        printCardsFromFlop();
    }
    else { // if it hasn't been shuffled we shun the user.
        System.out.println("Are you sure you shuffled the deck and dealt a card before trying to make a move?");
    }
    System.out.print("Total cards on the flop: " + lastDealtCardPos + "\n"); // checking to see that its working as intended
}

The above method detects if one of the two values match, then removes the previously dealt card from the arraylist and the most recently dealt card takes its place in the arraylsit (and in terms of the board itself in real life, the most recently dealt card goes on top of the previously dealt one, hiding it for the rest of the game).

What I'm trying to do is similar to the removal method above. I'm looking to replace the card that was dealt 2 turns back if one of the two characteristics are met. Let's assume that the arraylist now looks like this, and we have to replace object (2) with object (0):

Ace of Clubs
King of Diamonds
Ace of Spades

How would I go about replacing the object at position 0 with the object at position 2, assuming 3 cards had been dealt?

2 Answers 2

1

Code below is using hard indexes of 0 and 2 as per your question. It is upto you to decide how to safeguard against exceptions there,

Replace like this

cardsList.set(0, cardsList.get(2));

If you want to remove 2 then use this two

cardsList.remove(2);
Sign up to request clarification or add additional context in comments.

3 Comments

Would that then move the object in position 2 in the ArrayList to position 0? Or would it push object 0 up to position 1 and then set object (2) to position 0? if that makes sense.
It will replace the object at position zero with object at position 2. Is that what you want ? Also to your comment in other asnwer. Try theFlop.set(theFlop.indexOf(TwoCardsBackDealtPos), theFlop.get(lastDealtCardPosition));
Tried and tested, worked perfectly. Without the remove it simply replaced the value but kept the existing one in position 2, adding in the remove took it out of the array. Thanks a lot, marking your answer as correct. Cheers!
1

Better approach:

Collections.swap(list, list.indexOf(firstCardToBeReplaced), list.indexOf(secondCardToBeReplaced));

Or you can use the method "set(int index, E element)" from ArrayList.

int firstPosition = list.indexOf(firstCardToBeReplaced);
int secondPosition = list.indexOf(secondCardToBeReplaced);
list.set(firstPosition, secondCardToBeReplaced);
list.set(secondPosition, firstCardToBeReplaced);

It will replace at position of the object by the new element.

2 Comments

Thanks, I've put in the following: theFlop.set(theFlop.indexOf(TwoCardsBackDealtPos), lastDealtCardPosition); which has now given me the error: The method set(int, Card) in the type ArrayList<Card> is not applicable for the arguments (int, int), would you be able to explain why this is occurring in plain terms so I can figure out a solution please?
You have to put the object to be replaced on the second parameter. I edited my code, I think that will be easier to understand.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.