# Poker Deck Project --Arrays ----Cutting deck Help

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 04-11-2013, 02:29 PM
NoobieCode
Poker Deck Project --Arrays ----Cutting deck Help
So here's what I have to do:

public void cut(int position)-- This method divides the deck into two subpackets: The part above the specified position, and the part that is at the specified position or below. The two subpackets are reversed (the top packet is placed on the bottom and the bottom packet is placed on the top.) So, the card at the originally selected position becomes the "front" of the deck. The position value uses 0-based indexing as usual, so the card at the top of the deck (to the left in the diagrams) is at position 0.

Code:

```         Card [] cut_deck = new Card[cards.length];                    /**                 *    A B C D E F G H I J  K                 *    0 1 2 3 4 5 6 7 8 9  10                 *                    *    if position = 6                 *    index = 5                 *                 *              G H I J K    A B C D E F                  *    index_old  6 7 8 9 10  0 1 2 3 4 5                  *    index_new  0 1 2 3 4    5 6 7 8 9 10                 *                    *    patterns: not cut off index difference is 6                 *              cut off index difference is 5                 *                              *                              *              pseudo-code                 *                              *              Card[] cut_deck = new Card[cards.length];                 *              loop {  cut_deck[i] = cards[i+6]  i++    i = 0 i; i< position-1}                 *              loop { cut_deck[j] = cards[j-5]  j++    j = position-1; j< cards.length}                 *                              *                              *                              *                 * **/                 for (int i = 0; i < position - 1 ; i++){                         cut_deck[i] = cards[i+position];                 }                 cards = cut_deck;                 for (int j = position -1 ; j<cards.length;  j++ ){                         cut_deck[j] = cards [j-(position-1)];                 }                        cards = cut_deck;                 return;```

Im new and I am struggling on how to do this. Thanks in advance
• 04-11-2013, 02:40 PM
Chris.Brown.SPE
Re: Poker Deck Project --Arrays ----Cutting deck Help
Dont set the cut_deck array to the original array until the end of the function (delete line 33). Otherwise your code looks correct. Did you have any other specific questions?
• 04-11-2013, 02:45 PM
NoobieCode
Re: Poker Deck Project --Arrays ----Cutting deck Help
I have another question regarding another method. :
public void shuffle() -- This method will re-arrange the cards that are in the deck. The idea is that the deck will be divided into two "packets" -- the top half and the bottom half. The new array of cards will consist of: the first card from the top packet, followed by the first card from the bottom packet, followed by the second card from the top packet, followed by the second card from the bottom packet, etc. Important: If there are an odd number of cards, the top packet should have one more card than the bottom packet. Remember that the top of the deck is considered to be the front of the array.

Code:

``` public void shuffle() {                 /**                 * Even Shuffle                 *                 * 8 elements                 *                 * ABCD EFGH          AEBF CGDH                 * 0123 4567          0415 2637                 *                 * -------                 * [0] = [0]          //array [0]  =  new array [0]                                                    * [1] = [4]          //array[1]    =  new array[total elements/2]                                      * [2] = [1]          //array[2]    =  new array[0+1]                 * [3] = [5]          //array[3]    =  new array[(total elements/2)+1]                 * [4] = [2]          //array[4]    =  new array[0+2]                 * [5] = [6]          //array[5]    =  new array[(total elements/2)+2]                 * [6] = [3]          //array[6]    =  new array [0+3]                 * [7] = [7]          //array[7]    =  new last shuffle OR array[(total elements/2)+3]                 *                 *                 * Odd Shuffle                 *                 * 19 elements                 * 19 / 2 = 9                 * 19 % 2 = 1                 *                 * ABCDEFGHIJ(9+1)  K  L  M  N  O  P  Q  R  S (9)                 * 0123456789        10 11 12 13 14 15 16 17 18                 *                 *                 * A K      B L      C M    D N    E O    F P    G Q    H R    I S    J                 * 0 10      1 11    2 12    3 13    4 14    5 15    6 16    7 17  8 18  9                 *                 * [0]  =  NEW [0]            //array[0]        =      new array[0]                 * [1]  =  NEW [10]            //array[1]        =      new array[(((total elements/2)+(total elements%2))]                 * [2]  =  NEW [1]            //array[2]        =      new array[0+1]                 * [3]  =  NEW [11]            //array[3]        =      new array[(((total elements/2)+(total elements%2))+1]                 * [4]  =  NEW [2]            //array[4]        =      new array[0+2]                 * [5]  =  NEW [12]            //array[5]        =      new array[(((total elements/2)+(total elements%2))+2]                 * [6]  =  NEW [3]            //array[6]        =      new array[0+3]                 * [7]  =  NEW [13]            //array[7]        =      new array[(((total elements/2)+(total elements%2))+ 3]                 * [8]  =  NEW [4]            //array[8]        =      new array[0+4]                 * [9]  =  NEW [14]            //array[9]        =      new array[(((total elements/2)+(total elements%2))+ 4]                 * [10]  =  NEW [5]            //array[10]      =      new array[0+5]                 * [11]  =  NEW [15]            //array[11]      =      new array[(((total elements/2)+(total elements%2))+ 5]                 * [12]  =  NEW [6]            //array[12]      =      new array[0+6]                 * [13]  =  NEW [16]            //array[13]      =      new array[(((total elements/2)+(total elements%2))+ 6]                 * [14]  =  NEW [7]            //array[14]      =      new array[0+7]                 * [15]  =  NEW [17]            //array[15]      =      new array[(((total elements/2)+(total elements%2))+ 7]                 * [16]  =  NEW [8]            //array[16]      =      new array[0+8]                 * [17]  =  NEW [18]            //array[17]      =      new array[(((total elements/2)+(total elements%2))+ 8]                 * [18]  =  NEW [9]            //array[18]      =      new array[0+9]                 *                  * **/                 if (cards.length % 2 == 0){ //even                         Card[] even_shuffle = new Card[cards.length];                         int top = 0;                         for (int index = 0; index < cards.length; index+=2){      //setting all even index to first values of packet                                 even_shuffle[index] = cards [top];                                 top++;                         }                         cards = even_shuffle;                         int bottom = cards.length/2;                         for (int index = 1; index < cards.length; index+=2){  //setting all odd index to first values of the second packet                                 even_shuffle[index] = cards[bottom];                                 bottom++;                         }                         cards = even_shuffle;                         return;                 }                 else{ //odd                         Card[] odd_shuffle = new Card [cards.length];                         int top = 0;                         for (int index = 0; index < cards.length; index += 2){                                  odd_shuffle[index] = cards[top];                                        top++;                         }                         cards = odd_shuffle;                         int bottom = (cards.length/2) + 1;                         for (int index = 1; index< cards.length; index += 2){                                 odd_shuffle[index] = cards [bottom];                                 bottom++;                         }                         cards = odd_shuffle;                         return;                 }         }```
• 04-11-2013, 02:45 PM
NoobieCode
Re: Poker Deck Project --Arrays ----Cutting deck Help
Its not working. Do I have to use some form of subsituting or swapping?
• 04-11-2013, 02:50 PM
Chris.Brown.SPE
Re: Poker Deck Project --Arrays ----Cutting deck Help
Again, you shouldnt be setting your new array to your old array until after you are all done. Doing this after the first for loop completely jacks up the second for loop.
• 04-11-2013, 02:58 PM
NoobieCode
Re: Poker Deck Project --Arrays ----Cutting deck Help
I fixed it and it still doesn't run.
• 04-11-2013, 03:01 PM
NoobieCode
Re: Poker Deck Project --Arrays ----Cutting deck Help
Just Kidding. The Shuffle works, the Cut doesnt. Thanks for your help though.
• 04-11-2013, 03:03 PM
NoobieCode
Re: Poker Deck Project --Arrays ----Cutting deck Help
could you help me on a copy constructor. For some reason It's not right. Its supposed to be shallow copy.

Code:

```public class Deck {         private Card[] cards;         public Deck() {                 cards = new Card[52]; //index [0,51]                 int index = 0;                 for (int suit = 0; suit <= 3; suit++){                         for (int value = 1; value <= 13; value++){                                 cards[index] = new Card(value, suit);                                 index++;                                 /**            COL    ROW                                 * cards[0] =  1-ace  0-spade                                 * cards[1] =  2      0-spade                                 * cards[2] =  3      0-spade                                 * cards[3] =  4      0-spade                                 *                                 * cards[13] = 1-ace  1-hearts                                  * cards[14] = 2      1-hearts                                 * cards[15] = 3      1-hearts                                 * cards[16] = 4      1-hearts                                 * cards[17] = 5      1-hearts                                 * cards[18] = 6      1-hearts                                 *                                 * cards[26] = 1-ace  2-club                                 * cards[27] = 2      2-club                                 * cards[28] = 3      2-club                                 * cards[29] = 4      2-club                                 *                                 * cards[39] = 1-ace  3-diamond                                 * cards[40] = 2      3-diamond                                 *                                 * cards[50] = 12-Q  3-diamond                                 * cards[51] = 13-K  3-diamond                                 * **/                         }                 }         }         public Deck(Deck other) {                 Card[] copy_one = new Card[52];                 int index = 0;                 for (int suit = 0; suit <= 3; suit++){                         for (int value = 1; value <= 13; value++){                                 copy_one[index] = new Card(value, suit);                                 index++;                         }                 }                 Card[] copy_two = new Card[copy_one.length];                 for (int i = 0; i< copy_one.length; i++){                         copy_two[i] = copy_one[i];                        }         }```

The method Deck(Deck other) is the copy constructor which I cant seem to get.
• 04-11-2013, 03:10 PM
Chris.Brown.SPE
Re: Poker Deck Project --Arrays ----Cutting deck Help
You're never setting the array cards. You're also never referencing the Deck other.

A copy constructor should take all the values from one object into another. First you have to determine how shallow you want it. It could be as easy as setting other.cards to this.cards. Or for a deeper copy you would loop through other.cards and copy each card to the new cards array. Or even deeper, in that loop you would create a new card from each card taken from other.cards. Figuring out which of these you want to do is the first step.
• 04-11-2013, 03:19 PM
NoobieCode
Re: Poker Deck Project --Arrays ----Cutting deck Help
I want to do a shallow copy. I dont know how to set array cards or refereence other. The type is different.
• 04-11-2013, 03:22 PM
Chris.Brown.SPE
Re: Poker Deck Project --Arrays ----Cutting deck Help
It looks like you need to make some getters and setters for the array cards. Then, if you really just want a turbo shallow copy, it should just be one line this.setCards(other.getCards()); If you want to get more complicated then that you will have to use a loop to assign specific array elements to each one.
• 04-11-2013, 03:27 PM
NoobieCode
Re: Poker Deck Project --Arrays ----Cutting deck Help
How would I do that if I was using my code. Where would I need to reference other?
• 04-11-2013, 03:28 PM
NoobieCode
Re: Poker Deck Project --Arrays ----Cutting deck Help
hows this?

Code:

```        int cards = other.getNumCards();                 Card[] copy_one = new Card[other.getNumCards()];                                 int index = 0;                 for (int suit = 0; suit <= 3; suit++){                         for (int value = 1; value <= 13; value++){                                 copy_one[index] = new Card(value, suit);                                 index++;                         }                         Card[] copy_two = new Card[copy_one.length];                         for (int i = 0; i< copy_one.length; i++){                                 copy_two[i] = copy_one[i];                                }                 }         }```
• 04-11-2013, 03:40 PM
Chris.Brown.SPE
Re: Poker Deck Project --Arrays ----Cutting deck Help
I think the point of the copy constructor is so that your new deck has the exact same order as the first deck. Your for loops are creating a new deck that is in order of suit and value. And to be honest i have no idea what copy_two is for.

Goal = at the end of your copy constructor, newDeck.cards should equal other.cards...take a second and think what it would take to make that happen.
• 04-11-2013, 03:48 PM
NoobieCode
Re: Poker Deck Project --Arrays ----Cutting deck Help
hows this?

Code:

```Card[] copy_one = new Card[cards.length];                 int index = 0;                 for (int suit = 0; suit <= 3; suit++){                         for (int value = 1; value <= 13; value++){                                 copy_one[index] = new Card(value, suit);                                 index++;                         }                         other.equals(copy_one);                         }                 }```
• 04-11-2013, 03:53 PM
NoobieCode
Re: Poker Deck Project --Arrays ----Cutting deck Help
wait sorry that was dumb... Deck type cant equal Card type..
• 04-11-2013, 03:56 PM
Chris.Brown.SPE
Re: Poker Deck Project --Arrays ----Cutting deck Help
Also, you're not trying to change the values in "other" you want to take the values FROM "other" and put them in the new one you are creating which is referenced as "this"
• 04-11-2013, 04:06 PM
NoobieCode
Re: Poker Deck Project --Arrays ----Cutting deck Help
this is all I got...I'm lost ahaha

Code:

``` int numCards = other.getNumCards();                 Card[] copy_one = new Card[numCards];                                 int index = 0;                 for (int suit = 0; suit <= 3; suit++){                         for (int value = 1; value <= 13; value++){                                 copy_one[index] = new Card(value, suit);                                 index++;                         }                         this.cards = copy_one;                         }                 }```
• 04-11-2013, 04:18 PM
Chris.Brown.SPE
Re: Poker Deck Project --Arrays ----Cutting deck Help
Delete all of that stuff. For a simple shallow copy constructor this is all you need.

this.cards = other.getCards(); //you need to use getCards() because cards is private which means you cant reference it in "other" directly.

You're simply copying the cards array from other to this, that's all. Don't over complicate it.

The problem with shallow constructors is that if you change anything in the cards array in one then it will effect the cards array in the other because they both have the same reference. If that is not acceptable then you need a deep copy constructor.
• 04-11-2013, 04:22 PM
NoobieCode
Re: Poker Deck Project --Arrays ----Cutting deck Help
thanks alot. Im sorry for bothering you.
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last