• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using Singly Linked List to Simulate Dealing Cards - Data Duplication Bug  RSS feed

 
Derek Chavez
Greenhorn
Posts: 2
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings JavaRanch, I lurked these forums for solutions in the past, but have created an account just now to ask about this.

I've got a singly linked list that I'm using to write a little program to play cards. Node -> Card, LinkedList -> Hand:



I am writing a method in a third class to deal cards from a single deck (leaving the possibility for 6 deck black jack in the future for example), here is the code in question:



So, Basically in the game I have 3 hands. A player hand, a dealer hand, and a third "hand" to keep track of which cards have been used so I don't deal 3 aces of spades.

I call the following:
oneDeckDeal(7,playerHand, playedCards);
oneDeckDeal(7,dealerHand, playedCards);

and get this as output when I print playerHand then dealerHand:

Player Hand:
King of Diamonds
10 of Clubs
9 of Diamonds
King of Clubs
10 of Diamonds
6 of Diamonds

Dealer Hand:
Queen of Hearts
2 of Diamonds
10 of Hearts
6 of Clubs
King of Hearts
5 of Clubs
4 of Hearts
King of Diamonds
10 of Clubs
9 of Diamonds
King of Clubs
10 of Diamonds
6 of Diamonds

I see the off by one error, but I'm more concerned with why dealerHand is getting all the cards from playerHand. When I enter the debugger, dealerHand starts off empty, but when the first card gets added in, the 6 from playerHand come with it. Any ideas?
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

The first thing I can think of is once the card is dealt to player or dealer, that specific card should be added to the playedCard deck (you did that) AND removed from the other party's deck. For example, say 6 of Diamonds is dealt to player, this card needs to be removed from the dealer deck. So this card will not be dealt to dealer when you call:

oneDeckDeal(7,dealerHand, playedCards);

How to do that? You probably need another parm (dealerHand) in the method. Now to make life simple the method name may need changing to distinguish who you are dealing card to, player or dealer.
 
Derek Chavez
Greenhorn
Posts: 2
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang: Thanks for the input. I am keeping duplicates out by passing in the hand with a copy of all previously played cards. This is line 8, if I randomly generate a card that has already been played, I generate a new one instead of dealing it. So I'm not actually "dealing" duplicates, but once I generate the first unique card during my second call to oneDeckDeal and add it to the hand, the rest of the previous hand comes with it. I suspect it's some kind of reference issue, but I'm not sure.

I also considered starting with an exhaustive list of cards and then flagging them played as they go to different hands, but I thought this method would be easier...
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Derek Chavez wrote:K. Tsang: Thanks for the input. I am keeping duplicates out by passing in the hand with a copy of all previously played cards. This is line 8, if I randomly generate a card that has already been played, I generate a new one instead of dealing it. So I'm not actually "dealing" duplicates, but once I generate the first unique card during my second call to oneDeckDeal and add it to the hand, the rest of the previous hand comes with it. I suspect it's some kind of reference issue, but I'm not sure.

I also considered starting with an exhaustive list of cards and then flagging them played as they go to different hands, but I thought this method would be easier...

There's actually a much simpler way to do this; and it's much closer to what actually happens when you deal cards:
  • You start out with a deck of 52 cards.
  • You shuffle it.
  • You deal out the cards from the top.

  • Luckily, there's a wonderful method in the Collections class called shuffle(), which does exactly what you want.

    Unfortunately, it performs very poorly with "linked" lists, so in fact you've hamstrung yourself by deciding on an implementation too soon.

    My advice would be to:
    (a) Make your card deck a List, not a LinkedList. That allows you to choose any List implementation you like.
    (b) Initialize it with an ArrayList, not a LinkedList; because they shuffle() very quickly. You might want to have a look at the RandomAccess interface, because it explains why - ArrayList implements it; LinkedList doesn't.
    Also: you already know how many cards there are going to be, so making it a LinkedList doesn't add much.

    HIH

    Winston
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!