试图编写一个程序,将5张随机牌交给扑克牌

时间:2018-04-13 15:06:50

标签: java

使用ThinkJava教科书第14章(对象的对象)和作为开始的地方提供的代码。我不知所措,迷失在哪里开始,什么工作。以下是我正在使用的所有代码。 “手”目前是“卡片收集”的延伸。

  

“编写一个原始的Java程序来实现扑克游戏的基本结构。该程序应该创建一副52张扑克牌,创建至少2个空手牌,并为每手牌交出5张随机牌。一种随机化方式这笔交易是先将套牌洗牌。程序应该通过在控制台上打印或用卡表代码绘制它们来显示手。“

给定定义卡片集的代码:

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

/**
 * A collection of playing cards.
 */

public class CardCollection {

    private String label;
    private ArrayList<Card> cards;

    /**
     * Constructs an empty collection.
     */
    public CardCollection(String label) {
        this.label = label;
        this.cards = new ArrayList<Card>();
    }

    /**
     * Returns the label of the card collection.
     */
    public String getLabel() {
        return label;
    }

    /**
     * Adds the given card to the collection.
     */
    public void addCard(Card card) {
        cards.add(card);
    }

    /**
     * Removes and returns the card with the given index.
     */
    public Card popCard(int i) {
        return cards.remove(i);
    }

    /**
     * Removes and returns the last card.
     */
    public Card popCard() {
        int i = size() - 1;
        return popCard(i);
    }

    /**
     * Returns the number of cards.
     */
    public int size() {
        return cards.size();
    }

    /**
     * True if the collection is empty, false otherwise.
     */
    public boolean empty() {
        return cards.size() == 0;
    }

    /**
     * Moves n cards from this collection to the given collection.
     */
    public void deal(CardCollection that, int n) {
        for (int i = 0; i < n; i++) {
            Card card = popCard();
            that.addCard(card);
        }
    }

    /**
     * Moves all remaining cards to the given collection.
     */
    public void dealAll(CardCollection that) {
        int n = size();
        deal(that, n);
    }

    /**
     * Returns the card with the given index.
     */
    public Card getCard(int i) {
        return cards.get(i);
    }

    /**
     * Returns the last card.
     */
    public Card last() {
        int i = size() - 1;
        return cards.get(i);
    }

    /**
     * Swaps the cards at indexes i and j.
     */
    public void swapCards(int i, int j) {
        Card temp = cards.get(i);
        cards.set(i, cards.get(j));
        cards.set(j, temp);
    }

    /**
     * Randomly permute the cards.
     */
    public void shuffle() {
        Random random = new Random();
        for (int i = size() - 1; i > 0; i--) {
            int j = random.nextInt(i);
            swapCards(i, j);
        }
    }

    /**
     * Returns a string representation of the card collection.
     */
    public String toString() {
        return label + ": " + cards.toString();
    }

}

Code that I have modified a little to build the hand of cards

/**
 * A hand of playing cards.
 */

public class Hand extends CardCollection {

    /**
     * Constructs an empty hand.
     */
    public Hand(String label) {
        super(label);
    }

    /**
     * Prints the label and cards.
     */
    public void display() {
        System.out.println(getLabel() + ": ");
        for (int i = 0; i < size(); i++) {
            System.out.println(getCard(i));
        }
        System.out.println();
    }

    public static void main()
    {
        Hand h = new Hand("one");
        Card c = new Card(13,3); //(rank, suit)
        h.addCard(c);
        System.out.println(h);
    }

}

Defines what a card is

/**
 * A standard playing card.
 */

public class Card {

    public static final String[] RANKS = {
        null, "Ace", "2", "3", "4", "5", "6", "7",
        "8", "9", "10", "Jack", "Queen", "King"};

    public static final String[] SUITS = {
        "Clubs", "Diamonds", "Hearts", "Spades"};

    private final int rank;

    private final int suit;

    /**
     * Constructs a card of the given rank and suit.
     */
    public Card(int rank, int suit) {
        this.rank = rank;
        this.suit = suit;
    }

    /**
     * Returns a negative integer if this card comes before
     * the given card, zero if the two cards are equal, or
     * a positive integer if this card comes after the card.
     */
    public int compareTo(Card that) {
        if (this.suit < that.suit) {
            return -1;
        }
        if (this.suit > that.suit) {
            return 1;
        }
        if (this.rank < that.rank) {
            return -1;
        }
        if (this.rank > that.rank) {
            return 1;
        }
        return 0;
    }

