• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

SOLVED JAVA DUDES - HELP !! :)

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

Xtreme Barton

Member
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' :bang head) .. 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 :comp:


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++;
    } 
}
 
i realize what i did in the first part.. i could put them three into a single loop .. dont know why i did that other than just being weird.

Also i realize that this is a horrible way to do so. i just read a little better thread about shuffling and i should have went about this completely different.

i should have stored the arrays into a list of 52 then i could have shuffled that list 100x and then just looped output till counter hit 51.


but i think the guys who wrote that were def not noobs :)
 
Last edited:
Have you considered making a card object with Suit and Value as final int/enum values? doing this would allow you to initialize a deck of cards and only have one copy of each card.

to shuffle
Code:
Random randy = new Random();
for (int i=0; i<1000; i++){
  int rIndex1 = randy.nextInt(51);
  int rIndex2 = randy.nextInt(51);
  Card temp = deck[rIndex1];
  deck[rIndex1] = deck[rIndex2];
  deck[rIndex2] = temp;
}
 
I second what ssjwizard said. Make a habit out of creating an object out of anything that needs to contain multiple variables, or sometimes even something that contains one variable that needs to be modified with rules (at least in a web environment it's a good habit to make an object out of anything received as a parameter quite quickly).

Not only will it make it easier to comprehend, but also when an application get's more complicated it will reduce the amount of places you need to implement a fix in (validation only needs to be added to setters etc).
 
My teach gave this to me also .. i get so confused (why i dont know) when i try to go back over code .. ill probably end up re-writing this (dam no sleep this week again :( ) ..

pgrs-sm.gif

clear1x1.gif
clear1x1.gif








clear1x1.gif
clear1x1.gif






clear1x1.gif
clear1x1.gif
clear1x1.gif


Code:
[SIZE=2]It' a swap to reach the next card. Yes, I did give the farm away.[/SIZE]
[SIZE=2]for (int i = 0; i < 100; i++)[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2]String savedCard = "";[/SIZE]
[SIZE=2]int variant = ((int)(Math.random() * 50)) + 1;[/SIZE]
[SIZE=2]for (int j = 0; j < cards.length; j++)[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2]if (j + variant < cards.length)[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2]savedCard = cards[j];[/SIZE]
[SIZE=2]cards[j] = cards[j + variant];[/SIZE]
[SIZE=2]cards[j + variant] = savedCard;[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2]}[/SIZE]
 
gonna post up what i got pretty soon .. trying to see if i can determine length of string before output so i can adjust alignment ..


i have strings which are two characters long and i have strings of three characters long .. i need to determine which is going to be displayed so i can put an IF statement in .

if (stringLength < 3)

System.out.printf("%s", deck);

else if (stringLength > 2)

System.out.printf("%s ", deck)
 
Code:
if (String.length() == 3){
//no space
}
else if (String.length() == 2){
//add space
}
else {
//error
}

or 

switch(String.length()){
  case 2:
      //print with space
      break;
  case 3:
      //print without space
      break;
  default:
      //error
}
 
If you want to go the fancier printf route, you're probably looking for:
Code:
System.out.printf("%-3s blah blah blah", deck[i]);
The minus sign tells Java to do left alignment (it normally does right-alignment), the "3" indicates you want the field to take up no more than 3 spaces, and the "s" tells java to expect a String.

Personally though, even with me doing mostly C programming nowdays, I still find the format specifies of printf hard to read.

JigPu
 
Indeed javas string formatting in general is poor and unintuitive. Thats about the only thing about C# that I prefer.
 
Ok this is my final output guys !! YAY !! its works .. completely started over and used the teachers shuffle clue's to finish it up ..I see now how sad and sloppy my code was .. Re-reading the chapter helped allot too.

Code:
/** Murach, J. and Steelman, A., ( 2007).  Murach�s
 Java SE6, Training and Reference.  Fresno, CA: Mike Murach & Associates.
 Modifications by Wm Bowers, 2007 - 2012
 Additional Modifications Made By - D.Keller, March 2012
*/   

import java.util.*;

public class Deck_Assignment_10 
{
    private static String[] suits = {"H", "C", "D", "S"};
    private static String[] numbers = {"1","2","3","4","5","6","7",
                                        "8","9","10","11","12","13"};
    private static String[] cards = new String[52];
    
    
    public static void main(String[] args) 
    {
       
       loadCardArray();
       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");
       printCardArray();
       shuffleCards();
       System.out.printf("\n\n%65s\n\n","Deck reshuffled 100x resulting in "
               + "the following redistribution.");
       printCardArray();
       System.out.printf("\n\n%40s\n\n","Dealing four hands of five cards "
               + "each.");
       ShowCards();
       System.out.printf("\n\n%21s","New deck, new deal.");
       
       
    }
    
    private static void loadCardArray() 
    {
        int f = 0;
        int b = 0;
      
        for (int c = 0; c < 52; c++)
        {
            if (c == 13)
            {
                f++;
            }
            else if (c == 26)
            {
                f++;
            }
           else if (c == 39)
           {
                f++;
           }
            
            cards[c] = suits[f] + numbers[b];
           
            if (b == 12)
            {
                b = b - 13;
            }
            
            b++;
        }
    }
    
    
    private static void printCardArray() 
    {
      
       
       for (int c = 0; c < 52; c++)
       {
           if (c == 13)
           {
               System.out.printf("\n");
           }
           else if (c == 26)
           {
                System.out.printf("\n");
           }
           else if (c == 39)
           {
               System.out.printf("\n");
           }
            System.out.printf("  %-3s",cards[c]);
       }
    }
    
    private static void shuffleCards() 
    {
          for (int i = 0; i < 100; i++)
        {
            String savedCard = "";
            int variant = ((int)(Math.random() * 50)) + 1;
            for (int j = 0; j < cards.length; j++)
            {
                if (j + variant < cards.length)
                {
                    savedCard = cards[j];
                    cards[j] = cards[j + variant];
                    cards[j + variant] = savedCard;
                }
            }
        }
    }
  
    private static void ShowCards() 
    {
        int r = 0;
       
        for (int d = 0; d < 4;d++)
        {
           if (d == 1)
               System.out.printf("\n");
           else if (d == 2)
               System.out.printf("\n");
           else if (d == 3)
               System.out.printf("\n");
           
           System.out.printf("  %s %s ", "Player",d + 1 );
           for (int e = 0; e < 5; e++)
           {
               System.out.printf(" %-3s ",cards[r]);
               r++;
           }
        }
    }
}
 
Back