簡體   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