problem in card shuffle

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rubyhuang
    New Member
    • Dec 2007
    • 19

    #1

    problem in card shuffle

    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 47 48 49 50 51
    We can shuffle this pack of cards by randomly generating two indexes and swapping those two cards.
    For example if we randomly generate the indexes 8 and 50 and swap these two "cards" we would get
    (assuming this is the first swap):
    0 1 2 3 4 5 6 7 50 9 10 11 12.....39 40 41 42 43 44 45 46 47 48 49 8 51
    After a suitable number of swaps have been made the pack can be said to be "shuffled".
    Design and implement a Java programme which takes as input the number of desired swaps, outputs an
    ordered set of cards which are before the shuffling and output the set of cards after the shuffling. The
    number of swaps must be at least 1 (otherwise we would not be doing any "shuffling" ) and not more
    than 1000 (as any more than this would be ridiculous).

    i want to use four class to solve this problem. below is my code. can any one help me to change the wrong code into the right one. thank you.
    Code:
    public class Card{
    	private String value_;
    	private String suit_;
    	public Card(String value, String suit){
    		value_=value;
    		suit_=suit;
    	}
    	public String toSting()
    	{
    		return value_ + "-" + suit_;
    	}
    }
    Code:
    public class CardApp{
    	public static void main (String[] args){
    	  System.out.print(card);
    	  newManager = new IOManager(numberOfSwaps);
    	  newManager.shuffle();
    	  }
    	  }
    Code:
    import java.util.*;
    public class IOManager{
    	private static Scanner input= new Scanner(System.in);
    	private int numberOfSwaps;
    	private int index1;
    	private int index2;
    	public void shuffle(){
    		numberOfSwaps = input.nextInt();
    		int index1 = 0;
    		int index2 = 0;
    			for(int i=0;i<numberOfSwaps;i++){
    			index1=(int)(Math.random()*52);
    			index2=(int)(Math.random()*52);
    			newPack = new Pack();
    			newPack.swap(index1,index2);
    		}
    	}
    	}
    Code:
    public class Pack{
    	
    	private Card[] cardArray_;
    	public Pack(){
    		cardArray_=newCard[52];
    	}
    public void initialise(){
    	String value,suit;
    	for(int i=0;i<52;i++){
    		suit=i/13;
    		value=i%13;
    		Card card=new Card(value,suit);
    		cardArray_[i]=card;
    }
    public void swap(int index1, int index2){
    	Card card=cards[index1];
    	cards[index1]=cards[index2];
    	cards[index2]=card;
    	}
    }
    }
  • mia023
    New Member
    • May 2007
    • 89

    #2
    Originally posted by rubyhuang
    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 47 48 49 50 51
    We can shuffle this pack of cards by randomly generating two indexes and swapping those two cards.
    For example if we randomly generate the indexes 8 and 50 and swap these two "cards" we would get
    (assuming this is the first swap):
    0 1 2 3 4 5 6 7 50 9 10 11 12.....39 40 41 42 43 44 45 46 47 48 49 8 51
    After a suitable number of swaps have been made the pack can be said to be "shuffled".
    Design and implement a Java programme which takes as input the number of desired swaps, outputs an
    ordered set of cards which are before the shuffling and output the set of cards after the shuffling. The
    number of swaps must be at least 1 (otherwise we would not be doing any "shuffling" ) and not more
    than 1000 (as any more than this would be ridiculous).

    i want to use four class to solve this problem. below is my code. can any one help me to change the wrong code into the right one. thank you.
    Code:
    public class Card{
    	private String value_;
    	private String suit_;
    	public Card(String value, String suit){
    		value_=value;
    		suit_=suit;
    	}
    	public String toSting()
    	{
    		return value_ + "-" + suit_;
    	}
    }
    Code:
    public class CardApp{
    	public static void main (String[] args){
    	  System.out.print(card);
    	  newManager = new IOManager(numberOfSwaps);
    	  newManager.shuffle();
    	  }
    	  }
    Code:
    import java.util.*;
    public class IOManager{
    	private static Scanner input= new Scanner(System.in);
    	private int numberOfSwaps;
    	private int index1;
    	private int index2;
    	public void shuffle(){
    		numberOfSwaps = input.nextInt();
    		int index1 = 0;
    		int index2 = 0;
    			for(int i=0;i<numberOfSwaps;i++){
    			index1=(int)(Math.random()*52);
    			index2=(int)(Math.random()*52);
    			newPack = new Pack();
    			newPack.swap(index1,index2);
    		}
    	}
    	}
    Code:
    public class Pack{
    	
    	private Card[] cardArray_;
    	public Pack(){
    		cardArray_=newCard[52];
    	}
    public void initialise(){
    	String value,suit;
    	for(int i=0;i<52;i++){
    		suit=i/13;
    		value=i%13;
    		Card card=new Card(value,suit);
    		cardArray_[i]=card;
    }
    public void swap(int index1, int index2){
    	Card card=cards[index1];
    	cards[index1]=cards[index2];
    	cards[index2]=card;
    	}
    }
    }
    Why don't you sort the cards?Here is a sample code for sorting cards
    public void sort() {
    for (int i = 0; i < N; i++) {
    for (int j = i; j > 0; j--) {
    if (cards[j-1].less(cards[j])) {
    Card swap = cards[j];
    cards[j] = cards[j-1];
    cards[j-1] = swap;
    }
    }
    }
    }
    BE CAREFUL THAT when you compare 2 cards you have to design a method to compare the two corresponding cards.

    Comment

    • BigDaddyLH
      Recognized Expert Top Contributor
      • Dec 2007
      • 1216

      #3
      > can any one help me to change the wrong code into the right one.

      You shouldn't just toss assignments into this forum and expect members to do them for you. What do you think is wrong?

      Comment

      Working...