0

我正在做一个卡片对游戏,它从字符串数组中生成两个随机值。我想知道的是如何从字符串数组中获取两个随机值的总和以确定获胜者。这是代码

import java.util.*;

public class Cards {
private String suit;
private String face;
private String[] cardSuits;
private String[] cardFaces;
private Random ran;

public Cards() {
    ran = new Random();
    cardSuits = new String[] { "of Spade", "of Hearts", "of Diamonds",
            "of Clubs" };
    cardFaces = new String[] { "Ace", "2", "3", "4", "5", "6", "7", "8", "9",
            "10", "Jack", "Queen", "King" };

}

public String setPlayerCardSuit() {
    suit = cardSuits[ran.nextInt(4)];
    return suit;

}

public String setPlayerCardFace() {
    face = cardFaces[ran.nextInt(13)];
    return face;
}

public String setPlayerCardSuit2() {
    suit = cardSuits[ran.nextInt(4)];
    return suit;

}

public String setPlayerCardFace2() {
    face = cardFaces[ran.nextInt(13)];
    return face;
}

public String setCompCardSuit() {
    suit = cardSuits[ran.nextInt(4)];
    return suit;

}

public String setCompCardFace() {
    face = cardFaces[ran.nextInt(13)];
    return face;
}

public String setCompCardSuit2() {
    suit = cardSuits[ran.nextInt(4)];
    return suit;

}

public String setCompCardFace2() {
    face = cardFaces[ran.nextInt(13)];
    return face;
}

public void getResults() {
    System.out.println("Here are your cards: " + setPlayerCardFace() + " "
            + setPlayerCardSuit() + " and " + setPlayerCardFace2() + " "
            + setPlayerCardSuit2());
}

public void getCompCard() {
    System.out.println("Here's the computer's cards: " + setCompCardFace()
            + " " + setCompCardSuit() + " and " + setCompCardFace2() + " "
            + setCompCardSuit2());
}

}

这是测试 Cards 类的代码:

import javax.swing.JOptionPane;

public class TestCards {
public static void main(String[] args) {
    Cards playerCards = new Cards();
    Cards computerCards = new Cards();

    int confirm, x = 1;
    while (x == 1) {
        JOptionPane.showMessageDialog(null,
                "Random Card game \nPlease press OK to Start Game",
                "Card Pair Game", JOptionPane.INFORMATION_MESSAGE);

        JOptionPane.showMessageDialog(
                null,
                "Here are your Cards: " + playerCards.setPlayerCardFace()
                        + " " + playerCards.setPlayerCardSuit() + " and "
                        + playerCards.setPlayerCardFace2() + " "
                        + playerCards.setPlayerCardSuit2()
                        + "\nThe Computer's Cards are: "
                        + computerCards.setCompCardFace() + " "
                        + computerCards.setCompCardSuit() + " and "
                        + computerCards.setCompCardFace2() + " "
                        + computerCards.setCompCardSuit2());


        confirm = JOptionPane.showConfirmDialog(null, "Game Ends. Again?",
                "Game Over", JOptionPane.YES_NO_OPTION);

        if (confirm != JOptionPane.YES_OPTION) {
            x = 2;
        }
    }
}
}

现在缺少的是确定获胜者的代码。

PS:我是Java编程的初学者..所以如果你看到不寻常的代码使用请多多包涵:)


我已经尝试过 Dylan 的建议,但我似乎无法让它发挥作用.. 而是使用了他的想法并将此代码添加到 Cards 类。

public int playerValues(){
    int temp = 0;
    if(face != cardFaces[0] && face != cardFaces[10] && face != cardFaces[11] && face != cardFaces[12]){
        temp = Integer.parseInt(face);
    }else if(face == cardFaces[0]){
        temp = 1;
    }else if(face == cardFaces[10]){
        temp = 11;
    }else if(face == cardFaces[11]){
        temp = 12;
    }else if(face == cardFaces[12]){
        temp = 13;
    }
    return temp;
}
public int computerValues(){
    int temp = 0;
    if(face != cardFaces[0] && face != cardFaces[10] && face != cardFaces[11] && face != cardFaces[12]){
        temp = Integer.parseInt(face);
    }else if(face == cardFaces[0]){
        temp = 1;
    }else if(face == cardFaces[10]){
        temp = 11;
    }else if(face == cardFaces[11]){
        temp = 12;
    }else if(face == cardFaces[12]){
        temp = 13;
    }
    return temp;
}
4

