简体   繁体   English

运行 Java 扑克游戏

[英]Running Java Poker Game

I have to write a program that creates 5 decks of 5 random cards chosen, and evaluate them to return the value of the deck.我必须编写一个程序,创建 5 张随机选择的 5 张牌,并评估它们以返回牌组的值。 Etc. 9 is a royal lush and a 2 is a 2 pair.等 9 是皇家郁金香,2 是 2 对。

I have three classes so far.到目前为止,我已经上了三个课。 One to represent the Card, One to represent the Deck, and one to represent the Hand evaluation.一张代表卡牌,一张代表牌组,一张代表手牌评估。 I do not have a main program yet to call these, and wanted to know how that would be done.我还没有一个主程序来调用这些,并且想知道如何做到这一点。 For example, I want to have a main program that once ran, disperses out 5 hands of 5 random cards, evaluates them, and returns the value.例如,我想有一个主程序,一旦运行,分散出 5 张随机牌的 5 手,评估它们,并返回值。 I have the classes necessary to everything, just not sure how to make a class to make it run.我有一切所需的课程,只是不知道如何创建一个课程来让它运行。

public class Card{
private short rank, suit;

private static String[] suits = { "hearts", "spades", "diamonds", "clubs" };
private static String[] ranks  = { "Ace", "2", "3", "4", "5", "6", "7", 
                                   "8", "9", "10", "Jack", "Queen", "King" };

public static String rankAsString( int __rank ) {
    return ranks[__rank];
}

Card(short suit, short rank)
{
    this.rank=rank;
    this.suit=suit;
}

public @Override String toString()
{
      return ranks[rank] + " of " + suits[suit];
}

public short getRank() {
     return rank;
}

public short getSuit() {
    return suit;
}
}

import java.util.Random;import java.util.ArrayList;

public class Deck {
private ArrayList<Card> cards;

 Deck()
{
    cards = new ArrayList<Card>();
    int index_1, index_2;
    Random generator = new Random();
    Card temp;

    for (short a=0; a<=3; a++)
    {
        for (short b=0; b<=12; b++)
         {
           cards.add( new Card(a,b) );
         }
    }

    int size = cards.size() -1;

    for (short i=0; i<100; i++)
    {
        index_1 = generator.nextInt( size );
        index_2 = generator.nextInt( size );

        temp = (Card) cards.get( index_2 );
        cards.set( index_2 , cards.get( index_1 ) );
        cards.set( index_1, temp );
    }
}

public Card drawFromDeck()
{       
    return cards.remove( cards.size()-1 );
}

public int getTotalCards()
{
    return cards.size();
    //we could use this method when making 
    //a complete poker game to see if we needed a new deck
}
}