    /**
     * Returns true if the given card has the same
     * rank AND same suit; otherwise returns false.
     */
    public boolean equals(Card that) {
        return this.rank == that.rank
            && this.suit == that.suit;
    }

    /**
     * Gets the card's rank.
     */
    public int getRank() {
        return this.rank;
    }

    /**
     * Gets the card's suit.
     */
    public int getSuit() {
        return this.suit;
    }

    /**
     * Returns the card's index in a sorted deck of 52 cards.
     */
    public int position() {
        return this.suit * 13 + this.rank - 1;
    }

    /**
     * Returns a string representation of the card.
     */
    public String toString() {
        return RANKS[this.rank] + " of " + SUITS[this.suit];
    }

}

The deck to work from

import java.util.Arrays;
import java.util.Random;

/**
 * A deck of playing cards (of fixed size).
 */

public class Deck {

    private Card[] cards;

    /**
     * Constructs a standard deck of 52 cards.
     */
    public Deck() {
        this.cards = new Card[52];
        int index = 0;
        for (int suit = 0; suit <= 3; suit++) {
            for (int rank = 1; rank <= 13; rank++) {
                this.cards[index] = new Card(rank, suit);
                index++;
            }
        }
    }

    /**
     * Constructs a deck of n cards (null).
     */
    public Deck(int n) {
        this.cards = new Card[n];
    }

    /**
     * Gets the internal cards array.
     */
    public Card[] getCards() {
        return this.cards;
    }

    /**
     * Displays each of the cards in the deck.
     */
    public void print() {
        for (int i = 0; i < this.cards.length; i++) {
            System.out.println(this.cards[i]);
        }
    }

    /**
     * Returns a string representation of the deck.
     */
    public String toString() {
        return Arrays.toString(this.cards);
    }

    /**
     * Chooses a random number between low and high, including both.
     */
    public int randomInt(int low, int high) {
        return 0;
    }

    /**
     * Swaps the cards at indexes i and j.
     */
    public void swapCards(int i, int j) {
    }

    /**
     * Randomly permutes the array of cards.
     */
    public void shuffle() {
    }

    /**
     * Finds the index of the lowest card
     * between low and high inclusive.
     */
    public int indexLowest(int low, int high) {
        return 0;
    }

    /**
     * Sorts the cards (in place) using selection sort.
     */
    public void selectionSort() {
    }

    /**
     * Returns a subset of the cards in the deck.
     */
    public Deck subdeck(int low, int high) {
        Deck sub = new Deck(high - low + 1);
        for (int i = 0; i < sub.cards.length; i++) {
            sub.cards[i] = this.cards[low + i];
        }
        return sub;
    }

    /**
     * Combines two previously sorted subdecks.
     */
    public static Deck merge(Deck d1, Deck d2) {
        return null;
    }

    /**
     * Returns a sorted copy of the deck using merge sort.
     */
    public Deck mergeSort() {
        return this;
    }

    /**
     * Reorders the cards (in place) using insertion sort.
     */
    public void insertionSort() {
    }

}

1 个答案:

答案 0 :(得分:1)

首先你的虚拟主要需要创建套牌而不是卡

public static void main()
{
    Hand h = new Hand("one");
    Deck D = new Deck();
    h.addCard(c);
    System.out.println(h);
}

然后你必须建立你的功能来洗牌和交易卡

在里面的甲板上有一个叫做卡片的阵列,你必须置换那个阵列 你需要这样的想法

public void shuffle()
{
    //code to shuffle your deck
}

最后交易卡应该处理你的牌组的顶牌,然后从牌组中取出卡

int dealcardindex = 0;

    public card Dealcard()
    {
            Card DealedCard = this.Cards[dealcardindex];
            dealcardindex++;
            return DealedCard ;
    }

最后,你的虚空主

上会有类似的东西
public static void main()
{
    Hand h1 = new Hand("one");
    Hand h2 = new Hand("two");
    Deck D = new Deck();
    Deck.Shuffle();
    h1.addCard(Deck.Dealcard());
    h1.addCard(Deck.Dealcard());
    h1.addCard(Deck.Dealcard());
    h1.addCard(Deck.Dealcard());
    h1.addCard(Deck.Dealcard());
    h2.addCard(Deck.Dealcard());
    h2.addCard(Deck.Dealcard());
    h2.addCard(Deck.Dealcard());
    h2.addCard(Deck.Dealcard());
    h2.addCard(Deck.Dealcard());
    System.out.println(h);
}