2017-04-19 4 views
1

このプログラムの目的は、カードのデッキを作成することであり、メソッドの名前は自明です。私はすべての方法が正しいと思うが、私はそれらをすべてメインでテストする必要があり、どのようにわからない。私はオブジェクト、配列、およびヒストグラムが初めてです。どのように配列/ヒストグラムメソッドのいくつかをテストする方法の例が役に立ちます。ただ学び、理解しようとしています。ありがとうございました!Javaカードゲーム。どのように呼び出すか

たとえば、テストしたいメソッドの1つが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(デッキ));"次に可変デッキの定義方法とその定義方法を尋ねます。 –

関連する問題