2017-04-19 42 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));”然后询问如何定义变量卡组以及将其定义为的内容。 –