如何制作卡片阵列以及如何轻松匹配它们

时间:2015-01-19 19:02:05

标签: java arrays java-collections-api

我正在制作一个疯狂的8级Java纸牌游戏并为一个玩家手牌制作了一系列牌,但是当我尝试打印出那只手随机字符的混乱时。我也有一个卡类,但似乎没问题。请帮忙。

错误:

Welcome to Crazy Eights!(Type 0 to start)
0
Your Hand:
[LnnajiO.Card;@73ae74f5

卡类:

import java.util.Random;
public class Card {

//All of the card numbers, in strings
final String[] FACESTRINGS = {"" , "Ace" , "Two", "Three", "Four", "Five", "Six", 
        "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
//All of the card numbers as integers
final int[] FACENUMS = {0,1,2,3,4,5,6,7,8,9,10,11,12,13};
final String[] SUITSTRINGS = {"Hearts", "Diamonds", "Spades", "Clubs"};
final int[] SUITNUMS = {1,2,3,4};

int FaceNumIndex;
int SuitIndex;
int cardFaceNumber;
String cardFaceString;
int suitFaceNumber;
String suitFaceString;
Random rand = new Random();

    //Default Random Card that assignes a random suit and number
public Card(){
    int tempSuit = rand.nextInt(3);
    int tempNumber = rand.nextInt(13)+1;
    FaceNumIndex = tempNumber;
    cardFaceNumber = FACENUMS[tempNumber];
    cardFaceString = FACESTRINGS[tempNumber];
    suitFaceNumber = SUITNUMS[tempSuit];
    suitFaceString = SUITSTRINGS[tempSuit];

}

    //Overrides the default 
public Card(int suit, int face){
    //Return 1-13 for the face and 0-3 for the suit
    FaceNumIndex = face;
    SuitIndex = suit;
    cardFaceNumber = FACENUMS[face];
    cardFaceString = FACESTRINGS[face];
    suitFaceNumber = SUITNUMS[suit];
    suitFaceString = SUITSTRINGS[suit];
}

public void setFaceName(int faceName){
    //Takes an int to set the Suit of the card  input 1-13
    FaceNumIndex = faceName;
    cardFaceNumber = FACENUMS[faceName];
    cardFaceString = FACESTRINGS[faceName];
}

public void setSuitName(int suitName){
    //Takes an int 1-4 to set the suit name from the array
    SuitIndex = suitName;
    suitFaceNumber = SUITNUMS[suitName];
    suitFaceString = SUITSTRINGS[suitName];
}
//Returns Face Values
public int getFace(){
    return FACENUMS[FaceNumIndex];
}
public String getFaceName(){
    return FACESTRINGS[FaceNumIndex];
}

//Return Suit Values
public int getSuit(){
    return SUITNUMS[SuitIndex];
}
public String getSuitName(){
    return SUITSTRINGS[SuitIndex];
}

//Compares this card to the returned class
public boolean isHigherThan(Card passCard, boolean AceHigher){
    int tempCardSuit = passCard.getSuit();
    int tempCardFace = passCard.getFace();
    if (tempCardFace>FaceNumIndex){
        return false;
    }else if (tempCardFace<FaceNumIndex){
        return true;
    }else{
        if (tempCardSuit>SuitIndex){
            return false;
        }else if(tempCardSuit<SuitIndex){
            return true;
        }else{
            System.out.println("The Cards are equal");
            return false;
        }
    }

}

//Override for previous isHigherThan
public boolean isHigherThan(Card PassCard){

    return isHigherThan(PassCard, true);

}

public String toString(){
    return cardFaceString + " of " + suitFaceString;
}

public boolean isHigherThan(int i, String string) {
    // TODO Auto-generated method stub
    return false;
}
}

对于这个&#34;如果&#34;声明我是这样做的,它将检查是否有任何&#34; suitFaceNumber&#34;或&#34; cardFaceNumber&#34;在player2手中的任何牌都匹配&#34; suitFaceNumber&#34;或&#34; cardFaceNumber&#34;起始卡,而不是一个接一个地做。

&#34;如果&#34;声明

if(player2Hand[0].cardFaceNumber==startingCard.cardFaceNumber ||
   player2Hand[4].suitFaceNumber==startingCard.suitFaceNumber ){

}

1 个答案:

答案 0 :(得分:0)

你的方法

public String toString(){...}
类卡中的

打印一张卡,但是当你有

Card[] hand

这不会自动应用于数组元素。你必须编写一个迭代数组的方法。

或者,编写一个Hand类,它将拥有自己的toString。