2017-10-13 41 views
-1

我想爲卡片遊戲製作一個基本輪廓,我創建了卡片,創建了一個處理隨機卡片的方法,但是我很難將卡片添加到實際的手中。問題出在遊戲類與getCard()方法。我不知道這樣做的正確方法,因爲我的想法沒有奏效。卡片遊戲將卡片添加到一組「手」--Java

類創建甲板:

import java.util.Random; 

public class Deck<E> { 

//create a new linked list for Deck 
LinkedPositionalList deck = new LinkedPositionalList(); 

public Deck(){ 
    for (int i = 0; i < 4; i++){ 
     for(int j = 2; j< 14; j++){ 
      Card card = new Card(i,j); 
      deck.addLast(card); //add to linked list 
     } 
    } 
} 

public Card card(){ 
    Random rand = new Random(); 
    int position = rand.nextInt(52);//create random number 0-52 
    int counter = 0; 
    Iterator<Card> iter = this.deck.iterator(); 

    while(counter <= position){ 
     Card card = iter.next(); 
     if(counter == position){ 
      iter.remove(); 
      return card; 
     } 
     counter++; 
    } 
    return null; 
} 
} 

類創建卡:

public class Card<E> { 
public final static int CLUBS = 0, 
         SPADES = 1, 
         DIAMONDS = 2, 
         HEARTS = 3; 
private final static String [] suitNames = {"CLUBS", "SPADES", "DIAMONDS", "HEARTS"}; 

// Special cards 
private int JACK_VALUE = 11; 
private int QUEEN_VALUE = 12; 
private int KING_VALUE = 13; 
private int ACE_VALUE = 14; 

// The card 
private int suit = 0;   // Suit of the card 
private int value = 2;   // Value of the card 

public int getSuit() { 
    return this.suit; 
} 

public int getValue() { 
    return this.value; 
} 

public Card(int suit, int value){ 

    this.suit = suit; 
    this.value = value; 
} 

public String suitToString() { 
    return suitNames[ suit ]; 
} 

public String valueToString() { 
    if (value == ACE_VALUE) { 
     return "ACE"; 
    } else if (value == JACK_VALUE) { 
     return "JACK"; 
    } else if (value == QUEEN_VALUE) { 
     return "QUEEN"; 
    } else if (value == KING_VALUE) { 
     return "KING"; 
    } else if (value > 0) { 
     return String.valueOf(value); 
    } 
    return ""; 
} 

public String shortValueToString() { 
    if (value == ACE_VALUE) { 
     return " A"; 
    } else if (value == JACK_VALUE) { 
     return " J"; 
    } else if (value == QUEEN_VALUE) { 
     return " Q"; 
    } else if (value == KING_VALUE) { 
     return " K"; 
    } else if (value > 0) { 
     return String.format("%2d",value); 
    } 
    return ""; 
} 

public String toString() { 
    return valueToString() + " of " + suitToString(); 
} 

public String toShortString() { 
    return shortValueToString() + suitToString().substring(0,1); 
} 

public boolean equalTo(Card c) { 
    if (c.getSuit() != this.getSuit()) return false; 
    if (c.getValue() != this.getValue()) return false; 
    return true; 
} 
} 

類創建的手:

public class CardHand<E> { 

LinkedPositionalList<E> hand = new LinkedPositionalList(); 
//TODO: create method to order hand 
} 

類初始化遊戲:

public class Game{ 
    int players; 
    int maxCardsInHand; 
    int decks; 
    CardHand[] hand; 
    Card card; 
    Deck deck; 

//constructor 
public Game(int player, int max, int numDecks){ 
    players = player; 
    maxCardsInHand = max; 
    decks = numDecks; 
    this.hand = new CardHand[player]; 
    for(int index = 0; index < hand.length; index++){ 
     this.hand[index] = new CardHand(); 
    } 
} 

public void getCard(){ 
    System.out.println("You got this far..."); 
    card = deck.card(); 
    for(int index = 0; index < hand.length; index++){ //to add to each players hands 
     hand[index] = card; //issues with this part 
    } 
    //TODO: ordered correctly by suit AND value 
} 
+0

首先,'card'應該是'getCard()'方法中的局部變量。其次,你遇到了什麼問題? – Stefan

+1

@Stefan我得到一個空指針異常。我的輸出打印出「你有這麼多」。我需要打電話給deck.card();因爲這實際上處理卡 – Travis

+0

可能重複[什麼是NullPointerException,以及如何解決它?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do -i-fix-it) – GriffeyDog

回答

1

鑑於致電iter.remove();的工作原理,每當您撥打card()方法時,您的套牌就會越來越小。然而,你認爲你可以遍歷直通全甲板:

rand.nextInt(52); 

相反,迭代只有在你已經離開卡:

rand.nextInt(this.deck.size()); 

如果沒有大小()或length()方法的列表對象,每次調用卡片()時減少一個計數器:

rand.nextInt(cardsInDeck++); 
+1

我需要爲此使用位置鏈表。 – Travis