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
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:
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?
You could improve the design by creating a Player class which contains the methods you are calling. A simple implementation would look like this.
now instead of having 2 arrays you simply have a single array containing each player. As you loop through this array you give the player their cards by using the
setA()andsetB()methods. Once every player has cards, you loop through the array again and usegetA()andgetB()to print the cards out. Usingpoints()gives you an easy way to calculate the points for every players hand without dragging around the extra array.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. This would let you use the same system for oter card games which might not only use 2 cards.