如何从甲板上取下卡片

时间:2016-10-20 00:24:00

标签: java arrays oop object

我试图找到一种方法来删除卡片中的特定卡片或随机卡片并返回已删除的卡片。我已经为Card和DeckHand创建了类。在DeckHand类中,我尝试创建一个允许用户选择卡值的删除方法,从卡片组中删除该卡片值的一个实例,然后返回已删除的卡片,最后缩短我还试图制作一个deleteAny方法,从牌组中删除一张随机牌,返回已删除的牌,并将数组缩短为1.

对于删除方法,我无法找到说出来的方法:

*如果用户输入的值不在录像机中,则打印错误信息。 *如果是,则找到具有该值的卡的第一个实例,删除它,然后返回卡。

我不明白如何找到具有该值的卡的第一个实例,然后找到一种方法来设置可用的套装以创建卡的实例然后将其删除并移动阵列中的位置。

我开始尝试删除随机卡的deleteAny方法。我能够将卡片输出提供给被删除的用户,但我的方法出错了。有什么想法吗?

卡类:

class Card {
private int _value, _suit;
private String[] _cardValues = {null, "Ace", "2", "3", "4","5", "6", "7", 
    "8", "9", "10", "Jack", "Queen", "King"};
private String[] _cardSuits = {null, "Clubs", "Diamonds", "Hearts", "Spades"};

public Card(int value,int suit) {
    _value = value;
    _suit = suit;
}
public int getCardValue() {
    return _value;
}
public int getCardSuit() {
    return _suit;
}
public String toString() {
    return  _cardValues[_value] + " of " + _cardSuits[_suit];
}

} 甲板课程:

class DeckHand{
        private Card[] _deckHand;
        private int _deckSize;
        private static final int MAXSIZE = 52;
        private Card[] newDeck;

        public DeckHand() {
           _deckHand = new Card[MAXSIZE];
           _deckSize = 0;
        }
        public DeckHand(int deckSize) {
           _deckHand = new Card[MAXSIZE];
           int index = 0;
           for (int suit = 1; suit <= 4; suit++) {
              for (int rank = 1; rank <= 13; rank++) {
                  _deckHand[index] = new Card(rank, suit);
                  index++;
              }
           }
       _deckSize = deckSize;
       }
 //Here's the delete method, but I have no idea what I'm doing here.
       public void delete(int value) {
          for (int i = 0; i<_deckSize; i++) {
              if(_deckHand[i].getCardValue()==value) {
                  _deckHand[value] = _deckHand[_deckSize-1];
                  newDeck = new Card[_deckHand.length-1];
          } else
            System.out.println("\n--------------------------------------"
                    + "\nThe deck does not contain that value"
                    + "\n--------------------------------------");
          }
       }
 //Here's the deleteAny method, but I'm getting an error
    public void deleteAny(Card newCard) {
    if(_deckSize >= MAXSIZE) {
        newDeck = new Card[_deckHand.length-1];
        for(int i = 0; i<_deckSize; ++i)
            if(_deckHand[i].equals(newCard)) {
                newDeck[i] = _deckHand[i];
            }
        _deckHand = newDeck;
    }
//the error says it has to do with this next line
    _deckHand[_deckSize-1] = newCard;
    _deckSize-=1;
}
}

主: 这是我使用这些delete和deleteAny方法的主要方法的一部分:

                    case 3:
                        System.out.println("\nWhich card would you "
                                + "like to remove from the deck?");
                        valueOption();
                        System.out.print("\tOption: ");
                        value = keyboard.nextInt();

                        if(pickDeck == 1) {
                            standard.delete(value);
                        } else {
                            System.out.println("\n-------------------------"
                                    + "-------------------------------\n"
                                    + "The card value \"" + values[value]
                                    + "\" appears "
                                    + empty.count(value)
                                    + " times in the deck."
                                    + "\n---------------------------------"
                                    + "-----------------------");
                        }
                        break;
                    case 4:
                        Random generator = new Random();
                        value = generator.nextInt(13)+1;
                        suit = generator.nextInt(4)+1;
                        newCard = new Card(value,suit);
                        System.out.println("\n--------------------------"
                                + "---------------------"
                                + "\n" + newCard + " was removed from the "
                                + "deck."
                                + "\n--------------------------"
                                + "---------------------");
                        if(pickDeck==1) 
                            standard.deleteAny(newCard);
                        else
                            empty.deleteAny(newCard);

                        break;

2 个答案:

答案 0 :(得分:0)

我的回答使用了上面的大部分方法。我已经调整它以包含检查我们之前是否找到该值的方法。

public Card delete(int value) {
      Card result = new Card(-1,-1);               // Starter card to check if value has been found.
      newDeck = new Card[_deckHand.length-1]
      int location = -1                            // Initial location. This changes once we find the value.
      for (int i = 0; i<_deckHand.length; i++) {
          if(_deckHand[i].getCardValue()==value) { // We've found a match
              if(result.value==-1){                // Check if our starter card still has its original value
                  result = new Card(_deckHand[i].getCardValue(),_deckHand[i].getCardSuit());
                  location = i;                    // Adjust location
              }
      } 
      // make a helper that does the rest. That way you can delete any card from the deck.

      if(location != -1){                          // See if location has been adjusted (i.e. value has been found)
          for(int i = 0; i < location; i++){       // Copy the remnants of _deckHand to newDeck
              newDeck[i]=_deckHand[i];
          }
          for(int j = location+1; j<_deckHand.length-1; j++){
              newDeck[j]=_deckHand[j];
          }
          _deckHand = new Card[newDeck.length]
          _deckHand = newDeck                      // Change _deckHand to newDeck
          return result;                           // Return the card that was removed from _deckHand.
      } else {                                     // `else` indicates that the value has not been found
        System.out.println("\n--------------------------------------"
                + "\nThe deck does not contain that value"
                + "\n--------------------------------------");
      }   
}

编辑: 没有看到关于deleteAny()的最后一部分。您可以创建一个名为helperDelete(value,location)的辅助方法,该方法将删除值以及要删除的卡的位置。使用与上述相同的策略,一旦找到所需初始值的location,将其从甲板上移除,将甲板复制到新的缩短甲板,并将甲板实例设置为新甲板。

这应该允许您根据deleteAny()的需要以随机位置值移除卡片,并根据delete()的需要移除指定的位置值。

答案 1 :(得分:0)

如果需要在不使用system.arraycopy或array.utils的情况下从数组中删除元素,则可能会执行类似下面的remove函数。 (它只是静态的,因为我在一个文件中对此进行了测试。)

import java.util.Arrays;

public class HelloWorld{
     public static String[] remove(String[] arr,int index){
         String[] ret = new String[arr.length-1];
         for(int i = 0; i<index; i++){
             ret[i]=arr[i];
         }
         for(int i = index+1; i<arr.length; i++){
             ret[i-1]=arr[i];
         }
         return(ret);
     }
     public static void main(String []args){
        System.out.println("Hello World");
        String[] r = {"This","Is","ATest"};
        System.out.println(Arrays.toString(remove(r,0)));
        System.out.println(Arrays.toString(remove(r,1)));
        System.out.println(Arrays.toString(remove(r,2)));
     }
}