6 回答 6

4

你有一个问题,不是所有的卡片都是独一无二的。两个玩家可以拥有相同的牌。他们甚至都可以是同一张卡。

相反,您应该生成所有可能卡片的列表。我建议使用一个Card类。用来洗牌Collections.shuffle。这样所有的玩家都会有不同的牌。

您需要比较一组卡片的规则。先用英文定义,再翻译成代码。

于 2012-08-06T14:58:43.860 回答
3

您不应该使用字符串数组来保存卡片面,而是使用 CardFace 数组,具有名称(用于显示)和值(用于计算面的总和)。由于只有 13 个值,它应该是一个枚举:

public enum CardFace {
    ACE("Ace, 1),
    TWO("2", 2),
    ...
    KINK("King", 13);

    private String face;
    private int value;

    private CardFace(String face, int value) {
        this.face = face;
        this.value = value;
    }

    public String getFace() {
        return this.face;
    }

    public int getValue() {
        return this.value;
    }
}
于 2012-08-06T15:03:08.620 回答
0

好像没有人讨论过获取组合,所以我就在这里说一下。如果有什么不明白的,欢迎在这里评论。

同花顺 - 你需要一个 for 循环。

首先,您应该按升序(1、2、3、4、5 等)对玩家牌的数组进行排序。现在,从第一个条目开始检查,最低的。

请注意,数组卡片仅以整数形式存储手牌的值。

// Variables to store the current and previous cards.
int previous = 0;
int current = 0; 

cards = Arrays.sort(cards); // Sort cards into ascending order

boolean straightFlush = false;

for (i = 0; i < cards.length; i++) {
    current = cards[i];
    if (!current == cards[0]) { // If current is not the first card, execute the rest. You don't want random errors popping up, do you? :)
        // Check if it is the last card - a straight flush would then be present
        if (current == cards[cards.length - 1]) {
            straightFlush = true;
            break;
        }

        // Checks if current - 1 != previous or the current card is not consecutive to the previous card
        if (current - 1 != previous) {
            break;
        }
    }
...

那么如何比较双手呢?如果是同花顺,则找出牌中的最高值。

我很快就会发布更多关于这个的信息。

于 2012-08-10T09:43:10.510 回答
0

使用该String.parseInt()方法将返回字符串中的数字。但是,您会遇到 ace、kind、queen、jack 的问题,因为它们不是数字。我建议创建如下声明:

if(!cardFaces[x].equals("Ace")&&!cardFaces[x].equals("Queen")&&!cardFaces[x].equals("King")&&!cardFaces[x].equals("Jack"))
  {
    int temp = cardFaces[x].parseInt();
   }
else if(cardFaces[x].equals("Ace")
{
  int temp = 1;
}
else if(cardFaces[x].equals("King") || cardFaces[x].equals("Queen") || cardFaces[x].equals("Jack"))
{
 int temp = 10;
于 2012-08-06T15:07:38.313 回答
0

我真的建议您的 Card 是一个类或至少是一个枚举。然后将它们相加将是有意义的。

另外,请解释这两个

public String setCompCardFace()
public String setCompCardFace2()  //identical

几乎你需要的是让每个玩家都有一个卡片实例,就像你开始的那样

public class Cards extends ArrayList<Card>

但是一路走来,您迷路了,并将这两者混为一堂,实际上什么也没做。

所以在你的卡片里你把这些

public class Card 
{
    private String suit;
    private String face;

    public Card(String face, String color)
    {
        suit = color; this.face = face;
    }

    @Override
    public int compareTo(Card otherCard); //implement this yourself
 }
于 2012-08-06T15:00:07.947 回答
0

我设法通过修改TestCard类而不是显示字符串数组中的两个随机值来确定获胜者。我对其进行了更改,使其仅显示数组中的一个值。有了这个,我设法确定谁是赢家。

于 2012-08-08T13:08:26.947 回答