我需要帮助创建一个Java卡游戏:
到目前为止,我只能让我的程序从一个牌组中抽出一次牌。我需要一遍又一遍地从n个牌组中抽出n张牌,直到没有剩下的牌,然后程序需要重置。
我为这个程序制作了多个课程:
卡类:
public class Card{
private int rank, suit;
private static String[] suits = { "hearts", "spades", "diamonds", "clubs" };
private static String[] ranks = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" };
Card(int suit, int rank)
{
this.rank=rank;
this.suit=suit;
}
public @Override String toString()
{
return ranks[rank] + " of " + suits[suit];
}
public int getRank() {
return rank;
}
public int getSuit() {
return suit;
}
}
甲板课程:
public class Deck {
private ArrayList<Card> cards;
Deck()
{
cards = new ArrayList<Card>();
int index_1, index_2;
Random generator = new Random();
Card temp;
for (int a=0; a<=3; a++)
{
for (int b=0; b<=12; b++)
{
cards.add( new Card(a,b) );
}
}
for (int i=0; i<100; i++)
{
index_1 = generator.nextInt( cards.size() - 1 );
index_2 = generator.nextInt( cards.size() - 1 );
temp = (Card) cards.get( index_2 );
cards.set( index_2 , cards.get( index_1 ) );
cards.set( index_1, temp );
}
}
public Card drawFromDeck(Scanner console)
{
return cards.remove(0);
}
public int getTotalCards()
{
return cards.size();
}
public int getNumberOfDecks(Scanner console){
int tempNumDecks = 0;
boolean isOk = false;
System.out.println("How many decks would you like to use?");
do{
try{
String userInput = console.nextLine();
tempNumDecks = Integer.parseInt(userInput);
if(tempNumDecks <= 0) {
System.out.println ("Invalid input. Enter a number > 0");
isOk = false;
}//end if
else{
isOk = true;
}//end else
}//end try
catch(Exception e){
System.out.println("Enter an integer greater than 0:");
}//end catch
}while(isOk == false);
return tempNumDecks;
}//end getNumberOfDecks()
public int getNumberOfCards(Scanner console){
int tempNumCards = 0;
boolean isOk = false;
System.out.println("How many cards would you like to deal?");
do{
try{
String userInput = console.nextLine();
tempNumCards = Integer.parseInt(userInput);
if(tempNumCards <= 0) {
System.out.println ("Invalid input. Enter a number > 0");
isOk = false;
}//end if
else{
isOk = true;
}//end else
}//end try
catch(Exception e){
System.out.println("Enter an integer greater than 0:");
}//end catch
}while(isOk == false);
return tempNumCards;
}//end getNumberOfCards()
}
CardDealer类:
public class CardDealer {
public static void main(String[] args) {
System.out.println("Welcome to Shuffle!");
Scanner console = new Scanner(System.in);
Deck deck = new Deck();
Card card;
int decks = deck.getNumberOfDecks(console);
int cards = deck.getNumberOfCards(console);
while(deck.getTotalCards() != (52-cards)){
card = deck.drawFromDeck(console);
System.out.println( card.toString() );
}
}
}
代码输出:
Welcome to Shuffle!
How Many decks would you like to use? (USER INPUT: 4) <---This is unused
How many cards would you like to deal? (USER INPUT: 5) <---This actually works
7 of diamonds
3 of diamonds
Queen of spades
8 of hearts
Jack of spades
所以,我得到了多张牌,但没有多张牌。请帮我解决这个问题,我对java和StackOverflow很新。
如果我能提供任何其他信息,请告诉我(这是我的第一篇文章:x)
谢谢。
编辑::我创建了一个包含用户决定的多个套牌的arrayList。但是,我在从多个卡片中抽取卡片时遇到问题。这只从一个套牌中抽出一套牌。
package javacards;
import java.util.ArrayList;
import java.util.Scanner;
public class CardDealer {
private static ArrayList<Deck> decks;
public static void main(String[] args) {
System.out.println("Welcome to Shuffle!");
Scanner console = new Scanner(System.in);
Deck deck = new Deck();
int numdecks = deck.getNumberOfDecks(console);
decks = new ArrayList<Deck>();
Card card;
int cards = deck.getNumberOfCards(console);
for (int a=0; a < numdecks; a++){
decks.add( a , new Deck() );
}
while(deck.getTotalCards() != (52-cards)){
card = deck.drawFromDeck(console);
System.out.println( card.toString() );
}
}
}
答案 0 :(得分:0)
要使用多个套牌,请创建ArrayList<Deck>
。这与您已经拥有的ArrayList<Card>
非常相似。
要继续直到牌组为空,请在isEmpty()
班级添加Deck
方法并使用while
圈。