Solved

Moving the contents at the start of an array to the end

Posted on 2004-10-19
6
198 Views
Last Modified: 2010-03-31
Heya people,
Could kind of do with some help :P ...  I'm trying to write a card game in Java at the minute, and I've been trying to work out how to 'split' the deck as such for the last 3 hours, and everything I've tried just doesn't seem to be working, I dont think its anything difficult to do as such, but I just dont know how to do it, lol.

I have an array of 52 elements, 0-51, representing all of the cards in a full deck...  What I need to do, is find a way to split the deck, at a random point, and then move the contents from the end of the array to the beginning...

I've tried to copy the contents across to a temporary array and stuff, but it didn't seem to work, and I'm not to sure if I was even doing it the right way..

All I've got so far really as far as the 'split' function goes is:

      public void cut()
      {
            int rand = (int)(Math.random()*(52));      //      0 * 52 = 0;      1 * 52 = 52; generates a number between these two.
      }

Lol, not a fat lot I know, but I've been trying for the last 2 hours, and I've not really gotten anywhere...  I think I've managed to work out how to do the shuffle function thought :-) even though i expected it to be harder to do than the cut( )

- >

    public void shuffle()
    {
        for ( int i = 51; i > 0; i-- )
        {
            int rand = (int)(Math.random()*(i+1));
            Card temp = deck[i];
            deck[i] = deck[rand];
            deck[rand] = temp;
        }
        cardsDealt = 0;
    }

and it seems to work, but as far as the other function goes, although I get the feeling its something pretty simple, its making me want to throw my laptop across the room now...  please help, I'm a student, and I really cant afford a new one, he he...  any pointers in the right direction even would be much appreciated...

Thanks in advance.
0
Comment
Question by:ecksplosive
  • 2
  • 2
  • 2
6 Comments
 
LVL 13

Assisted Solution

by:Webstorm
Webstorm earned 40 total points
ID: 12348804
Hi ecksplosive,

Use System.arraycopy :

     public void cut()
     {
          int rand = (int)(Math.random()*(52));     //     0 * 52 = 0;     1 * 52 = 52; generates a number between these two.
          Card[] tmp=new Card[rand];

          System.arraycopy(deck,0,tmp,0,rand);
          System.arraycopy(deck,rand,deck,0,52-rand);
          System.arraycopy(tmp,0,deck,52-rand,rand);
     }
0
 

Author Comment

by:ecksplosive
ID: 12349023
Thanks dude, will give it a try!
0
 
LVL 37

Accepted Solution

by:
zzynx earned 60 total points
ID: 12351706
Maybe you better use a List instead of an array.
1) you get the shuffle for free:  java.util.Collections.shuffle(yourList);
2) doing what you asked for is rather easy

Remark:  java.util.Random r = new java.util.Random();
               int random = r.nextInt(52);  // Generates you a number between 0 and 51

So,

if your deck is  

    List deck = new ArrayList();
    deck.add(...); // add your cards this way

you can shuffle it by calling

    java.util.Collections.shuffle(deck);

and cut could look like

     public void cut() {
          int cutPosition = r.nextInt(52);   // with r defined as new Random() in the class's constructor
          for (int i=1; i<=52-cutPosition; i++)
            deck.add(0, deck.remove(deck.size()-1));  // take the last card and place it at the beginning (and do this cutPosition times)
     }

Yeah, that's really all there is.
And improvement to keep the number of moves as small as possible would be:

     public void cut() {
          int cutPosition = r.nextInt(52);   // with r defined as new Random() in the class's constructor
          if (cutPosition>=26) {
             for (int i=1; i<=52-cutPosition; i++)
               deck.add(0, deck.remove(deck.size()-1));  // take the last card and place it at the beginning (and do this cutPosition times)
          } else {
             for (int i=1; i<=cutPosition; i++)
               deck.add(deck.remove(0)); // Take the first card and move it to the back
          }
     }
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:ecksplosive
ID: 12360269
Thanks people, really helped me out of a lot; your help is much appreciated.
The way I decided to do it in the end, was by using a sort of combination of the two...

      public void cut()
      {
            Random randVar = new Random();
            int rand = randVar.nextInt(52);
            Card[] tmp = new Card[rand];

            System.arraycopy(deck,0,tmp,0,rand);
            System.arraycopy(deck,rand,deck,0,52-rand);
            System.arraycopy(tmp,0,deck,52-rand,rand);
      }

:-)
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 12360425
:-)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12367116
Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java asynchronous logging 4 50
session migration servlets 2 26
couple of eclipse 5 25
servlet filter example 37 43
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now