• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can't split card shuffle method into a separate class

 
Bill Stanard
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have created a class named DeckOfCard.java which creates a deck of cards (using a class named Card.java) and then shuffles it. This class includes a method to deal cards from the created, shuffled deck one at a time. What I want to do is split out, into a separate class, the shuffle method. I know that I�m missing something simple, but I keep getting a compile error. Here, first, is the one-file (single class) code for Card.java:

Here is the DeckOfCards.java code, the single-class create-shuffle-deal class:

My question is this: If I want to set up Shuffle.java as a separate class and use its method to shuffle the deck created in DeckOfCards, how do I have to configure Shuffle.java and how do I have to call its shuffle method? Here is what I wrote for a shuffle class:

Here is what I wrote for a test class (with the non-working call commented):

When I comment out the shuffle method in the original code and then run the Test.java with the Shuffle.riffle( ) call, I get the following error:
Test.java:10: riffle(Card[]) in Shuffle cannot be applied to (DeckOfCards)
Bottom line: splitting out the shuffle code to a separate class and calling its method, named riffle( ), from test is a no-go. I know it is something simple, but its obviousness escapes me.

[ December 02, 2005: Message edited by: Bill Stanard ]

[ December 05, 2005: Message edited by: Bill Stanard ]
[ December 05, 2005: Message edited by: Bill Stanard ]
 
Henry Wong
author
Marshal
Pie
Posts: 20829
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The riffle() method is not static. To call it, you would need an instance of a Shuffle class. Or make the method static.

Henry
 
Bill Stanard
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Henry.

When I run the following


I then get the following error:

which seems to me to say that a DeckOfCards object cannot be "processed" by the riffle method

even though the method clearly calls for a Card argument. Is the DeckOfCards that riffle( ) cannot be applied to possibly NOT a Card[] object. If not, (a) what is it? and, (b) how do I have to declare the riffle method to achieve the shuffle desired?
 
Henry Wong
author
Marshal
Pie
Posts: 20829
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am guessing once you fix the static method problem, this error will go away.

Henry
 
Junilu Lacar
Bartender
Pie
Posts: 6529
21
Java Linux Mac Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the reason for creating a separate Shuffle class? A shuffle method seems more appropriate as part of the Deck class. I can see you having a ShuffleStrategy interface that has a shuffle method and the Deck class taking an implementation of the ShuffleStrategy. That way, you could easily change the way the cards in a Deck are shuffled, if that's what you're trying to achieve.

 
Bill Stanard
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good question, Junilu. Less than satisfactory answer: I want to create a card game project for a group of students, so I want to break out a series of methods into separate classes (card, deck, shuffle, deal, discard, draw, score, et cetera).

I have been having the devil's own time calling classes that contain methods to use in this manner. I'm obviously making some fundamental error, but I don't seem to be able to figure it out. I can write the entire card game in a couple of classes, but I can't split it into a larger number of classes. Is there a project that you all know of that demonstrates the kind of interlinked series of classes like my projected card game that will give me a picture of how I must structure these classes?
[ December 05, 2005: Message edited by: Bill Stanard ]
 
Mahesh Sethi
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
riffle method is expecting array of Card objects as parameter, not the DeckofCard object.
However, if you pass the member "deck" (which is array of Card objects) of DeckofCard object, you may expect riffle to execute properly.

You can create a "getDeck" method in DeckofCard class that returns private member "deck" which is an array of card object and call this method on "boo" instance of DeckofCard class, passing as parameter to riffle.

Method to be included in DeckofCard Class:

public Card[] getDeck(){

return deck;

}


Call in main method:

s.riffle(bobo.getDeck());

Mahesh
 
Junilu Lacar
Bartender
Pie
Posts: 6529
21
Java Linux Mac Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bill Stanard:
Good question, Junilu. Less than satisfactory answer: I want to create a card game project for a group of students, so I want to break out a series of methods into separate classes (card, deck, shuffle, deal, discard, draw, score, et cetera).[ December 05, 2005: Message edited by: Bill Stanard ]


Bill, some of the things in the list don't logically qualify as classes, e.g. shuffle, deal, discard, draw. Notice that these are all verbs. The nouns are better candidates for classes, e.g. card, deck, score.

Some candidate classes might be: Card, Deck, Game, Dealer, Player, Hand, Shuffler.

Shuffler - takes one or more decks and shuffles them.

Game - encapsulates the rules of the particular card game: determines how many players can join a game, how many cards can be dealt, checks Hands to see if it has won the game, etc. Implementations may include PokerGame, BlackJackGame.

Player - encapsulates rules for playing a certain game. Variations of this class may include AggressivePlayer, ConservativePlayer, DumbLuckPlayer.

Dealer - a special kind of Player. Controls the Game and what Players can do, deals Hands to Players.

Hand - a collection of Card objects; varies depending on Game. Implementations may include PokerHand, BlackJackHand, etc.
 
Bill Stanard
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bill, some of the things in the list don't logically qualify as classes, e.g. shuffle, deal, discard, draw. Notice that these are all verbs. The nouns are better candidates for classes, e.g. card, deck, score.

Some candidate classes might be: Card, Deck, Game, Dealer, Player, Hand, Shuffler.


Aahhh... I think I need some direction in how to think about classes, methods, et cetera. Can you (or any of the helpfull folks in this neighborhood )suggest a book that might set me thinking more logically about the Java structure. I very much like the linguistic approach you seem to be suggesting and would like to pursue that...
 
Junilu Lacar
Bartender
Pie
Posts: 6529
21
Java Linux Mac Scala Spring
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic