简体   繁体   English

如何从卡座中取出卡

[英]How to Remove a Card from a Deck

I'm trying to find a way to delete a specific card or a random card in a deck and return that deleted card. 我正在尝试找到一种方法来删除卡组中的特定卡或随机卡并返回该已删除的卡。 I've created classes for Card and DeckHand. 我已经为Card和DeckHand创建了类。 In the DeckHand class, I'm trying to create a delete method that allows the user to pick a card value, delete ONE instance of that card value from the deck, then returns the card that was deleted, and lastly, shorten the array by 1. I'm also trying to make a deleteAny method that deletes a random card from the deck, returns the card that was deleted, and shorten the array by 1. 在DeckHand类中,我试图创建一个delete方法,该方法允许用户选择一张卡值,从卡座中删除该卡值的一个实例,然后返回被删除的卡,最后,将数组缩短1.我还试图创建一个deleteAny方法,该方法从卡组中删除随机卡,返回已删除的卡,并将数组缩短1。

For the delete method, I'm having trouble finding a way to say: 对于delete方法,我很难找到一种说法:

*if the value input by the user isn't in the deck, print an error message. *如果用户输入的值不在卡座中,则输出错误消息。 *if it is, then find the first instance of a card with that value, delete it, and return the card. *如果是,则找到具有该值的卡的第一个实例,将其删除,然后退还该卡。

I don't understand how to find the first instance of a card with the value and then finding a way to set an available suit to create the instance of the card to then delete it and shift the positions in the array. 我不明白如何找到具有该值的卡的第一个实例,然后找到一种方法来设置可用的花色来创建卡的实例,然后删除它并移动数组中的位置。

I started trying to do an deleteAny method that deletes a random card. 我开始尝试执行deleteAny方法来删除随机卡。 I'm able to get the card output to the user that's getting removed, but I'm getting an error with my method. 我可以将卡输出输出给要删除的用户,但是我的方法出现错误。 Any ideas? 有任何想法吗?

Card Class: 卡类:

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

} Deck class: 甲板类:

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: Here's part of my main method that uses these delete and deleteAny methods: 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;

My answer uses most of your method from above. 我的答案从上面使用了您的大多数方法。 I've tweaked it to incorporate means of checking if we've found the value before. 我已经对其进行了调整,以结合检查之前是否已找到值的方法。

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

Edit: Didn't see the last part about deleteAny(). 编辑:没有看到关于deleteAny()的最后一部分。 You could make a helper method called helperDelete(value,location) that takes the value to delete and the position of the card which you want to delete. 您可以创建一个名为helperDelete(value,location)的帮助程序方法,该方法获取要删除的值和要删除的卡的位置。 Using the same strategy as above, once you find the location of the initial value that you want, remove it from the deck, copy the deck into a new, shortened deck, and set your deck instance to be the new deck. 使用与上述相同的策略,找到所需初始值的location后,将其从卡座中删除,将卡座复制到新的,缩短的卡座中,然后将卡座实例设置为新卡座。

This should allow you to remove the card at a random position value, as needed by deleteAny(), and at a specified location value, as needed by delete(). 这应该允许您根据deleteAny()的需要在随机位置值处以及根据delete()的指定位置值处删除卡。

If you need to remove an element from an array without using system.arraycopy or array.utils you might do something like the remove function that follows. 如果需要在不使用system.arraycopy或array.utils的情况下从数组中删除元素,则可以执行以下删除函数。 (It is only static because I tested this up in one file.) (这是静态的,因为我在一个文件中对此进行了测试。)

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