gpt4 book ai didi

Java纸牌游戏。如何在main中调用

转载 作者:行者123 更新时间:2023-11-30 02:34:23 24 4
gpt4 key购买 nike

该程序的目的是创建一副纸牌,方法的名称是不言自明的。我认为我的所有方法都是正确的,但我需要在 main 中测试它们,但我不确定如何进行。我对对象、数组和直方图很陌生。有关如何测试某些数组/直方图方法的任何示例都会有所帮助。只是试图学习和理解。谢谢!

例如,我要测试的方法之一是 printDeck。所以在 main 中,我输入“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) {

}

}

最佳答案

I think I have all the methods correct,

不完全是...

你构建牌组的方法实际上应该为你提供一个构建好的牌组。

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

这应该可以帮助你在 main 中获得一副牌。

Card[] cards = buildDeck();
printDeck(cards);
<小时/>

测试程序的正确方法通常是单元测试,而不是主要方法

关于Java纸牌游戏。如何在main中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43484888/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com