简体   繁体   English

如何从纸牌游戏字符串数组获取整数值

[英]how to get int values from card game string array

I'm trying a create a card game of war. 我正在尝试创建纸牌战争。 Each player draws 1 card at a time, up to 26 each. 每个玩家一次抓一张卡,每张最多26张。 Right now I need to turn the string values of the cards into ints so I can use them to determine who has the higher hand. 现在,我需要将卡片的字符串值转换为整数,以便可以使用它们来确定谁的手牌更高。 This is my first program using arrays and I've been having a lot of problems with this program. 这是我的第一个使用数组的程序,我在使用该程序时遇到了很多问题。 How would I go about doing this? 我将如何去做呢?

EDIT : Incase it isn't clear, I'm asking how to get an int value from String [] numbers without manually assigning any values. 编辑 :如果不清楚,我在问如何从String []数字中获取一个int值,而无需手动分配任何值。 In order to see who wins each hand I need to compare ints. 为了查看谁赢了每一局,我需要比较整数。

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

First of all make suits and numbers static in class Card. 首先,将西服上的西装和数字设为静态。 Because the arrays always keep the same and don't have to be initialized every time you create a card. 因为阵列始终保持相同,因此不必在每次创建卡时都进行初始化。 Although you have to add getters to the variables suit and number in the Card class. 虽然您必须在Card类中将getter添加到变量suit和number中。

To check which card is higher, you first have to check which numbers index is higher, after that you check for the suits index of a card. 要检查哪个卡更高,您首先必须检查哪个数字索引更高,之后再检查卡的西服索引。

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

Keep in mind that with this implementation the order of the suits array is important 请记住,在此实现中,suits数组的顺序很重要

To convert the String object into primitive int just use the Integer.parseInt() method as follows: 要将String对象转换为原始int只需使用Integer.parseInt()方法,如下所示:

String card1="12";
String card2="20";

int Card1=Integer.parseInt(card1);     
int Card2=Integer.parseInt(card2);

if(Card1>Card2)
{
    System.out.println("You Win!");
}

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

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