gpt4 book ai didi

java - 我需要确定字符串格式 IE D-A 的两张卡之间的高卡

转载 作者:太空宇宙 更新时间:2023-11-04 12:58:35 24 4
gpt4 key购买 nike

这是我第一次在 Java Eclipse Mars 中编码,我们有一个我一直坚持的 Activity 。该 Activity 是模拟两名玩家的纸牌游戏。我们称它们为 P1 和 P2。要求是第一个:是创建一副 52 张牌,第二:创建一个方法来洗牌,第三:将牌轮流分发给P1和P2,直到各自的牌堆都是26张为止。第四:P1和P2从各自的牌堆中抽一张牌,然后互相比较,看看谁的牌更大。第五:获胜者拿走两张牌并将其放入自己的牌堆中。当玩家用完牌组中的牌时,游戏就结束了。

我现在正在完成的部分是第三部分。我现在需要的只是知道如何为这些卡分配一个值,以便完成比较部分。

我只会放置必要的代码。因为我的编码太困惑了。

String[] CurrentDeck = new String[52];
// This array contains the deck of cards to be distributed to each player
// This contains elements in this format:: "D-A", "D-K", "D-Q", "D-J", "D-10", "D-9", "D-8", "D-7", "D-6", "D-5", "D-4", "D-3", "D-2",... and so on.

String[] Pile1 = new String[52];
String[] Pile2 = new String[52];
// These represent the pile of cards for Player 1 and 2 respectively.
// They both start with 26 cards each.
// The reason they are 52 in size is because their pile can have 52 cards in it since

//获胜后,您将获得一轮中比较过的两张牌。

我已经将卡片正确地分配到两堆Pile1[0] 和 Pile2[0] 都是各自牌堆的顶牌。

现在是两位玩家抽出最上面的牌并进行比较的时候了。这就是现在的问题。我不知道如何将卡的值(value)放入卡中以供他们比较。以及如何从该轮的失败者那里拿到牌并将两张牌放入获胜者的牌库中。

顺便说一句,这就是游戏的运作方式。首先比较卡片的等级 IE、2-10、A、K、Q、JS-5 击败 D-3,因为 5 > 3当牌具有相同等级时,会发生第二次比较。我们现在去比较一下钻石、红心、黑桃、梅花这些牌的花色。D-5 击败 S-5,因为 D > S

所以,提前感谢任何能给我任何想法的人:)

最佳答案

虽然这不是我的第一个建议,但我在下面编写了一些适用于您的情况的代码。这将为卡返回一个整数值,您可以检查该整数值是更高还是更低。

public static int checkValue(String card)
{
String[] name = card.split("-");

switch(name[1])
{
case "J":
return 11;
case "Q":
return 12;
case "K":
return 13;
case "A:
return 1;
default:
return Integer.parseInt(name[1]);
}
}

我个人会为该卡创建一个全新的类,但如果您刚刚开始使用 Java,可能会有点困惑,因为我猜您只是习惯于使用一个类。

对于“以及如何从该轮的失败者那里获取卡片并将两张卡片放入获胜者的牌库”,您可以执行以下操作...

String winCardsP1 = "", winCardsP2 = "";
// ...

// When a player wins
winCardsP1 += Pile2[i] + " ";
// OR
winCardsP2 += Pile1[i] + " ";
// ...

// Then to create the final array...
Pile1 = winCardsP1.split(" ");
Pile2 = winCardsP2.split(" ");

// This will make a new array with all the cards they won.
// You will have to adjust your loop in order to compensate
// looping through the decks each time, and when they have no cards they lose.

// The '...' stands for code in between the assignments.

关于java - 我需要确定字符串格式 IE D-A 的两张卡之间的高卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35142084/

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