Java Crew

Discussion in 'OT Technology' started by Senor Spielbergo, Jun 8, 2005.

  1. Senor Spielbergo

    Senor Spielbergo New Member

    Joined:
    Oct 11, 2004
    Messages:
    534
    Likes Received:
    0
    Location:
    Toronto
    I need to shuffle an array (its a blackjack program). As of right now, my code seems to be suffling...but really badly. It is duplicating some values. Here is a sample of my code:

    PHP:
             public void shuffle()
             { 
    int[] order = new int[this.cardList.length];
                
    int count 0;
          
                while(
    count order.length)
                {  
    boolean alreadyExists false;
                   
    int index = (int)(Math.random()*order.length+1);
          
                   for(
    int i 0count 1i++)
                   {  if(
    count && order[i] == index-1)
                      {  
    alreadyExists true;
                      }
                   }
          
                   if(!
    alreadyExists)
                   {  
    order[count] = index 1;
                      
    count++;
                   }
                }
          
                
    Card[] temp = new Card[this.cardList.length];
          
                for(
    int i=0order.lengthi++)
                { 
    temp[i] = this.cardList[order[i]];
                   
    System.out.println(order[i]);
                }
          
               
    this.cardList temp;
          
             }
          
    any idea what i'm doing wrong :dunno:

    edit: i forgot to mention, i cant use Collections.shuffle :o
     
    Last edited: Jun 8, 2005
  2. Jezza

    Jezza OT Supporter

    Joined:
    Oct 27, 2003
    Messages:
    54
    Likes Received:
    0
    Location:
    Perth Australia
    If you're getting duplicates values, that would indicate that alreadyExists is sometimes evaluating to false when it should evaluate to true.

    alreadyExists can only be set to true is when this if statement evaluates to true.

    Code:
    if(count > 0 && order[i] == index-1)
    Therefore this if statement is evaluating to false too frequently.

    I've no idea how you're trying to reference elements in order[] (you seem to start counting from 1 in some places, and then start counting from 0 in others), but this line is a bit concerning:
    Code:
    int index = (int)(Math.random()*order.length+1);
    Firstly you're doing mixed mode arithmatic (multiplying a floating point number by an integer), then you add 1 to it which gives you your final value of index. Then, every other time you use the value index on these lines, you subtract 1 from it. I'm guessing that you've messed up the parenthesisation of the Math.random line.
     
  3. pwd

    pwd Guest

    try it this way (will have to change variable names etc)

    Random generator = new Random();
    int num1,num2;
    int temp[][] ={{0,0}};
    // loop through the array, effectively
    // swapping two cards' positions 52 times.
    for( int i=0;i<cards.length;i++)
    {
    // choose the first card
    num1= generator.nextInt(); //generate random numbers
    num1=Math.abs(num1); //making sure they are positive
    num1= num1%51; //making sure no is less than the pack of cards

    // choose the second card to swap with the first
    num2= Math.abs(generator.nextInt())%51;
    temp[0]= cards[num1];
    cards[num1]= cards[num2];
    cards[num2]= temp[0];
    }
     
  4. Furner

    Furner New Member

    Joined:
    Jan 7, 2003
    Messages:
    7,584
    Likes Received:
    0
    Location:
    Grand Rapids, MI
    If you want, I have a card class and a deck class that has shuffle functions built into it. its a really neat, versitile program that you can use for pretty much any card game
     

Share This Page