java - 无法为多个玩家创建手牌

标签 java

我的程序要求用户输入纸牌数量以及玩家数量。我只是想打印出每张球员的卡片。前任。 (玩家1:黑桃A,红心二。玩家2:梅花二,等等。)我已经打印出来了,但它似乎只打印出一套牌,尽管输入了2张牌。这是我的代码。

手类:

public class Hand 
{
	private int handSize;			//how many cards in the hand
	private int cardsInHand;		//counter
	private Card [] hand;
	
	
	public Hand ()
	{
		hand = new Card[52];
		handSize = 5;
		cardsInHand = 0;
	}
	
	public Hand (int handSize)
	{
		hand = new Card [handSize];
		this.handSize = handSize;
		
	}

	public void addCard (Card card)
	{
		if (cardsInHand >= handSize) 
		{
			Card[] temp = new Card[hand.length*2];
			for (int i=0; i < cardsInHand; i++)
			{	
				temp[i] = hand[i];
				hand = temp;
			}
		}
		
	}
甲板等级:
public class Deck 
{
	
	private Card [] deck;
	private int nextCard;
	
	
	public Deck()
	{
		deck = new Card[52];
        int iCardCount=0; 	// Holds how many cards have been created.
        for ( int suit = 0; suit <= 3; suit++ ) 
        {
            for ( int face = 1; face <= 13; face++ ) 
            {
                deck[iCardCount] = new Card(iCardCount);
                iCardCount++;
            }
        }
        nextCard = 0;
	}
 
    public Card dealACard ()
	{
		if (nextCard < 52)
		{
			System.out.println( deck[nextCard++]);
		}
		else
		{
			System.out.print("\nError, out of cards." );	
		}
		return (null);
	}
   
	public Hand dealAHand (int n)
	{	
		Hand hand = new Hand(n);
		Card deal = new Card(n);
		
	
		for (int i = 0; i < n; i++) 
		{ 
			hand = dealACard();
		}
		return hand;
	}

我的司机:

System.out.println ("How many cards in the hand? ");
int iHand = kb.nextInt();
Hand newHand = new Hand(iHand);
System.out.println("How many players are playing? ");
int iPlayers = kb.nextInt();
Deck secondDeck = new Deck();
secondDeck.shuffle();
secondDeck.dealAHand(iHand);

我得到的输出:

How many cards in the hand? 
5
How many players are playing? 
2
the TWO of SPADES
the ACE of HEARTS
the TEN of DIAMONDS
the QUEEN of CLUBS
the NINE of HEARTS

我希望它打印出两套卡片,而不是只打印一套,但从逻辑上讲,我不知道该怎么做。任何帮助都会很棒,谢谢。

最佳答案

你只有一个 Hand 实例,就像 D.Wallace 所说的那样,你只调用 dealAHand 一次。在开始编码之前,您应该了解解决方案的更多结构。

关于java - 无法为多个玩家创建手牌,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29223617/

相关文章:

java - 将环境变量作为参数

JavaFX - 将 Canvas 保存到 png 文件

java - 我可以仅使用 xhtml 文件声明复合组件并且仍然能够在 JAVA 代码中使用该组件吗

java - Properties.load() 是否报告无效字节

java - java 中的 xmlSchema apache

java - org.openqa.selenium.SessionNotCreatedException:无法创建新的远程 session 。在模拟器中初始化android驱动程序时

java - 每次我想调用 doInBackground() 时是否应该创建一个新的 Java SwingWorker 实例?

java - String.format() 将数组作为单个参数

java - Android Fragments 转换问题

java - 正则表达式 - 无法匹配模式 "(cmd: )"