【发布时间】:2015-05-07 21:28:52
【问题描述】:
我正在尝试检测我的数组列表中 2 个对象(卡片)之间的 2 个特征之一是否相同,并将其中一个对象替换为另一个。
例如ArrayList中最后3个对象如下:
King of Diamonds
Ace of Clubs
Ace of Spades
梅花 A 和黑桃 A 的共同因素是 A 值。在现实世界中,您会将黑桃 A 移到梅花 A 上,在游戏中不再显示梅花 A。
就 ArrayList 而言,这可以通过在 arraylist 中的位置用黑桃 A 替换梅花 A 并完全删除梅花 A 来实现,ArrayList 的大小为 51(52 张牌减去我们刚刚删除了一个。
在某些情况下,下面是 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);
}
}
这是我的洗牌方法(从 .txt 文件“cards.txt”中读取所有卡片并随机排列它们的顺序):
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
}
如果之前发的一张牌具有匹配的价值(花色或价值),这是我用来移除一张牌的方法:
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
}
上述方法检测两个值中的一个是否匹配,然后从数组列表中删除之前发过的牌,而最近发过的牌在数组中取而代之(就现实生活中的棋盘本身而言,最最近发过的牌在之前发过的牌之上,在接下来的游戏中隐藏)。
我正在尝试做的类似于上面的删除方法。如果满足两个特征之一,我正在寻找替换发到 2 回合的卡片。假设数组列表现在看起来像这样,我们必须将对象 (2) 替换为对象 (0):
Ace of Clubs
King of Diamonds
Ace of Spades
假设已经发了 3 张牌,我将如何将位置 0 的对象替换为位置 2 的对象?
【问题讨论】: