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?