2016-10-20 52 views
0

我正試圖找到一種方法來刪除卡片或隨機卡片中的特定卡片並返回刪除的卡片。我爲Card和DeckHand創建了類。在DeckHand類中,我試圖創建一個刪除方法,該方法允許用戶選擇卡片值,從卡組中刪除該卡片的一個實例,然後返回被刪除的卡片,最後,通過縮短數組1.我也嘗試使用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; 
} 
} 

主營: 這裏是我的主要方法的一部分,使用這些刪除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; 
+0

我會用'array.utils.remove' http://stackoverflow.com/questions/642897/removing-an-element-from -an-array-java – kpie

+0

我被告知不要使用它來完成這項任務。 – Alisa

+0

告訴你的教授停止成爲一個小貓,並以默認情況下實際需要數據結構的語言教授數據結構。 (這將是C)另外,如果你看到了答案,還有另一個沒有array.utils。你允許使用'system.arraycopy'嗎? – kpie

回答

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()的需要,以隨機位置值刪除該卡。

+0

謝謝你的建議。它有一點幫助,但這似乎並不正確。它不會將陣列重新調整到將卡片帶走的位置。 – Alisa

+0

有趣。也許用'_deckHand = new Card [_deckHand.length-1]'替換'_deckHand = new Card [newDeck.length]'。 [此鏈接](http://stackoverflow.com/questions/2777762/shorten-array-length-once-element-is-remove-in-java)可能會幫助你調整大小。 –

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