2017-04-19 62 views
1

這個程序的目的是創建一副撲克牌,方法的名字是自我解釋的。我想我所有的方法都是正確的,但我需要全部測試它們,我不知道如何。我是對象,數組和直方圖的新手。有關如何測試某些陣列/直方圖方法的任何示例都會有所幫助。只是想學習和理解。謝謝!Java卡片遊戲。如何打電話

例如,我想測試的一種方法是printDeck。所以主要我輸入「printDeck(deck);」但它需要知道在哪裏獲得套牌,我對如何做到這一點感到困惑。

class Cards { 
int suit, rank; 


public Cards() { 
    this.suit = 0; 
    this.rank = 1; 
} 


public Cards(int suit, int rank) { 
    this.suit = suit; 
    this.rank = rank; 
} 

public static int compareCard (Cards c1, Cards c2) { 
    if (c1.suit>c2.suit) return 1; 
    else if (c1.suit<c2.suit) return-1; 
    else { 
     if (c1.rank == 1)c1.rank = 14; 
     if (c2.rank == 1)c2.rank = 14; 
     if (c1.rank>c2.rank) { 
      if (c1.rank == 14)c1.rank = 1; 
      return 1; 
     } else if (c1.rank<c2.rank) { 
      if (c2.rank == 14)c2.rank = 1; 
      return -1; 
     } else { 
      if (c1.rank == 14)c1.rank = 1; 
      if (c2.rank == 14)c2.rank = 1; 
      return 0; 
     } 
    } 
} 


public static void buildDeck() { 
    Cards[] deck = new Cards[52]; 
    int i = 0; 
    for (int s = 0; s<4; s++) { 
     for (int r = 1; r<14; r++) { 
      deck[i] = new Cards(s, r); 
      i++; 
     } 
    } 
} 


public static int handScore (Cards[] ar) { 
    int x = 0; 
    for (int i =0; i< ar.length; i++) { 
     if (ar[i].rank > 10) x += (ar[i].rank-10); 
    } 
    int result = 0; 
    for (int i =0; i< ar.length; i++) { 
     result += ar[i].rank; 
    } 
    return result-x; 
} 


public static Cards parseCard(String s) { 
    Cards emptyCard = null; 
    String[] suits = {" ", "Clubs", "Diamonds", "Hearts", "Spades"}; 
    String[] ranks = {"n", "Ace", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" }; 
    String sr = s.substring(0, s.indexOf(' ')); 
    String ss = s.substring (s.lastIndexOf(' ')+1, s.length()-1); 
    int holdRank = 0; 
    for (int i=0; i<ranks.length; i++) { 
     if (ranks[i].equals(sr)) holdRank = i; 
    } 
    if (holdRank == 0) return emptyCard; 
    int holdSuit = 0; 
    for (int i=0; i<suits.length; i++) { 
     if (ranks[i].equals(sr)) holdSuit = i; 
    } 
    if (holdSuit == 0)return emptyCard; 
    Cards result = new Cards (holdSuit-1, holdRank); 
    return result; 
} 

public static int[] suitHist (Cards[] ar) { 
    int[] hist = {0, 0, 0, 0}; 
    for (int i = 0; i<ar.length; i++) { 
     hist[ar[i].suit]++; 
    } 
    return hist; 
} 


public static boolean isFlush (Cards[] ar) { 
    int[] h = suitHist(ar); 
    if (h[0] > 4 || h[1] > 4 || h[2] > 4|| h[3] > 4) return true; 
    else return false; 
} 


public static void printCard (Cards c) { 
String[] suits = { "Clubs", "Diamonds", "Hearts", "Spades" }; 
String[] ranks = { "narf", "Ace", "2", "3", "4", "5", "6", 
"7", "8", "9", "10", "Jack", "Queen", "King" }; 
System.out.println (ranks[c.rank] + " of " + suits[c.suit]); 
} 

public static void printDeck (Cards[] deck) { 
for (int i=0; i<deck.length; i++) { 
printCard (deck[i]); 
} 
} 

public static void main(String[] args) { 

} 

} 
+0

因爲都是靜態方法,只是叫他們,他們需要傳遞的參數從主要。 – Jason

+0

downvoter,請解釋 – jace

+0

這個問題沒有錯。 OP正在尋求信息來連接想法。 – Jason

回答

2

我想我有正確的方法,

不太...

你的方法建立一個平臺實際上應該給你一個內置的甲板。

public static Card[] buildDeck() { 
    ... 
    return deck; 
} 

這應該會幫助你獲得一個主牌。

Card[] cards = buildDeck(); 
printDeck(cards); 

來測試你的程序的正確方法,但通常是單元測試,不是主要的方法

+0

謝謝。現在,當我打印主畫面時,我輸入「System.out.println(buildDeck());」並且輸出是「[LCards; @ 15db9742」。我如何才能真正打印一副牌? –

+0

您是否編寫了這段代碼?你有'printDeck',所以用那 –

+0

啊對不起,我很慌亂。我打算輸入「System.out.println(printDeck(deck));」然後詢問如何定義變量卡組以及將其定義爲的內容。 –