Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Dealing 13 cards to 4 players.

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Dealing 13 cards to 4 players.

    How would I deal 13 cards to 4 players? I have my program below, but I'm stuck.

    This is my work so far. I'm new to this programming but I'm trying! I know there are a lot of things wrong with this, but bear with me I'm learning.
    public class Project_BridgeDeal {
    	static String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
    	static String[] ranks = {"2", "3", "4", "5", "6", "7", "8",
    				 "9", "10", "Jack", "Queen", "King", "Ace"};
     
    	public static void main(String[] args) {
    	}
    	public static void unwrap(int[] pack) {  	
    		int[] deck = new int[52];	
    		unwrap(deck);
    		for (int i = 0; i < deck.length; i++)
    			deck[i] = i; }
     
    	public static void shuffle(int[] pack) {  
    		shuffle(deck);
    		for (int i = 0; i < deck.length; i++) {
    			int index = (int)(Math.random() * deck.length);
    			int temp = deck[i];
    			deck[i] = deck[index];
    			deck[index] = temp;
    		}
    	}
    	public static int[] deal(int[] pack, int n, int offset) {  
     
    		final int CARDS_PER_HAND = 13;
     
    		int[] north = deal(deck, CARDS_PER_HAND, CARDS_PER_HAND * 0);
    		showHand(north, "North was dealt: ");
     
    		int[] east = deal(deck, CARDS_PER_HAND, CARDS_PER_HAND * 1);
    		showHand(east, "East was dealt:");
     
    		int[] south = deal(deck, CARDS_PER_HAND, CARDS_PER_HAND * 2);
    		showHand(south, "South was dealt:");
     
    		int[] west = deal(deck, CARDS_PER_HAND, CARDS_PER_HAND * 3);
    		showHand(west, "West was dealt: ");
    	}

    I did a program like this before, but without dealing them out.
    public class ProgramDeck {
       public static void main(String[] args) {
          String[] suit = { "Clubs", "Diamonds", "Hearts", "Spades" };
          String[] rank = { "2", "3", "4", "5", "6", "7", "8", "9",
                            "10", "Jack", "Queen", "King", "Ace"    };
          int SUITS = suit.length;
          int RANKS = rank.length;
          int N = SUITS * RANKS;
     
          // build the deck
          String[] deck = new String[N];
          for (int i = 0; i < RANKS; i++)
             for (int j = 0; j < SUITS; j++)
                deck[SUITS*i + j] = rank[i] + " of " + suit[j];
     
          // shuffle the deck
          for (int i = 0; i < N; i++) {
             int r = i + (int) (Math.random() * (N-i));
             String t = deck[r];
             deck[r] = deck[i];
             deck[i] = t;
          }
     
          // print the shuffled deck
          for (int i = 0; i < N; i++)
             System.out.println(deck[i]);
       }
    }
    Here is a sample run of the program:

    North was dealt:
    2 of Spades
    6 of Spades
    8 of Spades
    2 of Hearts
    3 of Hearts
    8 of Hearts
    10 of Hearts
    6 of Diamonds
    8 of Diamonds
    Queen of Diamonds
    King of Diamonds
    4 of Clubs
    5 of Clubs

    East was dealt:
    5 of Spades
    King of Spades
    6 of Hearts
    9 of Hearts
    Jack of Hearts
    Queen of Hearts
    Ace of Hearts
    2 of Diamonds
    4 of Diamonds
    10 of Diamonds
    Ace of Diamonds
    9 of Clubs
    Jack of Clubs

    South was dealt:
    3 of Spades
    4 of Spades
    9 of Spades
    Jack of Spades
    Ace of Spades
    7 of Hearts
    3 of Diamonds
    5 of Diamonds
    7 of Diamonds
    Jack of Diamonds
    3 of Clubs
    7 of Clubs
    King of Clubs

    West was dealt:
    7 of Spades
    10 of Spades
    Queen of Spades
    4 of Hearts
    5 of Hearts
    King of Hearts
    9 of Diamonds
    2 of Clubs
    6 of Clubs
    8 of Clubs
    10 of Clubs
    Queen of Clubs
    Ace of Clubs


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Dealing 13 cards to 4 players.

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Dealing 13 cards to 4 players.

    I just did! Thank you for that tip.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,509
    My Mood
    Mellow
    Thanks
    155
    Thanked 613 Times in 600 Posts

    Default Re: Dealing 13 cards to 4 players.

    What's wrong with the sample run you posted? What do you need help with?

  5. #5
    Junior Member
    Join Date
    Dec 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Dealing 13 cards to 4 players.

    I thought I posted a reply.

    I guess maybe my question is in the wrong section. My ProgramDeck is fine, but not Project Bridgedeal. In ProgramDeck, I received 52 random cards. However, I cannot incorporate that into Project Bridgedeal, where I'm suppose to deal 13 random cards to 4 players. I'm new at this, so I don't know how to do it. I think maybe...
    for (int i = 0; i < 4; i++) {
    String suit = suits[deck[i] / 13];
    String rank = ranks[deck[i] % 13];
    System.out.println("North was dealt: " + deck[i]);

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Dealing 13 cards to 4 players.

    I'm suppose to deal 13 random cards to 4 players. I'm new at this, so I don't know how to do it.
    The Collections class has a shuffle() method for a List. If you can make a List of the cards, it could be shuffled with that method.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Dec 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Dealing 13 cards to 4 players.

    So I tidied up my code.
    I'm still have issues though. How I can make showhand a method to display what cards each player has? Please and thank you.
    public class Project_BridgeDeal {
    	static String[] suits = { "Spades", "Hearts", "Diamonds", "Clubs" };
    	static String[] ranks = { "2", "3", "4", "5", "6", "7", "8", "9", "10",
    			"Jack", "Queen", "King", "Ace" };
     
    	public static void main(String[] args) {
    		int[] deck = new int[52];
    		final int CARDS_PER_HAND = 13;
    		unwrap(deck);
    		shuffle(deck);
    		System.out.println();
     
    		int[] north = deal(deck, CARDS_PER_HAND, CARDS_PER_HAND * 0);
    		showHand(north, "North was dealt: ");
     
    		int[] east = deal(deck, CARDS_PER_HAND, CARDS_PER_HAND * 1);
    		//showHand(east, "East was dealt: ");
     
    		int[] south = deal(deck, CARDS_PER_HAND, CARDS_PER_HAND * 2);
    		//showHand(south, "South was dealt:");
     
    		int[] west = deal(deck, CARDS_PER_HAND, CARDS_PER_HAND * 3);
    		//showHand(west, "West was dealt:");
     
    	}
     
    	public static void unwrap(int[] pack) {
     
     
    		for (int i = 0; i < pack.length; i++)
    			pack[i] = i;
    	}
     
    	public static void shuffle(int[] pack) {
     
     
    		for (int i = 0; i < pack.length; i++) {
    			int index = (int) (Math.random() * pack.length);
    			int temp = pack[i];
    			pack[i] = pack[index];
    			pack[index] = temp;
    		}
    	}
     
    	public static int[] deal(int[] pack, int n, int offset) {
    		int [] hand = new int [13];
    		for (int i = 0; i <n; i++)
    			hand[i]= pack[i+offset];
     
    		return hand;
    	}
    //Issue starts here
    	public static int [] showHand(int[]pack) {
    		for (int north; north<13; north++)
    			showHand[north]=pack[north];
    			return pack;
     
    	}
    }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Dealing 13 cards to 4 players.

    make showhand a method to display what cards each player has
    Start simple and build on it.
    What is input to the method (its arguments),
    what does the method return (if anything),
    what does the method do with its input?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Dec 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Dealing 13 cards to 4 players.

    Am I on the right track, though?

    Or
    public static int [] hand(int[]showHand)
    for( int north; north < hand; north++)
    hand[hand] = showHand[hand]
    return show hand;

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Dealing 13 cards to 4 players.

    Am I on the right track, though?
    If you'll answer the questions I asked about what the method is supposed to do then I might be able to tell.

    What does the code do when it is compiled and executed.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Dec 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Dealing 13 cards to 4 players.

    I just saw the questions, guess you added them.

    I believe the input would be hand and showHand. I would say the players( North East South West), but I'm not sure.
    The method should return showHand, or display the 13 random cards each player receives.
    With the input, the method should I think take the player's hand and display them accordingly.

    I'm new so I'm sorry if it sounds like bull. I'm really trying right now.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Dealing 13 cards to 4 players.

    input would be hand and showHand
    That says the method has two arguments: hand and showHand.
    What are the data types for each of those? An int array or what???

    method should return showHand
    What is the data type of showHand?
    How is the value of showHand that is returned different from the value of showHand that was passed to the method?

    display them accordingly.
    Would it display both the contents of hand and the contents of showHand?
    Initially the simplest way to display the contents of the variables is to use println().
    If the variable is an array, use the Arrays class's toString() method to format the array for printing:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Dealing With Overflow?
    By linzylu1190 in forum Loops & Control Statements
    Replies: 1
    Last Post: May 23rd, 2013, 07:40 PM
  2. Two problems (Dealing with Classes and Objects)
    By AustinStanley in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 4th, 2012, 07:17 PM
  3. Can anyone point me in the right direction dealing with bytes
    By derekxec in forum Java Theory & Questions
    Replies: 5
    Last Post: August 18th, 2012, 01:44 PM
  4. Java Game - Alternate between players
    By JohnQ in forum What's Wrong With My Code?
    Replies: 8
    Last Post: November 21st, 2011, 07:49 PM
  5. java game, both players move for player 2 but not player 1
    By ajakking789 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 21st, 2011, 12:52 PM