- Joined
- Jan 17, 2004
ok i will start by saying this is some pretty sloppy work. but !! i threw it together rather quickly .. im having problem with one thing though. As i tried getting help off google i just confused myself ( as if i didnt do that already writing this hunk o' ) .. anyway i was thinking maybe someone could look at what i got and tell me if i need a few more loops or what ..
1. Everything is correct as required for homework except when i shuffle the array i get repeats.
2. I need to figure a way out to catch these repeats and ommit them .
a. i was thinking i could load the random number into an array. then before the loop executes again i could scan the new random number to make sure it doesnt match the one stored in the array[0]. when loop finds that it already is in array it throws a bool and exits the loop. decreasing the counter variable for a "bad find" so it will still execute until it finds all 52 variables.
b. I think im shuffling it 100x ..lol right ???
c. some of that stuff is so it will align to a required output as requested by the teach..
but thats just a noob thinking ..lol
here is my stuff ...laugh if you may but help if you can
1. Everything is correct as required for homework except when i shuffle the array i get repeats.
2. I need to figure a way out to catch these repeats and ommit them .
a. i was thinking i could load the random number into an array. then before the loop executes again i could scan the new random number to make sure it doesnt match the one stored in the array[0]. when loop finds that it already is in array it throws a bool and exits the loop. decreasing the counter variable for a "bad find" so it will still execute until it finds all 52 variables.
b. I think im shuffling it 100x ..lol right ???
c. some of that stuff is so it will align to a required output as requested by the teach..
but thats just a noob thinking ..lol
here is my stuff ...laugh if you may but help if you can
Code:
public class Deck_Cards
{
public static void main(String[] args)
{
Display_Deck.Display();
Display_Deck.Shuffle();
}
Code:
import java.util.*;
public class Display_Deck
{
static final int[] numbers = new int[]{1,2,3,4,5,6,7,8,9,10,11,12,13};
static final char[] suit = new char[]{'H','C','D','S'};
public static void Display()
{
int count = 0;
int count2;
System.out.printf("%33s \n", "Card deck is loaded as follows.");
System.out.printf("%49s\n", "H = Hearts C = Clubs D = Diamonds "
+ "S = Spades");
System.out.printf("%5s %50s %6s %5s \n", "Ace", "Jack", "Queen", "King");
for (count2 = 0;count2 < 13; count2++)
{
System.out.printf("%3s%s ", Display_Deck.suit[count],
numbers[count2]);
}
count++;
System.out.printf("\n");
for (count2 = 0;count2 < 13; count2++)
{
System.out.printf("%3s%s ", Display_Deck.suit[count], numbers[count2]);
}
count++;
System.out.printf("\n");
for (count2 = 0;count2 < 13; count2++)
{
System.out.printf("%3s%s ", Display_Deck.suit[count], numbers[count2]);
}
count++;
System.out.printf("\n");
for (count2 = 0;count2 < 13; count2++)
{
System.out.printf("%3s%s ", Display_Deck.suit[count], numbers[count2]);
}
System.out.printf("\n");
}
public static void Shuffle()
{
System.out.printf("\n%s\n\n"," Deck reshuffled 100x resulting in the following "
+ "redistribution.");
int deck2 =0;
while (deck2 < 4)
{
for (int deck = 0; deck < 13; deck++)
{
Random rgen = new Random();
int count100;
for (count100 = 0; count100 < 100; count100++)
{
//--- Shuffle by exchanging each element randomly
for (int i=0; i< numbers.length; i++)
{
int randomPosition = rgen.nextInt(numbers.length);
int temp = numbers[i];
numbers[i] = numbers[randomPosition];
numbers[randomPosition] = temp;
}
}
Random suit2 = new Random();
int count4;
count4 = suit2.nextInt(4);
if (numbers[count4] < 10)
{
System.out.printf(" %1s%s ",suit[count4],numbers[count4]);
}
else if (numbers[count4] > 9)
{
System.out.printf(" %1s%s",suit[count4],numbers[count4]);
}
}
System.out.printf("\n");
deck2++;
}
}