我正在开发一个卡片对游戏,它从一个字符串数组生成两个随机值..我想知道的是如何从字符串数组中获取两个随机值的总和来确定胜利者。这是代码
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());
}
}
以下是测试卡类的代码:
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的建议,但我似乎无法使其工作..而是使用他的想法并将此代码添加到卡类。
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;
}
答案 0 :(得分:4)
您遇到的问题是并非所有卡都是唯一的。两名球员可以拥有相同的牌。它们甚至都可以是同一张卡。
相反,您应该生成所有可能卡片的列表。我建议使用Card
课程。使用Collections.shuffle
对其进行随机播放。这样所有玩家都会有不同的牌。
您需要比较一组卡片的规则。首先用英语定义它们,然后将它们翻译成代码。
答案 1 :(得分: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;
}
}
答案 2 :(得分:0)
我真的建议您的卡片是一个类或至少是一个枚举。然后总结它们将是有道理的。
另外,请解释这两个
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
}
答案 3 :(得分: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;
答案 4 :(得分:0)
我设法通过修改TestCard
类来确定获胜者,而不是显示字符串数组中的两个随机值。我改变它,以便它只显示数组中的一个值。有了这个,我设法确定胜利者是谁。
答案 5 :(得分: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;
}
}
...
如何比较手呢?如果是同花顺,请在牌中找到最高值。
我会尽快发布更多相关信息。