"非静态方法display()不能从静态上下文引用"

时间:2014-04-25 03:11:21

标签: java poker

我正在为学校制作一个扑克项目。我需要处理1000手3张牌扑克,分析牌局,并制作一个计数器来计算每种类型的牌(高牌,一对,三种,同花,直,同花顺)发生的次数。 1000手牌。

这是我的代码:

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

/**
 * All hand evaluations are done in the constructor, as to evaluate the hand automatically when an object is created.
 */
Hand(Deck d)
{
    val = new int[6];
    cards = new Card[3];
    for (int i=0; i<3; i++) // draws 3 cards from the deck
    {
        cards[i] = d.drawFromDeck();
    }

    int[] values = new int[14];

    for(int i=0; i<=13; i++) // Sets all contents of the array to zero.
    {
        values[i]=0;
    }

    for (int i=0; i<=2; i++) // increment value array at the index of each card's value
    {
        values[ cards[i].getValue() ]++;
    }

    int sameCards = 1; // There will always be one card that is the same
    int groupValue = 0;

    for(int i=13;i>=1;i--)
    {
        if (values[i] > sameCards)
        {
            sameCards=values[i];
            groupValue=i; // record value of cards
        }
    }

    int sameCards2=1;

    int largeGroupValue=0, smallGroupValue=0;

    for(int i=13; i>=1; i--) // Stores multiple pairs if needed.
    {
        if (values[i] > sameCards)
        {
            if (sameCards != 1)
            {
                sameCards2 = sameCards;
                smallGroupValue = largeGroupValue;
            }
            sameCards = values[i];
            largeGroupValue = i;
        }
        else if(values[i] > sameCards2)
        {
            sameCards2 = values[i];
            smallGroupValue = i;
        }
    }

    boolean flush = true;  //evaluates a flush
    for (int i=0; i<2; i++) //evaluates a flush
    {
        if (cards[i].getSuit() != cards[i+1].getSuit())
            flush=false;
    }

    int topStraightValue=0;
    boolean straight = false; // evaluates a straight

    for (int i=1; i<=11; i++) // evaluates a straight
    {
        if(values[i]==1 && values[i+1]==1 && values[i+2]==1)
        {
            straight = true;
            topStraightValue = i+2;
            break;
        }
    }
    if (values[12] == 1 && values[13] == 1 && values[1] == 1) // evaluates a straight
    {
        straight = true;
        topStraightValue = 14;
    }

    int[] orderedValues = new int[5];

    int index = 0;

    if (values[1] == 1) // Sets an Ace to 14
    {
        orderedValues[index]=14;
        index++;
    }
    for(int i=13;i>=2;i--)
    {
        if(values[i]==1)
        {
            orderedValues[index]=1;
            index++;
        }
    }

    // "if" statements below check evaluations and set the hand accordingly.

    if(sameCards==1){
        val[0]=1;
        val[1]=orderedValues[0];
        val[2]=orderedValues[1];
        val[3]=orderedValues[2];
        val[4]=orderedValues[3];
        val[5]=orderedValues[4];
    }
    if(sameCards==2 && sameCards2==1)
    {
        val[0]=2;
        val[1]=largeGroupValue;
        val[2]=orderedValues[0];
        val[3]=orderedValues[1];
        val[4]=orderedValues[2];
    }

    if(sameCards==3)
    {
        val[0]=4;
        val[1]=largeGroupValue;
        val[2]=orderedValues[0];
        val[3]=orderedValues[1];
    }
    if(straight)
    {
        val[0]=5;
        val[1]=topStraightValue;
    }
    if(flush)   
    {
        val[0]=6;
        val[1]=orderedValues[0];
        val[2]=orderedValues[1];
        val[3]=orderedValues[2];
        val[4]=orderedValues[3];
        val[5]=orderedValues[4];
    }
    if (straight && flush)
    {
        val[0]=9;
        val[1]=topStraightValue;
    }


}

/**
 * Displays the type of hand after evaluation.
 */
void display()
{
    String s;
    switch( val[0] )
    {
        case 1:
        s="high card";
        break;
        case 2:
        s="pair of " + Card.valueAsString(val[1]) + "\'s";
        break;
        case 3:
        s="two pair " + Card.valueAsString(val[1]) + " " + Card.valueAsString(val[2]);
        break;
        case 4:
        s="three of a kind " + Card.valueAsString(val[1]) + "\'s";
        break;
        case 5:
        s = Card.valueAsString(val[1]) + " high straight";
        break;
        case 6:
        s="flush";
        break;
        case 7:
        s="straight flush " + Card.valueAsString(val[1]) + " high";
        break;
        default:
        s="error in Hand.display: val[0] contains invalid value";
    }
    s = "                      " + s;
    System.out.println(s);
}

/**
 * Displays all cards.
 */
void displayAll()
{
    for (int i=0; i<3; i++)
        System.out.println(cards[i]);
}

卡:

public class Card
{
private int value, suit;

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

public static String valueAsString(int __value) {
    return values[__value];
}

Card(int suit, int value)

{
    this.value=value;
    this.suit=suit;
}

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

public int getValue() {
    return value;
}

public int 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 (int s=1; s<=4; s++) // adds cards of 13 values and 4 suits to the deck
    {
        for ( int v=1; v<=13; v++)
        {
            cards.add(new Card(s,v));
        }
    }

    int size;

    for (int i=0; i<1000; i++) // swaps random cards 1000 times to ensure a shuffled deck
    {
        index_1 = generator.nextInt(cards.size() - 1);
        index_2 = generator.nextInt(cards.size() - 1);

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

public Card drawFromDeck()
{
    return cards.remove(0);
}
public int getTotalCards()
{
    return cards.size();
}

}

计数器:

public class Counter
{
private Deck deck1;
private Hand hand1;

public Counter(Deck deck1)
{
    Deck deck = new Deck();
    Hand hand = new Hand(deck1);

}

public static void analyze()
{
    Hand.display();
}

public void count()
{
    int highcard = 0
    int pair = 0
    int threeofakind = 0
    int straight = 0
    int flush = 0
    int straightflush = 0
    for(i=0;i<1000;i++){
        Deck deck[i];
        Hand hand[i]
        analyze();
        switch( Hand.val[0] )
        {
            case 1:
            highcard++;
            break;
            case 2:
            pair++;
            break;
            case 3:
            null;
            break;
            case 4:
            threeofakind++;
            break;
            case 5:
            straight++;
            break;
            case 6:
            flush++;
            break;
            case 7:
            straightflush++;
            break;
            default:

        }
    }
}
}

0 个答案:

没有答案