Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ArrayList扑克游戏Java中的比较_Java_Arraylist_Poker - Fatal编程技术网

ArrayList扑克游戏Java中的比较

ArrayList扑克游戏Java中的比较,java,arraylist,poker,Java,Arraylist,Poker,问题(简短版本):如何比较ArrayList中的元素 我对ArrayList的大部分基础知识都掌握得很好(添加、获取、设置、大小…)。我很难进入ArrayList来比较对象(扑克牌的价值和套装),以确定最佳的扑克牌。我有一个类来存储有关卡片的信息 卡片类别: /** class Card : for creating playing card objects * it is an immutable class. * Rank - valid values are

问题(简短版本):如何比较ArrayList中的元素

我对ArrayList的大部分基础知识都掌握得很好(添加、获取、设置、大小…)。我很难进入ArrayList来比较对象(扑克牌的价值和套装),以确定最佳的扑克牌。我有一个类来存储有关卡片的信息

卡片类别:

    /** class Card : for creating playing card objects
     *  it is an immutable class.
     *  Rank - valid values are 1 to 13
     *  Suit - valid values are 0 to 3
     *  Do not modify this class!
     */
    class Card {

        /* constant suits and ranks */
        static final String[] Suit = {"Clubs", "Diamonds", "Hearts", "Spades" };
        static final String[] Rank = {"","A","2","3","4","5","6","7","8","9","10","J","Q","K"};

        /* Data field of a card: rank and suit */
        private int cardRank;  /* values: 1-13 (see Rank[] above) */
        private int cardSuit;  /* values: 0-3  (see Suit[] above) */

        /* Constructor to create a card */
        /* throw PlayingCardException if rank or suit is invalid */
        public Card(int rank, int suit) throws PlayingCardException { 
        if ((rank < 1) || (rank > 13))
            throw new PlayingCardException("Invalid rank:"+rank);
        else
                cardRank = rank;
        if ((suit < 0) || (suit > 3))
            throw new PlayingCardException("Invalid suit:"+suit);
        else
                cardSuit = suit;
        }

        /* Accessor and toString */
        /* You may impelemnt equals(), but it will not be used */
        public int getRank() { return cardRank; }
        public int getSuit() { return cardSuit; }
        public String toString() { return Rank[cardRank] + " " + Suit[cardSuit]; }


        /* Few quick tests here */
        public static void main(String args[])
        {
        try {
            Card c1 = new Card(1,3);    // A Spades
            System.out.println(c1);
            c1 = new Card(10,0);    // 10 Clubs
            System.out.println(c1);
            //c1 = new Card(10,5);        // generate exception here
        }
        catch (PlayingCardException e)
        {
            System.out.println("PlayingCardException: "+e.getMessage());
        }
        }
    } 
/** Check current currentHand using multipliers and goodHandTypes arrays
*  Must print yourHandType (default is "Sorry, you lost") at the end o function.
*  This can be checked by testCheckHands() and main() method.
*/
    private void checkHands()
    {
        // implement this method!
        ArrayList<Card> multiplierCheck = new ArrayList<Card>();
        String yourhandtype = "Sorry, you lost";

        for (int toList = 0; toList<5; toList++) {
                multiplierCheck.add(currentHand.get(toList));
            }
        System.out.println(multiplierCheck);

        System.out.println(yourhandtype);
    }
/**类卡:用于创建扑克牌对象
*它是一个不可变的类。
*等级-有效值为1到13
*套装-有效值为0到3
*不要修改这个类!
*/
班级卡{
/*不变的诉讼和等级*/
静态最终字符串[]套装={“梅花”、“钻石”、“红心”、“黑桃”};
静态最终字符串[]秩={”、“A”、“2”、“3”、“4”、“5”、“6”、“7”、“8”、“9”、“10”、“J”、“Q”、“K”};
/*卡的数据字段:等级和等级*/
private int cardRank;/*值:1-13(参见上面的秩[])*/
private int cardSuit;/*值:0-3(参见上文的Suit[]*/
/*构造函数创建一张卡片*/
/*如果等级或套装无效,则抛出PlayingCardException*/
公共卡(整数等级,整数套装)抛出PlayingCardException{
如果((秩<1)| |(秩>13))
抛出新PlayingCardException(“无效等级:+等级”);
其他的
cardRank=等级;
如果((诉讼<0)| |(诉讼>3))
抛出新的PlayingCardException(“无效套装:+套装”);
其他的
cardSuit=西装;
}
/*存取器和toString*/
/*您可以强制使用equals(),但不会使用它*/
public int getRank(){return cardRank;}
public int getSuit(){return cardSuit;}
公共字符串toString(){return Rank[cardRank]+“”+Suit[cardSuit];}
/*这里有一些快速测试*/
公共静态void main(字符串参数[])
{
试一试{
卡c1=新卡(1,3);//黑桃
系统输出打印LN(c1);
c1=新卡(10,0);//10个俱乐部
系统输出打印LN(c1);
//c1=新卡(10,5);//在此处生成异常
}
捕捉(播放卡片例外)
{
System.out.println(“PlayingCardException:+e.getMessage());
}
}
} 
还有一个类来检查每一手牌(这是我很难弄清楚的类)。我目前已经添加了代码,以便添加一个ArrayList并再次打印每一张卡片(只是为了确保我可以创建一个单独的ArrayList,因为我对自己的能力不太满意),但我不知道如何比较每张卡片的元素(等级和西装)

