将整数值分配给ArrayList中的字符串

时间:2015-11-09 00:16:23

标签: java arraylist

我正在尝试制作纸牌游戏。其中大部分都有效,但我无法找到一种有效的方法来为我的数组中的字符串元素分配整数值。基本上我有一个ArrayList,每张牌都在一个标准牌组中,每回合就有两张牌。即使它们是弦乐(黑桃王牌等)我也需要它们以某种方式具有数值,因此可以比较两张牌以确定胜利者(杰克击败7等)。我已经尝试过,我认为应该可以工作但不能。

boolean checkAce = deckList.get(0).startsWith("Ace");
boolean checkTwo = deckList.get(0).startsWith("Two");
boolean checkThree = deckList.get(0).startsWith("Three");
boolean checkFour = deckList.get(0).startsWith("Four");
boolean checkFive = deckList.get(0).startsWith("Five");
boolean checkSix = deckList.get(0).startsWith("Six");
boolean checkSeven = deckList.get(0).startsWith("Seven");
boolean checkEight = deckList.get(0).startsWith("Eight");
boolean checkNine = deckList.get(0).startsWith("Nine");
boolean checkTen = deckList.get(0).startsWith("Ten");
boolean checkJack = deckList.get(0).startsWith("Jack");
boolean checkQueen = deckList.get(0).startsWith("Queen");
boolean checkKing = deckList.get(0).startsWith("King");

boolean checkAceP2 = deckList.get(1).startsWith("Ace");
boolean checkTwoP2 = deckList.get(1).startsWith("Two");
boolean checkThreeP2 = deckList.get(1).startsWith("Three");
boolean checkFourP2 = deckList.get(1).startsWith("Four");
boolean checkFiveP2 = deckList.get(1).startsWith("Five");
boolean checkSixP2 = deckList.get(1).startsWith("Six");
boolean checkSevenP2 = deckList.get(1).startsWith("Seven");
boolean checkEightP2 = deckList.get(1).startsWith("Eight");
boolean checkNineP2 = deckList.get(1).startsWith("Nine");
boolean checkTenP2 = deckList.get(1).startsWith("Ten");
boolean checkJackP2 = deckList.get(1).startsWith("Jack");
boolean checkQueenP2 = deckList.get(1).startsWith("Queen");
boolean checkKingP2 = deckList.get(1).startsWith("King");

int playerOneScore = 0;
int playerTwoScore = 0;

if (checkAce = true) {
    playerOneScore = 14;
}else if (checkTwo = true) {
    playerOneScore = 2;
}else if (checkThree = true) {
    playerOneScore = 3;
}else if (checkFour = true) {
    playerOneScore = 4;
}else if (checkFive = true) {
    playerOneScore = 5;
}else if (checkSix = true) {
    playerOneScore = 6;
}else if (checkSeven = true) {
    playerOneScore = 7;
}else if (checkEight = true) {
    playerOneScore = 8;
}else if (checkNine = true) {
    playerOneScore = 9;
}else if (checkTen = true) {
    playerOneScore = 10;
}else if (checkJack = true) {
    playerOneScore = 11;
}else if (checkQueen = true) {
    playerOneScore = 12;
}else if (checkKing = true) {
    playerOneScore = 13;
}

if (checkAceP2 = true) {
    playerTwoScore = 14;
}else if (checkTwoP2 = true) {
    playerTwoScore = 2;
}else if (checkThreeP2 = true) {
    playerTwoScore = 3;
}else if (checkFourP2 = true) {
    playerTwoScore = 4;
}else if (checkFiveP2 = true) {
    playerTwoScore = 5;
}else if (checkSixP2 = true) {
    playerTwoScore = 6;
}else if (checkSevenP2 = true) {
    playerTwoScore = 7;
}else if (checkEightP2 = true) {
    playerTwoScore = 8;
}else if (checkNineP2 = true) {
    playerTwoScore = 9;
}else if (checkTenP2 = true) {
    playerTwoScore = 10;
}else if (checkJackP2 = true) {
    playerTwoScore = 11;
}else if (checkQueenP2 = true) {
    playerTwoScore = 12;
}else if (checkKingP2 = true) {
    playerTwoScore = 13;
}

if (playerOneScore > playerTwoScore) {
    System.out.println("Player One Wins!");
}else if (playerTwoScore > playerOneScore) {
    System.out.println("Player Two Wins!");
}else if (playerOneScore == playerTwoScore) {
    System.out.println("Tie");
}

这背后的想法是我预先处理卡片的每个循环都会检查ArrayList元素以查看开头是否与面值的字符串匹配。然后if语句检查哪一个为真,并为每个玩家的得分值分配适当的点并进行比较。出于某些原因,每次运行checkAcecheckAceP2时,无论实际位于truedeckList.get(0)的卡是什么,都会评估为.get(1)。这导致每次都打平。任何人都能看到我的代码中的缺陷吗?或者,任何人都可以推荐一种更好的方法来实现我的目标吗?

3 个答案:

答案 0 :(得分:1)

(checkAce = true)true分配给checkAce并且(作为副作用)评估为true。您需要==或只需布尔值(适用于所有if)。

if (checkAce == true)

更短的

if (checkAce)

答案 1 :(得分:0)

回答

  

我无法找到一种有效的方法来为我的数组中的字符串元素分配整数值。

如果可能的话,我会使用Enumeration而不是String,因为每张卡的值都是常量。然后,您没有字符串ArrayList,而是拥有一个ArrayList of Cards。

public enum Card {
    ACE(14),
    ONE(1),
    TWO(2),
    ...
    KING(13);

    /** The point value associated to each card. */
    private int pointValue;

    private Card(int pointValue) {
        this.pointValue = pointValue;
    }

    public int getPointValue() {
        return this.pointValue;
    }

}

答案 2 :(得分:0)

您的问题是您将卡存储为字符串。卡片通常表示为具有两个属性的对象:Rank和Suit。

卡片的字符串表示很容易从Rank和Suit构建。然而,从字符串中提取Rank和Suit并不容易,特别是如果您的纸牌游戏变得流行并且需要翻译成其他语言。它也比其他方式慢得多。

所以我会说你的麻烦就是你错了。使用Rank字段和Suit字段创建Card类。这两个领域都可以从枚举中受益:

public enum Rank { TWO, THREE, ... , JACK, QUEEN, KING, ACE }
public enum Suit { SPADES, HEARTS, DIAMONDS, CLUBS }
public class Card {
    public final Rank rank;
    public final Suit suit;
    public Card(Rank rank, Suit suit) {
        this.rank = rank;
        this.suit = suit;
    }
    @Override
    public String toString() {
        // code here
    }
}

或制作字段private并添加getter方法。

这样您的代码就变得更容易了:

if (deckList.get(0).rank == Rank.ACE) {

打印一张卡片(或整张deckList)仍然会显示全文,例如" Queen of Hearts",toString()方法提供。