aspose file tools*
The moose likes Beginning Java and the fly likes Method help? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Method help?" Watch "Method help?" New topic
Author

Method help?

Alan Smith
Ranch Hand

Joined: Oct 19, 2011
Posts: 162

Hi,

I am writing a card game at the moment. I have a Card class, Deck class, Player class, Opponent class, basically everything I need. I know how methods work and how to write them but this is the most complex program (in my eyes anyway ) that I have done in terms of logic. As the program gets bigger, I am getting confused as to where I place my methods or in other words, what class(s) should have them. I have the obvious methods in their appropriate classes ie the deck can shuffle and deal its cards, the player and opponent can receive/play a card, etc. The problem is where I place the rules for the game. When a player plays a card for example, what class should check whether the card he has chosen is playable; when the players turn is over, what decides who should go next. I'm slowly but surely ending up with lots of private methods below my main method to handle all the rules and 'the game in play'. It just seems messy to me. Where does one class stop and another begins!? Sorry, its a strange post, but maybe some experienced Java programmers understand what I'm trying to say !

Thanks,
Alan
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1510
    
    5

Hi Alan Smith,

In my opinion, what you need is a descent class diagram and understanding of IS-A and HAS-A relationships. Once you get clear there, you'll understand who are 'actors' in your application, what are their respective 'responsibilities' etc.

e.g. I would create a Game class - which will have few methods - one to keep track of what's going on in the game, another to validate the rules (or in your words - which card is 'playable' after current card) and so on. This is just one approach. In designing, nothing is right or wrong. Its just bad or good.
Or I would not create two separate classes for Player and Opponent. Because by functionality, both are same. So, I would create two objects of Player class. Advantage of this approach is it would be relatively easy to increase number of players.
And so on.

Once you start to approach your problem from those angles, it'll be much easier (instead of directly opening editor and start typing code).

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39855
    
  28
Why have you got Player and Opponent? You don’t have players and opponents in card games. An opponent is a player the same as you are.

Sounds as though you have too few classes. Get your card and deck classes working, then get your players working later. For Cards you have suit and rank, so you might have suit and rank classes too (an enum is good for that sort of thing). Your players would have take and receive methods. The cards would need an ordering. That is enough to keep you busy all day.
You do not want lots of private methods like that. Your cards and players are simply the raw materials for Games.

And yes, games can be very complicated.
Alan Smith
Ranch Hand

Joined: Oct 19, 2011
Posts: 162

I have my Card and Deck classes sorted. They work perfectly and I could use them for any card game.

Campbell Ritchie wrote:Why have you got Player and Opponent? You don’t have players and opponents in card games. An opponent is a player the same as you are.


^^this is my problem! I realise that an opponent is a player but take this scenario. At the start of the game all players get to swap a few cards that they are dealt with other cards from the deck (basically a random shot of getting better cards). The human player gets asked what cards they would like to swap and then they input their choices. The opponent is computer controlled so the difference is that I need a seperate method for the opponents to swap cards based on conditions ie. if they have good cards already, don't make them swap; if they have decent cards they will swap based on a certain probability etc. In a nutshell, I made a seperate class for the opponent because its ai controlled.
Alan Smith
Ranch Hand

Joined: Oct 19, 2011
Posts: 162

Anayonkar Shivalkar wrote:Hi Alan Smith,
In my opinion, what you need is a descent class diagram and understanding of IS-A and HAS-A relationships. Once you get clear there, you'll understand who are 'actors' in your application, what are their respective 'responsibilities' etc.


Yes, I find this approach very useful and is why I decided to write cards games. A deck HAS cards, a player HAS a hand, etc.

e.g. I would create a Game class - which will have few methods - one to keep track of what's going on in the game, another to validate the rules (or in your words - which card is 'playable' after current card) and so on. This is just one approach. In designing, nothing is right or wrong. Its just bad or good.


This sounds good, I never thought about having a Game/Game manager class. I'll try that.

Or I would not create two separate classes for Player and Opponent. Because by functionality, both are same. So, I would create two objects of Player class. Advantage of this approach is it would be relatively easy to increase number of players.
And so on.


I explained in another post that I did this as the opponent is ai controlled but I understand where your coming from.

Once you start to approach your problem from those angles, it'll be much easier (instead of directly opening editor and start typing code).

I hope this helps.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Alan Smith wrote:I have my Card and Deck classes sorted. They work perfectly and I could use them for any card game.

Campbell Ritchie wrote:Why have you got Player and Opponent? You don’t have players and opponents in card games. An opponent is a player the same as you are.


^^this is my problem! I realise that an opponent is a player but take this scenario. At the start of the game all players get to swap a few cards that they are dealt with other cards from the deck (basically a random shot of getting better cards). The human player gets asked what cards they would like to swap and then they input their choices. The opponent is computer controlled so the difference is that I need a seperate method for the opponents to swap cards based on conditions ie. if they have good cards already, don't make them swap; if they have decent cards they will swap based on a certain probability etc. In a nutshell, I made a seperate class for the opponent because its ai controlled.


Sounds like you want an interface or abstract class Player, and two different implementations: HumanPlayer and ComputerPlayer. The difference is that, for example, HumanPlayer's swapCards() method takes input from the console or GUI, while ComputerPlayer's swapCards() method is written by you to randomly swap out cards, or to evaluate the cards and swap based on whatever rules you decide to code. As far as the game control goes though, they're both just Players.
Alan Smith
Ranch Hand

Joined: Oct 19, 2011
Posts: 162

Jeff Verdegan wrote:
As far as the game control goes though, they're both just Players.


They are but every method (or behaviour) the computer player has is ai controlled, ie what card to play, what cards to swap, etc. Ill look at an abstract class though, thanks.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Alan Smith wrote:
Jeff Verdegan wrote:
As far as the game control goes though, they're both just Players.


They are but every method (or behaviour) the computer player has is ai controlled, ie what card to play, what cards to swap, etc.


I know. But the game control doesn't need to care about that. Whether Player is an abstract class or an interface, the game control treats human and computer as just plain old Player. It doesn't need to know anything about the existence of the different implementaitons.

The "AI control" vs. UI input behaviors come from the different implementations of the relevant methods on the Player class/interface.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39855
    
  28
Maybe you want two Player objects which are instances of the same class, but you want an AI control for one and a HCI (=human-computer interface) control for the other. You could have methods like displayHand and playCard; you could call the latter like this:
myPlayer.playCard(Suit.CLUBS, Rank.JACK); You might need some Exception handling in case the user tries to play a non-existent card.
Alan Smith
Ranch Hand

Joined: Oct 19, 2011
Posts: 162

Ok guys, thanks for the help. Ill try an abstract class for player and a HumanPlayer and ComputerPlayer from that. A game 'manager' that could handle whether a card which is about to be played can be played could be useful, ie it would handle the rules. If a player or opponent selects a card to play i could say something like:

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39855
    
  28
You’re welcome
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Method help?