public class Hand {
private Card[] cards;
private int[] value;

Hand(Deck d)
{
    value = new int[6];
    cards = new Card[5];
    for (int x=0; x<5; x++)
    {
        cards[x] = d.drawFromDeck();
    }

    int[] ranks = new int[14];
    //miscellaneous cards that are not otherwise significant
    int[] orderedRanks = new int[5];
    boolean flush=true, straight=false;
    int sameCards=1,sameCards2=1;
    int largeGroupRank=0,smallGroupRank=0;
    int index=0;
    int topStraightValue=0;

    for (int x=0; x<=13; x++)
    {
        ranks[x]=0;
    }
    for (int x=0; x<=4; x++)
    {
        ranks[ cards[x].getRank() ]++;
    }
    for (int x=0; x<4; x++) {
        if ( cards[x].getSuit() != cards[x+1].getSuit() )
            flush=false;
    }

    for (int x=13; x>=1; x--)
    {
             if (ranks[x] > sameCards)
             {
                 if (sameCards != 1)
                 //if sameCards was not the default value
                 {
                     sameCards2 = sameCards;
                     smallGroupRank = largeGroupRank;
                 }

                 sameCards = ranks[x];
                 largeGroupRank = x;

             } else if (ranks[x] > sameCards2)
             {
                 sameCards2 = ranks[x];
                 smallGroupRank = x;
             }
    }

    if (ranks[1]==1) //if ace, run this before because ace is highest card
    {
        orderedRanks[index]=14;
        index++;
    }

    for (int x=13; x>=2; x--)
    {
        if (ranks[x]==1)
        {
            orderedRanks[index]=x; //if ace
            index++;
        }
    }

    for (int x=1; x<=9; x++)
    //can't have straight with lowest value of more than 10
    {
        if (ranks[x]==1 && ranks[x+1]==1 && ranks[x+2]==1 && 
            ranks[x+3]==1 && ranks[x+4]==1)
        {
            straight=true;
            topStraightValue=x+4; //4 above bottom value
            break;
        }
    }

    if (ranks[10]==1 && ranks[11]==1 && ranks[12]==1 && 
        ranks[13]==1 && ranks[1]==1) //ace high
    {
        straight=true;
        topStraightValue=14; //higher than king
    }

    for (int x=0; x<=5; x++)
    {
        value[x]=0;
    }


    //start hand evaluation
    if ( sameCards==1 ) {
        value[0]=1;
        value[1]=orderedRanks[0];
        value[2]=orderedRanks[1];
        value[3]=orderedRanks[2];
        value[4]=orderedRanks[3];
        value[5]=orderedRanks[4];
    }

    if (sameCards==2 && sameCards2==1)
    {
        value[0]=2;
        value[1]=largeGroupRank; //rank of pair
        value[2]=orderedRanks[0];
        value[3]=orderedRanks[1];
        value[4]=orderedRanks[2];
    }

    if (sameCards==2 && sameCards2==2) //two pair
    {
        value[0]=3;
        //rank of greater pair
        value[1]= largeGroupRank>smallGroupRank ? largeGroupRank : smallGroupRank;
        value[2]= largeGroupRank<smallGroupRank ? largeGroupRank : smallGroupRank;
        value[3]=orderedRanks[0];  //extra card
    }

    if (sameCards==3 && sameCards2!=2)
    {
        value[0]=4;
        value[1]= largeGroupRank;
        value[2]=orderedRanks[0];
        value[3]=orderedRanks[1];
    }

    if (straight && !flush)
    {
        value[0]=5;
        value[1]=4;
    }

    if (flush && !straight)
    {
        value[0]=6;
        value[1]=orderedRanks[0]; //tie determined by ranks of cards
        value[2]=orderedRanks[1];
        value[3]=orderedRanks[2];
        value[4]=orderedRanks[3];
        value[5]=orderedRanks[4];
    }

    if (sameCards==3 && sameCards2==2)
    {
        value[0]=7;
        value[1]=largeGroupRank;
        value[2]=smallGroupRank;
    }

    if (sameCards==4)
    {
        value[0]=8;
        value[1]=largeGroupRank;
        value[2]=orderedRanks[0];
    }

    if (straight && flush)
    {
        value[0]=9;
        value[1]=0;
    }

}

void display()
{
    String s;
    switch( value[0] )
    {

        case 1:
            s="high card";
            break;
        case 2:
            s="pair of " + Card.rankAsString(value[1]) + "\'s";
            break;
        case 3:
            s="two pair " + Card.rankAsString(value[1]) + " " + 
                            Card.rankAsString(value[2]);
            break;
        case 4:
            s="three of a kind " + Card.rankAsString(value[1]) + "\'s";
            break;
        case 5:
            s=Card.rankAsString(value[1]) + " high straight";
            break;
        case 6:
            s="flush";
            break;
        case 7:
            s="full house " + Card.rankAsString(value[1]) + " over " + 
                              Card.rankAsString(value[2]);
            break;
        case 8:
            s="four of a kind " + Card.rankAsString(value[1]);
            break;
        case 9:
            s="straight flush " + Card.rankAsString(value[1]) + " high";
            break;
        default:
            s="error in Hand.display: value[0] contains invalid value";
    }
    s = "                " + s;
    System.out.println(s);
}

void displayAll()
{
    for (int x=0; x<5; x++)
        System.out.println(cards[x]);
}

int compareTo(Hand that)
{
    for (int x=0; x<6; x++)
    {
        if (this.value[x]>that.value[x])
            return 1;
        else if (this.value[x]<that.value[x])
            return -1;
    }
    return 0; //if hands are equal
}
} 

Wouldn't it just be something like this?不就是这样吗? Not too complex... you wrote all the logic of the program already (I didn't double check that, BTW)不太复杂......你已经编写了程序的所有逻辑(我没有仔细检查,顺便说一句)

public class MainRunner{
    public static void main(String[] args){
        Deck deck = new Deck();
        Hand myHand = new Hand(deck);
        myHand.display();
    }
}

i am pretty sure that there's no add function in your Card.java我很确定您的 Card.java 中没有添加功能

for (short a=0; a<=3; a++)
{
    for (short b=0; b<=12; b++)
     {
       cards.add( new Card(a,b) ); //here
     }
}

So maybe go through your classes first?所以也许先上你的课?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM