473,418 Members | 2,248 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

Can't shuffle

I've been studying java for a week now. I have a project that needed to display a deck of cards and shuffle them alternately (like cutting the deck then insert each card). Then deal it, much like the war game of cards. I'm just still on the shuffle stage but the the deck remains the same even after shuffling. I think my codes got problem with the swapping part. I try to think of another way to shuffle(not randomly) but nothing came up. So please HELP
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4.  public class Deck{
  5.  
  6.         Vector deck; // create a 52-card deck
  7.          final String[] suit = {"D", "H", "S", "C"};
  8.          final String[] rank = {"A","K", "Q", "J", "T", "9", "8", "7", "6", "5", "4", "3", "2"};
  9.  
  10.          public Deck(){
  11.  
  12.         // creating a list
  13.          List<String> list = new ArrayList<String>();
  14.             for (int i = 0; i < suit.length; i++)
  15.             {
  16.           for (int j = 0; j < rank.length; j++)
  17.             list.add(rank[j] + suit[i]);
  18.             }
  19.             // convert the array to vector
  20.           deck = new Vector(list);
  21.  
  22.             // access elements
  23.             System.out.println("Initial Deck:"); 
  24.             Enumeration deck1 = deck.elements();    
  25.             while (deck1.hasMoreElements())
  26.            {  
  27.                 System.out.println(deck1.nextElement().toString());             
  28.             }
  29.  
  30.     }
  31.  
  32.     public void shuffle()throws IOException{
  33.  
  34.  
  35.         BufferedReader br = new BufferedReader(new InputStreamReader (System.in));
  36.         System.out.println ("Enter the desired number for shuffle: ");
  37.         String temp = br.readLine();
  38.         int count = Integer.parseInt(temp);
  39.  
  40.             for (int b=0; b<=count; b++)
  41.                 {
  42.                 for (int i=1;i<(deck.size())/2;i+=2)
  43.                     {            
  44.                       for (int j=26;j<(deck.size())/2;j+=2)
  45.                      {
  46.                          Collections.swap(deck, i, j);}
  47.                      } 
  48.                 }System.out.println("Shuffled Deck:\n" + deck);    
  49.     }
  50.  
  51.  
  52.  
  53.  
  54.  
  55.         public static void main (String[] args) throws IOException{
  56.                 Deck cardeck = new Deck();
  57.                 cardeck.shuffle();    
  58.     }
  59. }
!
Jun 15 '10 #1
1 1794
jkmyoung
2,057 Expert 2GB
The for loops are to blame:
for (int j=26;j<(deck.size())/2;j+=2)

if deck.size() = 26, then j >= 26 from the start -> the for loop never runs.
Jun 15 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

23
by: JC | last post by:
I am very new to programming and learning on my own. Why do I keep getting duplicate values using this code? I want to shuffle a deck of 52 cards. The logic seems right to me. Randomize For...
24
by: Joerg Schuster | last post by:
Hello, I am looking for a method to "shuffle" the lines of a large file. I have a corpus of sorted and "uniqed" English sentences that has been produced with (1): (1) sort corpus | uniq >...
6
by: CaseyB | last post by:
If I wanted to create a game like Solitaire that would first randomly shuffle a deck of cards, I figured out that all I had to use is the Random() class or rnd and make sure I use the Randomize...
6
by: Happy YN | last post by:
in a Db there are 2 fields which contain 'matching' data. (there are other columns too) A Genesis B Exodus C leviticus D Numbers E Deutronomy I want to shuffle both columns so they are both...
4
by: Pratik | last post by:
For the time being, I'm doing a simple swap method for my deck of cards using the random number generator in cstdlib. I've created a dynamic array of a type class Card. When I go to shuffle it and...
0
by: RobAMacAF | last post by:
Here is my prediciment. I have a database with a form. It in the people can pick a main category, then sub category, then it shows a list of questions from that in a sub form. I am trying to find a...
9
by: Tuxedo | last post by:
I'd like to reorganize the third, fourth, fifth and sixth, as well as any elements thereafter in an array in random order: var a = new...
4
by: tvance929 | last post by:
Hey everyone, I created a theDeck class that creates a 52 card int List. Inside of this class I have a ShuffleCards method. I simply want 2 seperate decks that I can then shuffle and...
2
by: rubyhuang | last post by:
the problem is A standard pack of cards can be represented as an array of 52 integers with each number representing a standard card. Thus: 0 1 2 3 4 5 6 7 8 9 10 11 12.....39 40 41 42 43 44 45 46...
11
by: whodgson | last post by:
I`m trying to write a shuffle function to shuffle a sorted array with an even number of elements but it won`t print past the first 2 elements as shown below. When i print the temp array it only...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.