Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
149 views
in Technique[技术] by (71.8m points)

java - Using if statements to draw specific cards from a deck

I'm trying to create a for loop that takes out certain cards from a deck, but the way I'm trying to do it seems like I'm just coding for every single scenario. There must be a better way to do this and I was wondering if I could get some feedback.

(question goal) Card games have always been very popular and this popularity has found its way to the Internet. Write a very simple program that has a deck of 52 cards, 13 Spades, 13 Diamonds, 13 Hearts and 13 Clubs, each set consisting of the values 2 through 10 and the “picture cards”, Jack, Queen, King, and Ace. Use an ArrayList of Integers in the range [1, 52] to represent the cards. You may assume that the values 1, 13 represent the cards Ace, 2, 3, ..., Queen, King of Spades, the next 13 represent the same sequence of Diamonds, the next Hearts and the values [40, 52] represent Ace, 2, 3, ..., Queen, King of Clubs. Your program should randomly draw five cards from the simulated deck of cards and tell the user which five were drawn.

package com.company;

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

public class Main {

    public static void main(String[] args) {
        String spades = "spades";
        String diamonds = "diamonds";
        String hearts = "hearts";
        String clubs = "clubs";

        ArrayList<Integer> deck = new ArrayList<Integer>(52);
        for (int i = 0; i <= 52; i++) {
            deck.add(i);
        }
        ArrayList<Integer> hand = new ArrayList<Integer>(5);
        drawCards(deck, hand);
    }


    private static void drawCards(ArrayList<Integer> deck, ArrayList<Integer> hand) {
        for (int i = 0; i < 5; i++) {
            int random = getRandomNumbersInRange(1, 52);
            hand.add(random);
            deck.set(random,-1);

            if (random == 1) {
                System.out.println("Card " + (i + 1) + " is the ace of spades");
            }
            if (random == 14) {
                System.out.println("Card " + (i + 1) + " is the ace of diamonds");
            }
            if (random == 27) {
                System.out.println("Card " + (i + 1) + " is the ace of hearts");
            }
            if (random == 40) {
                System.out.println("Card " + (i + 1) + " is the ace of clubs");
            }
            if (random >= 2 && random <= 10) {
                System.out.println("Card " + (i + 1) + " is a " + random + " of spades");
            }
            if (random >= 15 && random <= 23) {
                System.out.println("Card " + (i + 1) + " is a " + (random - 13) + " of diamonds");
            }
            if (random >= 28 && random <= 36) {
                System.out.println("Card " + (i + 1) + " is a " + (random - 26) + " of hearts");
            }
            if (random >= 41 && random <= 49) {
                System.out.println("Card " + (i + 1) + " is a " + (random - 39) + " of clubs");
            }



            if (random==11 ||random==24||random==37||random==50){
                System.out.println("Card " + (i + 1) + " is a Jack of ");
            }

        }

    }

    private static int getRandomNumbersInRange(int min, int max) {
        if (min >= max) {
            throw new IllegalArgumentException();
        }
        Random r = new Random();
        return r.nextInt((max - min) + 1) + min;
    }

}
question from:https://stackoverflow.com/questions/65836275/using-if-statements-to-draw-specific-cards-from-a-deck

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can try this

enum Suit{
    Spades,hearts,
    diamonds,clubs;
}

enum Value{
    Ace,Two,Three,Four,
    Five,Six,Seven,Eight,
    Nine,Ten,Jack,Queen,King;
}

class Card{
    private Suit suit;
    private Value value;

    Card(Suit suit, Value value){
        this.suit = suit;
        this.value = value;
    }

    Suit getSuit(){
        return suit;
    }

    Value getValue(){
        return value;
    }

}

public static void main(String[] args){
        ArrayList<Card> deck = new ArrayList<>();

        for(Suit suit : Suit.values()) {
            for(Value value : Value.values()) {
                /*add new card into the deck
                with suite and value*/
                deck.add(new Card(suit, value));
            }
        }

        Collections.shuffle(deck);

        //print the first 5 because collection was shuffled
        for(int i = 0; i < 5; i++){
            System.out.println(deck.get(i).getValue() + " of " + deck.get(i).getSuit());
        }

    }  

This answer combines both @Andreas(Using enums) and @Gilbert Le Blanc(Shuffling the collections and printing the first 5 elements)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...