I am writing a program that does a card trick, but first I have to figure out how to play 3 rows of 7 cards. I canβt understand why, when I run my program, it displays only one number for all my cards and how I can print its name. I can get it to print 52 unique cards with the PrintCard () function, but not when I use Deal (), and I don't know why any help is appreciated. Thanks.
This is my code:
import java.util.Random; public class testing { public static void main(String[] args) { testing cs = new testing(); int [] deck = new int [52]; int [] [] play = new int [7] [3]; cs.BuildDeck (deck); cs.Deal(deck, play); } public static void BuildDeck(int deck[]) { int[] used = new int[52]; int card = 0; int i = 0; /* * Generate cards until the deck is full of integers */ while (i < deck.length) { /* * generate a random number between 0 and 51 */ Random random1 = new Random(); card = (random1.nextInt(52) % 52); /* * Check the used array at the position of the card. If 0, add the card and set the used location * to 1. If 1, generate another number */ if (used[card] == 0) { used[card] = 1; deck[i] = card; i++; } } } public static void PrintCard(int card) { int rank = (card % 13); int suit = (card / 13); String[] suits = {"Clubs", "Hearts", "Diamonds", "Spades"}; String[] ranks = {"King", "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen"}; System.out.println(ranks[rank] + " of " + suits[suit]); } public static void Deal(int deck[], int play[][]) { int card = 0; System.out.println ("\n Column 0 Column 1 Column 2"); System.out.println ("=======================================================\n"); for (int row = 0; row < play.length; row++) { for (int col = 0; col < play [row].length; col++) { play[row][col] = deck[card++]; System.out.print(" " + play[row][col] + " "); } System.out.println (); } } }
great program: http://pastebin.com/99tLpVjM
source share