Java code for selecting and comparing maps (without Arraylist)

I see a lot of questions like this, but they are answered by code that I have not yet studied. I should be able to complete this task without an arraylist, so this creates a problem for me. I need to play the war: create a card class, create a deck, and then randomly create the first card, “remove” the card from the deck and create the second card to see which card won, continue this comparison until the deck is exhausted. Here is what I still have:

public class Card
{
private int cardValue; 
private String cardSuit;
private String stringValue;
static final String[] SUIT = {"Clubs", "Diamonds", "Hearts", "Spades"}; 
static final String[] VALUE =  {"Ace","Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"}; 


/**
*CONSTRUCTOR for Card class
*contains the arrays for both suit and values
**/
public Card (int cardS, int cardV)
{

    cardSuit = SUIT[cardS]; 
    stringValue = VALUE[cardV];
}

/**
*Get method for card suit, access the value of cardSuit
**/
public String getCardSuit()
{
        return cardSuit;
}

/**
*get method for card VALUES
**/
public String getValue()
{
    return stringValue;
}
//in order to display string value of cards
public String toString() 
{ 
    return String.format("%s of %s", stringValue, cardSuit); 
} 

}

public class Deck
    {
    private final int HIGH_SUIT=3;
    private final int HIGH_VALUE=12;

    int i = 0; 
    int cardCount;              

    Card[] fullDeck = new Card[52]; 
    public Deck()
    {

        for(int s = 0; s <= HIGH_SUIT; s++)
        {
            //for loop to determine the suit
            for (int v = 0; v <= HIGH_VALUE; v++)
            {
                //construct all 52 cards and print them out 
                fullDeck[i] = new Card(s, v);
                cardCount = (i + 1);
                i++;//increment the card counter

            }
        }   
    }

}

public class War3

{
public static void main(String[] args)
{
    int i=0;
    Deck[] fullDeck = new Deck[52]; //set up the deck
    //create a random value for the first card
    int r = ((int) (Math.random() * 100) % 51);//create random number

    Card[] playerCard = new Card[r];//create the player card

    System.out.println("The card is: " + playerCard.toString());


    }

, 3, , . : : [LCard; @4a5ab2random # 1 ? , , , . .

+5
2

toString Card.

public String toString() {
    return String.format("%s of %s", stringValue, cardSuit);
}

, (suit value) , static final - :

static final String[] SUIT = {"Clubs", "Diamonds", "Hearts", "Spades"};
static final String[] VALUE = {"Ace","Two", "Three", "Four", "Five", "Six", "Seven",
                "Eight", "Nine", "Ten", "Jack", "Queen", "King"};

, :

Card[] playerCard = new Card[r];//create the player card

r playerCard. : Deck, :

public Card takeRandomCard() {
    int r;
    do {
        r = (int)(Math.random() * 52);
    } while (taken[r]);
    taken[r] = true;
    return fullDeck[r];
}

: taken - 52 boolean , , .

ideone.

+2

Card.cardSuit Card.stringValue . .

52 . .

Deck[] fullDeck = new Deck[52];

Deck fullDeck = new Deck();

, 52 . . , r, Deck:

fullDeck.fullDeck[r]

. :

System.out.println("The card is: " + fullDeck.fullDeck[r].getCardSuit() + fullDeck.fullDeck[r].getValue() + "random#" + r);

new .

0

All Articles