繁体   English   中英

如何从卡座中取出卡

[英]How to Remove a Card from a Deck

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

对于delete方法,我很难找到一种说法:

*如果用户输入的值不在卡座中,则输出错误消息。 *如果是,则找到具有该值的卡的第一个实例,将其删除,然后退还该卡。

我不明白如何找到具有该值的卡的第一个实例,然后找到一种方法来设置可用的花色来创建卡的实例,然后删除它并移动数组中的位置。

我开始尝试执行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;
}
}

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

                    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;

我的答案从上面使用了您的大多数方法。 我已经对其进行了调整,以结合检查之前是否已找到值的方法。

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()的指定位置值处删除卡。

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

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)));
     }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM