Page 1 of 1

## Deck of cards using various methods Robust ways to form the basics of any card game Rate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=110380&amp;s=1bde49ff31ad088a1a1c6e9d6a71f28b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 crazyjugglerdrummer

• GAME OVER. NERD WINS.

Reputation: 124
• Posts: 690
• Joined: 07-January 09

Posted 16 June 2009 - 04:31 PM

Dear DIC visitor/member/anyone else interesting in making a card game in java,

When most of use began programming, we started looking for various project ideas to try. I've noticed card games come up a lot, so let's have a look at some ways to implement a card game. We will be focusing on the core of many card games, the cards and the deck.

The Card class is fairly simple, although it can be done a number of ways. I've chosen to use numeric variables for the internal data about the suit and rank, which will get converted to strings via predefined arrays when they need to be outputed. This lets us easily compare the suits and ranks (see if they're equal or sequential), without trying to use strings to represent them internally. If we used strings, we'd have to use

``` if ( card1.rank.equals("three") && card2.rank.equals("four") )
```

with a whole bunch of other checks for different numbers, as opposed to

``` if ( card1.rank + 1 == card2.rank)
```

So, we end up with something like

```package javacards;

public class Card
{
private int rank, suit;

private static String[] suits = { "hearts", "spades", "diamonds", "clubs" };
private static String[] ranks  = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" };

Card(int suit, int rank)
{
this.rank=rank;
this.suit=suit;
}

public @Override String toString()
{
return ranks[rank] + " of " + suits[suit];
}

public int getRank() {
return rank;
}

public int getSuit() {
return suit;
}

}

```

So we have read-only suit and rank variables, a simple constructor, and a toString method. The class will be ultra fast as it knows which strings to output just by accessing indexes of static arrays. We don't even have to use String.ParseInt().

The Card class really isn't that complicated, so most of our time will be spent on the Deck. Our Deck class will have to make and dole out Cards. To hold our Cards, we can use a number of different storage methods/structures. I will show examples using an array and an ArrayList. Each storage method will have a constructor to make the cards, a drawFromDeck() method that will return a random card, and a getTotalCards() method that will return the number of cards left in the Deck. The hard part is, once we return a card we have to make sure it isn't used again.

Using Array:
```package javacards;

import java.util.Random;

public class Deck {
private Card[] cards;
int i;

Deck()
{
i=51;
cards = new Card[52];
int x=0;
for (int a=0; a<=3; a++)
{
for (int b=0; b<=12; b++)
{
cards[x] = new Card(a,b);
x++;
}
}
}

public Card drawFromDeck()
{
Random generator = new Random();
int index=0;

do {
index = generator.nextInt( 52 );
} while (cards[index] == null);

i--;
Card temp = cards[index];
cards[index]= null;
return temp;
}

public int getTotalCards()
{
return i;
}
}
[code]

First we have a constructor which fills our deck will the cards of various suits and ranks. Every time we add a card at x we increment x to put us at a fresh spot.

drawFromDeck() returns a random card from our container. We need to get a random index and return that card. We have a post-test loop that finds an array index  (less than 52) that isn't null. When we find one, we return its card and set it to null. Our deck might need to give feedback on how many cards are left, (maybe to determine if there are enough for another round of black jack or if we need to add all the cards back), so we make a variable i that represents the number cards left in the deck. i starts at 51 and is decremented everytime we take a card out. So if i is -1, then we know we're out of cards.

Now we need to write a test for this program. We want to make sure the cards that get drawn really are drawn in a random order. So we have main:

[code]
package javacards;

public class Main {

public static void main(String[] args)
{
Deck deck = new Deck();
Card C;

System.out.println( deck.getTotalCards() );

while (deck.getTotalCards()!= 0 )
{
C = deck.drawFromDeck();
System.out.println( C.toString() );
}
}

```

When we run this, we get a good look at how the cards would come off the deck. They seem random enough, so we're okay. However, this tutorial would be pretty int if I just ended it here, and many of you are probably looking at the drawFromDeck() method with disdain at its ineffeciency, so we press on......

This is really a pretty sloppy way of doing things, as we just keep trying to find an index in the array with a card in it. If the index doesn't have a card in it, we look in a new index, unti we finally get one. We really should only have to use one index, and we can do that if we make sure that we're only looking through places that actually have cards in them.

So to fix this, we use our variable i that represents the number of cards in the array that aren't used up yet. Now we need to make sure our selection comes only from that set of cards. Say the first card we take out is at index 4. We set that to null and decrement the number of cards. But index 4 is still null. We know we have 51 cards left, but we don't know where they are in the deck. There could be empty/null indexes all over the place, at 4, 7, 20, and all we can do is just keep our fingers crossed that we don't land on one of them. How about we organize our empty card spots a little better? Maybe we can switch the chosen card's empty spot with the last card in the deck? Let's put our i variable to work.

array example number 2:

```package javacards;

import java.util.Random;

public class Deck {
private Card[] cards;
int i;

Deck()
{
i=51;
cards = new Card[52];
int x=0;
for (int a=0; a<=3; a++)
{
for (int b=0; b<=12; b++)
{
cards[x] = new Card(a,b);
x++;
}
}
}

public Card drawFromDeck()
{
Random generator = new Random();
int index=0;

index = generator.nextInt( i );

Card temp = cards[index];
cards[index]=cards[i];
cards[i]=null;
i--;
return temp;
}
}

```

Our random index now is some number within the boundaries of the cards left, instead of always 52. If we have 40 cards left, index won't be 48. We get a random card, SWITCH IT'S NOW NULL LOCATION WITH THE LAST CARD IN THE DECK, and then return the random card. we picked the fourth card, and put the last card in its place. The null value we get when we take out the card at index 4 is now located at spot 51, the last spot in the deck, as opposed to some random spot in the middle of the deck. This ensures that the indexes up through i always have cards in them, and that the null values are always moved to the end of the array. When we get a random index between 0 and i, we can be 100% sure that there is a fresh card in that spot.

To test this method, we can use the exact same test code as we did for the first example, as all that's changed is the internal workings of the deck class. In fact, we will use that test code for all our Deck implementations.

We've implemented a lot of features with arrays that could've easily been done with generics, like an ArrayList (adding a card to the end of the array, removing a card by setting its place to null, keeping track of the size of the array, etc. ). An ArrayList has most of the features we need built in already, so we don't have to write our own code to do them.

ArrayList example:

```package javacards;

import java.util.Random;
import java.util.ArrayList;

public class Deck {
private ArrayList<Card> cards;

Deck()
{
cards = new ArrayList<Card>();
for (int a=0; a<=3; a++)
{
for (int b=0; b<=12; b++)
{
}
}
}

public Card drawFromDeck()
{
Random generator = new Random();
int index= generator.nextInt( cards.size() );
return cards.remove(index);
}

public int getTotalCards()
{
return cards.size();
}
}

```

We can just add a card to the end of the ArrayList with add(), as opposed to making our own variable to keep track of what index we're on. cards.size() automatically keeps track of how many cards are left in the array. When we return a card, we can actually remove it from the ArrayList, thus decrementing the size, leaving us with a collection that only has cards left in it. (as opposed to just replacing its spot with the last card in the array, and setting the last spot to null)

Run the test code again to make sure everything still works.

To mention other data structures, we could have used a linked list to hold the cards, but our primary purpose is to extract a card from the deck, not to iterate through the cards, making a linked list not the best choice.

One of the goals of OO programming is to emulate the real world. When we return a card from our deck, we remove it from the deck so it's not used again, just like a real dealer would. But when a dealer gives you a card, they just take it off the top of the deck, they don't fish through the deck to get a random card. They shuffle the deck, randomizing the cards before hand, so they can just pop cards off the top later and know that they're random. This leads us to another example:

ArrayList 2: shuffling the cards before we deal them

```package javacards;

import java.util.Random;
import java.util.ArrayList;

public class Deck {
private ArrayList<Card> cards;

Deck()
{
cards = new ArrayList<Card>();
int index_1, index_2;
Random generator = new Random();
Card temp;

for (int a=0; a<=3; a++)
{
for (int b=0; b<=12; b++)
{
}
}

for (int i=0; i<100; i++)
{
index_1 = generator.nextInt( cards.size() - 1 );
index_2 = generator.nextInt( cards.size() - 1 );

temp = (Card) cards.get( index_2 );
cards.set( index_2 , cards.get( index_1 ) );
cards.set( index_1, temp );
}
}

public Card drawFromDeck()
{
return cards.remove( 0 );
}

public int getTotalCards()
{
return cards.size();
}
}

```

We put the cards in the ArrayList, then randomly take 100 pairs of cards and switch them, shuffling our deck. To draw from the deck, we just return the last element/card, and then remove that card from the deck. All the randomization is done before hand in the constructor (prior to any actually dealing of the cards), making our drawFromDeck method much simpler and less processor intensive. Piece of cake!

I hoped you've enjoyed and learned something from this tutorial!

--------CrazyJugglerDrummer

This post has been edited by crazyjugglerdrummer: 20 June 2009 - 10:20 AM

Is This A Good Question/Topic? 1

## Replies To: Deck of cards using various methods

### #2 jcmaster2

Reputation: 2
• Posts: 183
• Joined: 27-April 09

Posted 19 June 2009 - 03:33 PM

crazyjugglerdrummer, on 16 Jun, 2009 - 03:31 PM, said:

Dear DIC visitor/member/anyone else interesting in making a card game in java,

When most of use began programming, we started looking for various project ideas to try. I've noticed card games come up a lot, so let's have a look at some ways to implement a card game. We will be focusing on the core of many card games, the cards and the deck.

The Card class is fairly simple, although it can be done a number of ways. I've chosen to use numeric variables for the internal data about the suit and rank, which will get converted to strings via predefined arrays when they need to be outputed. This lets us easily compare the suits and ranks (see if they're equal or sequential), without trying to use strings to represent them internally. If we used strings, we'd have to use

``` if ( card1.rank.equals("three") && card2.rank.equals("four") )
```

with a whole bunch of other checks for different numbers, as opposed to

``` if ( card1.rank + 1 == card2.rank)
```

So, we end up with something like

```package javacards;

public class Card
{
private int rank, suit;

private static String[] suits = { "hearts", "spades", "diamonds", "clubs" };
private static String[] ranks  = { "Ace", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" };

Card(int suit, int rank)
{
this.rank=rank;
this.suit=suit;
}

public @Override String toString()
{
return ranks[rank] + " of " + suits[suit];
}

public int getRank() {
return rank;
}

public int getSuit() {
return suit;
}

}

```

So we have read-only suit and rank variables, a simple constructor, and a toString method. The class will be ultra fast as it knows which strings to output just by accessing indexes of static arrays. We don't even have to use String.ParseInt().

The Card class really isn't that complicated, so most of our time will be spent on the Deck. Our Deck class will have to make and dole out Cards. To hold our Cards, we can use a number of different storage methods/structures. I will show examples using an array and an ArrayList. Each storage method will have a constructor to make the cards, a drawFromDeck() method that will return a random card, and a getTotalCards() method that will return the number of cards left in the Deck. The hard part is, once we return a card we have to make sure it isn't used again.

Using Array:
```package javacards;

import java.util.Random;

public class Deck {
private Card[] cards;
int i;

Deck()
{
i=51;
cards = new Card[52];
int x=0;
for (int a=1; a<=4; a++)
{
for (int b=1; b<=13; b++)
{
cards[x] = new Card(a,b);
x++;
}
}
}

public Card drawFromDeck()
{
Random generator = new Random();
int index=0;

do {
index = generator.nextInt( 52 );
} while (cards[index] == null);

i--;
Card temp = cards[index];
cards[index]= null;
return temp;
}

public int getTotalCards()
{
return i;
}
}
[code]

First we have a constructor which fills our deck will the cards of various suits and ranks. Every time we add a card at x we increment x to put us at a fresh spot.

drawFromDeck() returns a random card from our container. We need to get a random index and return that card. We have a post-test loop that finds an array index  (less than 52) that isn't null. When we find one, we return its card and set it to null. Our deck might need to give feedback on how many cards are left, (maybe to determine if there are enough for another round of black jack or if we need to add all the cards back), so we make a variable i that represents the number cards left in the deck. i starts at 51 and is decremented everytime we take a card out. So if i is -1, then we know we're out of cards.

Now we need to write a test for this program. We want to make sure the cards that get drawn really are drawn in a random order. So we have main:

[code]
package javacards;

public class Main {

public static void main(String[] args)
{
Deck deck = new Deck();
Card C;

System.out.println( deck.getTotalCards() );

while (deck.getTotalCards()!= 0 )
{
C = deck.drawFromDeck();
System.out.println( C.toString() );
}
}

```

When we run this, we get a good look at how the cards would come off the deck. They seem random enough, so we're okay. However, this tutorial would be pretty int if I just ended it here, and many of you are probably looking at the drawFromDeck() method with disdain at its ineffeciency, so we press on......

This is really a pretty sloppy way of doing things, as we just keep trying to find an index in the array with a card in it. If the index doesn't have a card in it, we look in a new index, unti we finally get one. We really should only have to use one index, and we can do that if we make sure that we're only looking through places that actually have cards in them.

So to fix this, we use our variable i that represents the number of cards in the array that aren't used up yet. Now we need to make sure our selection comes only from that set of cards. Say the first card we take out is at index 4. We set that to null and decrement the number of cards. But index 4 is still null. We know we have 51 cards left, but we don't know where they are in the deck. There could be empty/null indexes all over the place, at 4, 7, 20, and all we can do is just keep our fingers crossed that we don't land on one of them. How about we organize our empty card spots a little better? Maybe we can switch the chosen card's empty spot with the last card in the deck? Let's put our i variable to work.

array example number 2:

```package javacards;

import java.util.Random;

public class Deck {
private Card[] cards;
int i;

Deck()
{
i=51;
cards = new Card[52];
int x=0;
for (int a=1; a<=4; a++)
{
for (int b=1; b<=13; b++)
{
cards[x] = new Card(a,b);
x++;
}
}
}

public Card drawFromDeck()
{
Random generator = new Random();
int index=0;

index = generator.nextInt( i );

Card temp = cards[index];
cards[index]=cards[i];
cards[i]=null;
i--;
return temp;
}
}

```

Our random index now is some number within the boundaries of the cards left, instead of always 52. If we have 40 cards left, index won't be 48. We get a random card, SWITCH IT'S NOW NULL LOCATION WITH THE LAST CARD IN THE DECK, and then return the random card. we picked the fourth card, and put the last card in its place. The null value we get when we take out the card at index 4 is now located at spot 51, the last spot in the deck, as opposed to some random spot in the middle of the deck. This ensures that the indexes up through i always have cards in them, and that the null values are always moved to the end of the array. When we get a random index between 0 and i, we can be 100% sure that there is a fresh card in that spot.

To test this method, we can use the exact same test code as we did for the first example, as all that's changed is the internal workings of the deck class. In fact, we will use that test code for all our Deck implementations.

We've implemented a lot of features with arrays that could've easily been done with generics, like an ArrayList (adding a card to the end of the array, removing a card by setting its place to null, keeping track of the size of the array, etc. ). An ArrayList has most of the features we need built in already, so we don't have to write our own code to do them.

ArrayList example:

```package javacards;

import java.util.Random;
import java.util.ArrayList;

public class Deck {
private ArrayList<Card> cards;

Deck()
{
cards = new ArrayList<Card>();
for (int a=1; a<=4; a++)
{
for (int b=1; b<=13; b++)
{
}
}
}

public Card drawFromDeck()
{
Random generator = new Random();
int index= generator.nextInt( cards.size() );
return cards.remove(index);
}

public int getTotalCards()
{
return cards.size();
}
}

```

We can just add a card to the end of the ArrayList with add(), as opposed to making our own variable to keep track of what index we're on. cards.size() automatically keeps track of how many cards are left in the array. When we return a card, we can actually remove it from the ArrayList, thus decrementing the size, leaving us with a collection that only has cards left in it. (as opposed to just replacing its spot with the last card in the array, and setting the last spot to null)

Run the test code again to make sure everything still works.

To mention other data structures, we could have used a linked list to hold the cards, but our primary purpose is to extract a card from the deck, not to iterate through the cards, making a linked list not the best choice.

One of the goals of OO programming is to emulate the real world. When we return a card from our deck, we remove it from the deck so it's not used again, just like a real dealer would. But when a dealer gives you a card, they just take it off the top of the deck, they don't fish through the deck to get a random card. They shuffle the deck, randomizing the cards before hand, so they can just pop cards off the top later and know that they're random. This leads us to another example:

ArrayList 2: shuffling the cards before we deal them

```package javacards;

import java.util.Random;
import java.util.ArrayList;

public class Deck {
private ArrayList<Card> cards;

Deck()
{
cards = new ArrayList<Card>();
int index_1, index_2;
Random generator = new Random();
Card temp;

for (int a=1; a<=4; a++)
{
for (int b=1; b<=13; b++)
{
}
}

for (int i=0; i<100; i++)
{
index_1 = generator.nextInt( cards.size() - 1 );
index_2 = generator.nextInt( cards.size() - 1 );

temp = (Card) cards.get( index_2 );
cards.set( index_2 , cards.get( index_1 ) );
cards.set( index_1, temp );
}
}

public Card drawFromDeck()
{
return cards.remove( 0 );
}

public int getTotalCards()
{
return cards.size();
}
}

```

We put the cards in the ArrayList, then randomly take 100 pairs of cards and switch them, shuffling our deck. To draw from the deck, we just return the last element/card, and then remove that card from the deck. All the randomization is done before hand in the constructor (prior to any actually dealing of the cards), making our drawFromDeck method much simpler and less processor intensive. Piece of cake!

I hoped you've enjoyed and learned something from this tutorial!

--------CrazyJugglerDrummer

CJD

Good example...

1. Why don't you show a:

1. Array based solution
2. struct based solution (C/C++)
3. class OOP solution (what you have)
4. Java Collection based solution

I tend to learn if I see things done in different ways...

This post has been edited by jcmaster2: 19 June 2009 - 03:34 PM

### #3 eoswins

Reputation: 0
• Posts: 1
• Joined: 09-May 10

Posted 09 May 2010 - 05:45 PM

That works if you want just a deck of cards, but what about a deck of cards that you'd want to do calculations on. For instance, in this code, in the array, Ace is at position 0, but it's value is not 0 when working with an actual card game. Can you elaborate on assigning values to each card as well?

This post has been edited by eoswins: 09 May 2010 - 05:47 PM

### #4 blixna_waka

Reputation: -5
• Posts: 60
• Joined: 14-July 10

Posted 15 July 2010 - 07:18 PM

mmm....very well written. (*thinks up possible implementations*)....

### #5 magedx9

Reputation: 1
• Posts: 39
• Joined: 08-November 08

Posted 21 March 2011 - 07:18 AM

eoswins, on 09 May 2010 - 07:45 PM, said:

That works if you want just a deck of cards, but what about a deck of cards that you'd want to do calculations on. For instance, in this code, in the array, Ace is at position 0, but it's value is not 0 when working with an actual card game. Can you elaborate on assigning values to each card as well?

eoswins it would be easy to make it give the right value when u return the rank u just need to make the arrays values the right ones that they are suppose to be like in a deck of cards a simple if statement or switch-case should do the trick like this:

```
public class card
{
private int rank,suit;
private String[] suits = {"Hearts", "Spades", "Diamonds", "Clubs" };
private String[] ranks  = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" };

public card(int rank, int suit)
{
this.suit = suit;
this.rank = rank;
}
//Returns the card name with the suit
//also overrides the toString method in Java.lang.Object
public @Override String toString()
{
return ranks[rank] + " of " + suits[suit];
}

public int getRank()
{
switch(rank)
{
case 0: return 1;
case 1: return 2;
case 2: return 3;
case 3: return 4;
case 4: return 5;
case 5: return 6;
case 6: return 7;
case 7: return 8;
case 8: return 9;
case 9: return 10;
case 10: return 10;
case 11: return 10;
case 12: return 10;
default: return 0;
}
}

public int getSuit()
{
return suit;
}
}

```

to use the code is simple just make a variable that stores say addNum you could just go
```addNum += c.getRank();
```

it adds the correct card values together and display addNum it should work fine

P.S

Great tutorial help me out alot

This post has been edited by magedx9: 21 March 2011 - 07:19 AM

### #6 townerj

Reputation: 0
• Posts: 10
• Joined: 20-February 12

Posted 02 May 2012 - 05:35 PM

```public class Hand extends Deck{

private Card[] hand;

public Hand()
{
Deck deck = new Deck();
for(int i = 0;i<=7;i++)
{
hand[i] = deck.drawFromDeck();
}
}

public static void main(String[] args)
{
Hand d = new Hand();
System.out.println(d);
}
}

```

i am trying to create a hand class to draw seven cards and store them in an array of Cards
and am not sure what is wrong i keep getting a null pointer exception. i am using the array List tutorial for
my deck class.

### #7 saurav431

Reputation: 0
• Posts: 1
• Joined: 07-October 12

Posted 07 October 2012 - 04:17 AM

townerj, on 02 May 2012 - 05:35 PM, said:

```public class Hand extends Deck{

private Card[] hand;

public Hand()
{
Deck deck = new Deck();
for(int i = 0;i<=7;i++)
{
hand[i] = deck.drawFromDeck();
}
}

public static void main(String[] args)
{
Hand d = new Hand();
System.out.println(d);
}
}

```

i am trying to create a hand class to draw seven cards and store them in an array of Cards
and am not sure what is wrong i keep getting a null pointer exception. i am using the array List tutorial for
my deck class.

initialize your array hand in constructor hand()

### #8 flaminsnowman99

Reputation: 3
• Posts: 45
• Joined: 15-December 12

Posted 04 February 2013 - 08:06 PM

Very well written tutorial. It's helped me a lot. One question though, how would I make a method that only draws a certain amount of cards? So it would take a int as a parameter and get that many cards?

### #9 BigGrizzle

Reputation: 3
• Posts: 9
• Joined: 20-February 13

Posted 20 February 2013 - 04:54 AM

flaminsnowman99, on 04 February 2013 - 08:06 PM, said:

Very well written tutorial. It's helped me a lot. One question though, how would I make a method that only draws a certain amount of cards? So it would take a int as a parameter and get that many cards?

To do this I made a separate class name CardPlayer who has a hand with a given handsize. Then modified the main so that it draws from the deck and gives it to the player.

I also modified the shuffle to use the Collections static method of shuffle and gave it its own method in the Deck class.

Here is the CardPlayer class.
```import java.util.ArrayList;//Import ArrayList.

public class CardPlayer{
//Declare field.
private ArrayList<Card> hand;
private int HANDSIZE = 5;

public CardPlayer(){
//Instantiates hand as a new array.
hand = new ArrayList<Card>();
}//End of method.

//Adds passed card value to the hand.
}//End of method.

public void showCards(){
//Will print each element of the hand
System.out.println(hand);
}//End of method.

public int handSize()
{
return HANDSIZE;
}
}

```

Here is the Deck class.
```import java.util.Random;
import java.util.ArrayList;
import java.util.Collections;

public class Deck {
private ArrayList<Card> cards;

Deck()
{
cards = new ArrayList<Card>();

for (int a=0; a<=3; a++)
{
for (int b=0; b<=12; b++)
{
}
}

}

public Card deal()
{
return cards.remove( 0 );
}

public int getTotalCards()
{
return cards.size();
}

public void shuffle()
{
//Use static method of collections class to shuffle the array.
Collections.shuffle(cards);
}
}

```

Here is my main.
```public class CardDemo{

public static void main(String[] args)
{
int index = 0;
Deck deck = new Deck();
Card C;
CardPlayer CardShark = new CardPlayer();

//Shuffle the deck
deck.shuffle();

//Deals a hand from Deck to CardPlayer.
while (index<CardShark.handSize())
{
C = deck.deal();
index ++;
}

//Shows the Player's hand.
CardShark.showCards();

}
}

```

Please critique the hell out of my code. I am a beginner and want to suck as little as possible.

### #10 Diesel712

Reputation: 0
• Posts: 14
• Joined: 10-June 15

Posted 10 June 2015 - 06:08 AM

Wow pretty impressive for a starter,
I'm a starter as well, and I understant the code that'S written but for some reasons I just can't figure some codes by myself.
So frustrating, It's not my first course of programming but just can't get the hang of the OOP. I guess understanding the code is not enough to write what I want.
OK
What if I want to remove the top of the pile.

would I just change de 0 to 52
to the line return cards.remove(52); and why would .removeLast won't work

Diesel712, on 10 June 2015 - 06:06 AM, said:

Wow pretty impressive for a starter,
I'm a starter as well, and I understant the code that'S written but for some reasons I just can't figure some codes by myself.
So frustrating, It's not my first course of programming but just can't get the hang of the OOP. I guess understanding the code is not enough to write what I want.
OK
What if I want to remove the top of the pile.

ref- Deck class line 30
would I just change de 0 to 52
to the line return cards.remove(52); and why would .removeLast won't work

### #11 BigGrizzle

Reputation: 3
• Posts: 9
• Joined: 20-February 13

Posted 10 June 2015 - 06:16 AM

Diesel712, on 10 June 2015 - 06:08 AM, said:

Wow pretty impressive for a starter,
I'm a starter as well, and I understant the code that'S written but for some reasons I just can't figure some codes by myself.
So frustrating, It's not my first course of programming but just can't get the hang of the OOP. I guess understanding the code is not enough to write what I want.
OK
What if I want to remove the top of the pile.

would I just change de 0 to 52
to the line return cards.remove(52); and why would .removeLast won't work

Diesel712, on 10 June 2015 - 06:06 AM, said:

Wow pretty impressive for a starter,
I'm a starter as well, and I understant the code that'S written but for some reasons I just can't figure some codes by myself.
So frustrating, It's not my first course of programming but just can't get the hang of the OOP. I guess understanding the code is not enough to write what I want.
OK
What if I want to remove the top of the pile.

ref- Deck class line 30
would I just change de 0 to 52
to the line return cards.remove(52); and why would .removeLast won't work

cards.remove(51) would work to remove from the other side of the pile because lists and arrays start with 0 in most programming language you have to subtract 1. .removeLast will work if you wrote a function in the class to call on the object.

### #12 Diesel712

Reputation: 0
• Posts: 14
• Joined: 10-June 15

Posted 10 June 2015 - 06:21 AM

thanks
I wrote it to fast of course 51 is the last index of the deck
loll

### #13 lalahou

Reputation: 0
• Posts: 1
• Joined: 07-February 20

Posted 07 February 2020 - 09:43 AM

BigGrizzle, on 20 February 2013 - 04:54 AM, said:

flaminsnowman99, on 04 February 2013 - 08:06 PM, said:

Very well written tutorial. It's helped me a lot. One question though, how would I make a method that only draws a certain amount of cards? So it would take a int as a parameter and get that many cards?

To do this I made a separate class name CardPlayer who has a hand with a given handsize. Then modified the main so that it draws from the deck and gives it to the player.

I also modified the shuffle to use the Collections static method of shuffle and gave it its own method in the Deck class.

Here is the CardPlayer class.
```import java.util.ArrayList;//Import ArrayList.

public class CardPlayer{
//Declare field.
private ArrayList<Card> hand;
private int HANDSIZE = 5;

public CardPlayer(){
//Instantiates hand as a new array.
hand = new ArrayList<Card>();
}//End of method.

//Adds passed card value to the hand.
}//End of method.

public void showCards(){
//Will print each element of the hand
System.out.println(hand);
}//End of method.

public int handSize()
{
return HANDSIZE;
}
}

```

Here is the Deck class.
```import java.util.Random;
import java.util.ArrayList;
import java.util.Collections;

public class Deck {
private ArrayList<Card> cards;

Deck()
{
cards = new ArrayList<Card>();

for (int a=0; a<=3; a++)
{
for (int b=0; b<=12; b++)
{
}
}

}

public Card deal()
{
return cards.remove( 0 );
}

public int getTotalCards()
{
return cards.size();
}

public void shuffle()
{
//Use static method of collections class to shuffle the array.
Collections.shuffle(cards);
}
}

```

Here is my main.
```public class CardDemo{

public static void main(String[] args)
{
int index = 0;
Deck deck = new Deck();
Card C;
CardPlayer CardShark = new CardPlayer();

//Shuffle the deck
deck.shuffle();

//Deals a hand from Deck to CardPlayer.
while (index<CardShark.handSize())
{
C = deck.deal();
index ++;
}

//Shows the Player's hand.
CardShark.showCards();

}
}

```

Please critique the hell out of my code. I am a beginner and want to suck as little as possible.

if i want to deal cards to 8 different players, how should i do it ??

### #14 modi123_1

• Suitor #2

Reputation: 15880
• Posts: 63,555
• Joined: 12-June 08

Posted 07 February 2020 - 09:46 AM

I would think you would make eight player objects. Probably held in a collection or something.