简体   繁体   English

如何从java中的字符串数组中获取两个随机值的和?

[英]how to get the sum of the two random value from an array of strings in java?

im working on a card pair game that generates two random value from an array of strings.. what i want to know is how to get the sum of two random values from the array of strings to determine the winner. 我正在做一个纸牌对游戏,该游戏从一个字符串数组生成两个随机值..我想知道的是如何从字符串数组中获取两个随机值的和以确定获胜者。 here are the codes 这是代码

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());
}

} }

and here is the code to test the Cards Class: 这是测试Cards Class的代码:

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;
        }
    }
}
}

what lacks now is the code to determine the winner. 现在缺少的是确定获胜者的代码。

PS: im a beginner in java programming.. so please bear with me if u see an unusual use of codes :) PS:我是Java编程的初学者..因此,如果您看到代码的异常用法,请耐心等待:)


i have tried Dylan's suggestion but i can't seem to make it work.. instead used his idea and added this code to Cards class. 我已经尝试了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;
}

You have a problem that not all the cards are unique. 您有一个问题,并非所有卡都是唯一的。 Two players can have the same cards. 两个玩家可以拥有相同的卡。 They could all be the same card even. 他们甚至可能都是同一张牌。

Instead you should generate a list of all the possible cards. 相反,您应该生成所有可能的卡的列表。 I suggest using a Card class. 我建议使用Card类。 Use Collections.shuffle to shuffle them. 使用Collections.shuffle进行随机播放。 This way all the players will have different cards. 这样,所有玩家将拥有不同的牌。

You need rules for comparing a set of cards. 您需要比较一组卡的规则。 Define them in English first, and then translate them into code. 首先用英语定义它们,然后将它们翻译为代码。

You shouldn't use an array of Strings to hold the card faces, but an array of CardFace, having a name (for display), and a value (to compute the sum of the faces). 您不应该使用字符串数组来保存卡片面,而应该使用具有名称(用于显示)和值(用于计算面之和)的CardFace数组。 And since there are only 13 values, it should be an enum: 由于只有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;
    }
}

I would really suggest that your Card be a class or at least an enum. 我真的建议您的Card是班级或至少是枚举。 Then summing them will make sense. 然后总结它们将是有意义的。

Also, please explain these two 另外,请解释这两个

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

Pretty much what you need is for each player to have an instance of Cards much like you started 您几乎需要的是让每个玩家都像开始时一样拥有Cards实例

public class Cards extends ArrayList<Card>

but somewhere along the way you got lost and mixed up these two into one class that does nothing really. 但是一路上您迷路了,将这两者混为一类,实际上什么也没做。

so in your Card you put these 所以在你的卡上放这些

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
 }

Using the String.parseInt() method will return the number within a string. 使用String.parseInt()方法将返回字符串中的数字。 But, youll have issues with the ace,kind,queen,jack since they arent a number. 但是,由于它们没有数字,因此您在王牌,种类,女王,插孔方面会遇到问题。 i would Recommend creating a statement like: 我建议创建如下语句:

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;

I have managed to determine the winner by modifying the TestCard class instead of showing two random values from the array of strings. 我设法通过修改TestCard类来确定获胜者,而不是从字符串数组中显示两个随机值。 I changed it so that it shows only one value from the array. 我将其更改为仅显示数组中的一个值。 With that I managed to determine who the winner is. 这样我就确定了谁是赢家。

It seems that no one has talked about getting combinations, so I'll talk about it here. 似乎没有人谈论过获得组合,因此我将在这里进行讨论。 If there's anything you don't understand, feel free to comment here. 如果您不了解任何内容,请随时在此处评论。

Straight flush - you need a for loop. 直接冲洗-您需要一个for循环。

First, you should sort an array of the player's cards with ascending (1, 2, 3, 4, 5 etc.). 首先,您应该按升序(1、2、3、4、5等)对玩家的纸牌数组进行排序。 Now, check from the first entry, the lowest. 现在,从第一个条目(最低的)开始检查。

Note that the array cards stores only the values of the hand in integer. 请注意,阵列卡仅以整数存储手的值。

// 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;
        }
    }
...

How to compare the hands then? 那么如何比较双手呢? Find the highest value among the cards if it is a straight flush. 如果是同花顺,则在牌中找到最大值。

I'll post more about this soon. 我将很快发布更多有关此的信息。

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

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