检查手类:

    /** class Card : for creating playing card objects
     *  it is an immutable class.
     *  Rank - valid values are 1 to 13
     *  Suit - valid values are 0 to 3
     *  Do not modify this class!
     */
    class Card {

        /* constant suits and ranks */
        static final String[] Suit = {"Clubs", "Diamonds", "Hearts", "Spades" };
        static final String[] Rank = {"","A","2","3","4","5","6","7","8","9","10","J","Q","K"};

        /* Data field of a card: rank and suit */
        private int cardRank;  /* values: 1-13 (see Rank[] above) */
        private int cardSuit;  /* values: 0-3  (see Suit[] above) */

        /* Constructor to create a card */
        /* throw PlayingCardException if rank or suit is invalid */
        public Card(int rank, int suit) throws PlayingCardException { 
        if ((rank < 1) || (rank > 13))
            throw new PlayingCardException("Invalid rank:"+rank);
        else
                cardRank = rank;
        if ((suit < 0) || (suit > 3))
            throw new PlayingCardException("Invalid suit:"+suit);
        else
                cardSuit = suit;
        }

        /* Accessor and toString */
        /* You may impelemnt equals(), but it will not be used */
        public int getRank() { return cardRank; }
        public int getSuit() { return cardSuit; }
        public String toString() { return Rank[cardRank] + " " + Suit[cardSuit]; }


        /* Few quick tests here */
        public static void main(String args[])
        {
        try {
            Card c1 = new Card(1,3);    // A Spades
            System.out.println(c1);
            c1 = new Card(10,0);    // 10 Clubs
            System.out.println(c1);
            //c1 = new Card(10,5);        // generate exception here
        }
        catch (PlayingCardException e)
        {
            System.out.println("PlayingCardException: "+e.getMessage());
        }
        }
    } 
/** Check current currentHand using multipliers and goodHandTypes arrays
*  Must print yourHandType (default is "Sorry, you lost") at the end o function.
*  This can be checked by testCheckHands() and main() method.
*/
    private void checkHands()
    {
        // implement this method!
        ArrayList<Card> multiplierCheck = new ArrayList<Card>();
        String yourhandtype = "Sorry, you lost";

        for (int toList = 0; toList<5; toList++) {
                multiplierCheck.add(currentHand.get(toList));
            }
        System.out.println(multiplierCheck);

        System.out.println(yourhandtype);
    }
/**使用乘法器和GoodHandType数组检查当前的currentHand
*必须在o函数末尾打印您的手写体(默认为“对不起,您丢失了”)。
*这可以通过testCheckHands()和main()方法进行检查。
*/
私人无效支票
{
//实施这个方法!
ArrayList multiplierCheck=新建ArrayList();
String yourhandtype=“对不起,您输了”;

对于(int-toList=0;toList不确定您是否说过它是如何工作的,但要遍历arrayList

for (String s : arrayList)
    if (s.equals(value))
         // ...

字符串可以替换为int,etc.

来评估扑克牌可能最常见的事情是循环数据结构(可以是数组、列表等)并相互比较卡片。例如,这里有一些伪Java来比较直线:

for (int i = 1; i < /* length of hand */; i++) {

    if (/* rank for card i is not 1 greater
           than rank for card i - 1 */) {

         /* not a straight */
    }
}
请参阅,以了解其工作原理的描述,但这基本上是要排序的

然后,使用枚举或类似的东西会使计算在逻辑上变得相当简单

现在我推荐的是为评估创建一个方法,因为这样你可以方便地返回手的常数

static PokerHand evaluateHand(List<Card> hand) {
    for (PokerHand potential : PokerHand.values()) {
        if (potential.matches(hand))
            return potential;
    }

    /* imply there is not a matching hand */
    return null;
}
您不必制定方法,您可以执行以下操作:

PokerHand evaluated = null;
for (PokerHand potential : PokerHand.values()) {
    if (potential.matches(sortedHand)) {
        evaluated = potential;
        break;
    }
}

if (evaluated != null) {
    /* it's a recognized hand */
}
但是使用助手方法有助于组织代码


我希望这会有帮助。如果你还需要评分来决定是否有赢家,只需在枚举中添加另一个返回分数的方法。然后看看哪一个方法是最大的。

检查Hnads
到底做了什么,它怎么不起作用?@peeskillet我认为它还没有做任何事情。我想这是OP要问的。Mikael问题到底是什么?我假设你拥有的是每个玩家手牌的ArrayList,因此你可以得到牌,并将它们与扑克规则进行比较。这个问题实际上与ArrayList或比较扑克的牌值有关吗?@Radiodef我只是不确定如何访问ArrayList中的牌内信息。我我已经了解了如何使用[arraylist name].get(0).getRank()访问套装,但我很难了解如何比较(从等级中)找到直款,以及如何使一款比另一款王室同花顺款更“值钱”>同类三款>同花顺款>直款(或任何实际顺序)@peeskillet支票手应该看牌是否是同花顺的、直的、皇家同花顺的、三种同花顺的等等。我还不知道如何比较数组列表中的牌中的信息。所以“是”你的问题是关于扑克,而不是列表?这有助于反复浏览,谢谢。我是d找出如何比较特定值(牌的等级和套装)为了弄清楚直牌、同花顺牌、三种牌等等。我在手牌上迭代时遇到了很多麻烦,但这就解决了问题。另外,我现在看到,访问每张牌的等级和花色必须通过循环来完成。我试图弄清楚这一点,但你已经解决了。
PokerHand evaluated = evaluateHand(sortedHand);

if (evaluated != null) {
    /* it's a recognized hand */
}
PokerHand evaluated = null;
for (PokerHand potential : PokerHand.values()) {
    if (potential.matches(sortedHand)) {
        evaluated = potential;
        break;
    }
}

if (evaluated != null) {
    /* it's a recognized hand */
}