简体   繁体   English

发行了黑杰克印刷卡

[英]Black Jack Printing cards dealt

I need help in a black jack game. 我在黑杰克游戏中需要帮助。 I have a card deck in an array and every time i take a card out and deal it the array is reassgned to one less than the size. 我在阵列中有一个卡片组,每次我拿出一张卡片并进行处理时,阵列都会被重新确定为小于大小的一个。 So i have this loop that deals two cards to each nth player 所以我有一个循环,每个n位玩家发两张牌

        deck=createCardArray();
        shuffle(deck); 
        int[] players = createPlayers(n);
        int[] points = createPoints(n);

        for(int i = 1 ; i<= players.length; i++){
            Card a = dealCard(deck);
            updatePoints(players, points, i, a);
            Card b = dealCard(deck);
            updatePoints(players, points, i, b);
            printStats(players, points, a, b);
        }

What i want to do is to print out the cards and final points after dealing two cards to each player. 我要做的是在向每位玩家分发两张卡之后打印出卡和最终点数。 So I have this method printstats that looks like this: 所以我有这个方法printstats看起来像这样:

public static void printStats(int[] nplayers, int[] points, Card a, Card b){
    for (int i=0; i<nplayers.length;i++){
        System.out.println("Player " + nplayers[i] + ": Points " + points[i]);
        System.out.println("Card 1: " + a.showCardSuit() + " " + a.showCardValue());
        System.out.println("Card 2: " + b.showCardSuit() + " " + b.showCardValue());
        System.out.println();
    }
}

What it does it that it iterates every time and outputs the cards points and players each time. 每次迭代并每次输出纸牌点数和玩家的功能。 But when i run this it only gives the two cards of the last player. 但是当我运行它时,它只给出最后一个玩家的两张牌。 I understand why this happens. 我知道为什么会这样。 But i want a suggestion as how to fix this problem. 但是我想要一个如何解决此问题的建议。

Should I create a new 2d array length player and size 2Xn or is there another way I should do this? 我应该创建一个新的2d数组长度播放器并且大小为2Xn,还是应该采用其他方法?

You could improve the design by creating a Player class which contains the methods you are calling. 您可以通过创建一个包含要调用的方法的Player类来改进设计。 A simple implementation would look like this. 一个简单的实现看起来像这样。

   public class Player{
        Card a;
        Card b;

        public Card getA() {
            return a;
        }
        public void setA(Card a) {
            this.a = a;
        }
        public Card getB() {
            return b;
        }
        public void setB(Card b) {
            this.b = b;
        }

        public int points(){
            //calculation for returning point value
            int points = 0;
            return points;
        }

    }

now instead of having 2 arrays you simply have a single array containing each player. 现在,您不再需要2个数组,而是只拥有一个包含每个玩家的数组。 As you loop through this array you give the player their cards by using the setA() and setB() methods. 当您遍历此数组时,可以使用setA()setB()方法为玩家提供卡片。 Once every player has cards, you loop through the array again and use getA() and getB() to print the cards out. 一旦每个玩家都拥有纸牌,就可以再次遍历数组并使用getA()getB()来打印纸牌。 Using points() gives you an easy way to calculate the points for every players hand without dragging around the extra array. 使用points()为您提供一种简便的方法来计算每只玩家的手的点数,而不会拖累额外的数组。

You can make this even more useful if instead of having explicit A,B properties to hold the cards, you used a List or ArrayList. 如果您使用List或ArrayList而不是使用显式的A,B属性来存储卡,则可以使此功能更加有用。 This would let you use the same system for oter card games which might not only use 2 cards. 这样,您就可以将同一系统用于其他纸牌游戏,而该系统可能不仅使用2张纸牌。

A player "has-a" hand... so give the player his cards! 玩家“有”牌,所以给玩家他的牌! In other words, create a field that represents the players current cards. 换句话说,创建一个代表玩家当前卡的字段。 Then, when you deal a card, do player.hit(Card newCard) , then to show his hand, just do List<Card> player.getHand() 然后,当您发牌时,执行player.hit(Card newCard) ,然后要显示他的手,只需执行List<Card> player.getHand()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM