gpt4 book ai didi

java - 如何从纸牌游戏字符串数组中获取int值

转载 作者:行者123 更新时间:2023-12-01 13:43:53 24 4
gpt4 key购买 nike

我正在尝试创建一款 war 纸牌游戏。每位玩家一次抽 1 张牌,每张最多 26 张。现在我需要将卡片的字符串值转换为整数,以便我可以使用它们来确定谁拥有更高的手牌。这是我第一个使用数组的程序,我在使用这个程序时遇到了很多问题。我该怎么做呢?

编辑:如果不清楚,我问如何从 String [] 数字获取 int 值,而无需手动分配任何值。为了查看每手牌谁获胜,我需要比较整数。

import java.util.ArrayList;
import java.util.Random;

public class Card {
String finalCard = "";
int suit, number;
String [] suits = {"Heart" , "Diamond" , "Spade" , "Club"}; //suits
String [] numbers = { "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" , "Jack" , "Queen" , "King" , "Ace" }; //card values
String card = "";
public Card() {

}
public Card(int suits, int numbers)
{
suit = suits;
number = numbers;
}


public String toString()
{
String finalCard = numbers[number] + " of " + suits[suit];
return finalCard;
}
}

import java.util.ArrayList;
import java.util.Random;

public class FullDeck {
private ArrayList<Card> cards = new ArrayList<Card>();//card array list

public FullDeck()
{
for(int a =0; a<=3; a++) //loops through suits
{
for(int b =0; b<=12;b++) //loops through values
{
cards.add(new Card(a,b)); //creates adds cards to list
}

}

}

public Card drawRandomCard()
{
Random generator = new Random(); //picks random card
int index = generator.nextInt(cards.size());
return cards.remove(index); //removes card from list
}

public String toString()
{
String result = "Cards remaining in deck: " + cards; //not currently used
return result;
}
}
import java.applet.Applet;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import javax.swing.*;

public class WarUI extends JApplet
implements ActionListener {
JTextArea displayLabel = new JTextArea(""); //sets label to display message
JButton runButton = new JButton("Run"); //button that starts program
Container con = getContentPane(); //gets container


Card player1;
Card player2;
FullDeck hand1 = new FullDeck();

Card card = new Card();


public void init() {
con.setLayout(new FlowLayout());//sets flowlayout
con.add(new JLabel()); //jlabel container
con.add(runButton); //run button container
con.add(displayLabel); //display label container
runButton.addActionListener(this);//looks to see if run is clicked
}

public void actionPerformed(ActionEvent e) {


for (int i = 0; i < 1; i++) {
player1= hand1.drawRandomCard(); //draws cards for player 1
player1.toString();
}
for (int i = 0; i < 1; i++) {
player2= hand1.drawRandomCard(); //draws cards for player 2
player2.toString();
}

displayLabel.setText(player1.toString() + "\n" + player2.toString() ); //displays both players values/suits
}
}

最佳答案

首先将 Card 类中的花色和数字设为静态。因为数组始终保持相同,不必在每次创建卡片时都进行初始化。尽管您必须向 Card 类中的变量suit和number添加getter。

要检查哪张牌更大,您首先必须检查哪张牌的数字索引更高,然后检查某张牌的花色索引。

Card card1 = ...
Card card2 = ...

if(card1.getnumber() < card2.getnumber())
{
//card1 is lower
}else if(card1.getnumber() > card2.getnumber()){
//card2 ist lower
}else{
if(card1.getsuit() < card2.getsuit()){
//card1 is lower
}else if(card1.getsuit() > card2.getsuit()){
//card2 is lower
}else{
// Both Cards are the same
}
}

请记住,在此实现中,套装数组的顺序很重要

关于java - 如何从纸牌游戏字符串数组中获取int值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20484453/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com