0

This question already has an answer here:

I'm making revisions to a Console version of contract bridge I made in Java. The source can be found here: Bridge

So most of the code is terrible and I've been trying make improvements wherever possible. The game runs but the design is bad.

It currently has three classes: Card Deck BridgeConsole

All the game related code is in BridgeConsole but I've been told it's too long. So I'm considering separating some of the code in there. A GameState (renaming BridgeConsole to Main) class comes to mind but I'm not sure how to implement it. I've seen examples online where they have methods like init(), cleanup(), draw(), etc. but I'm not sure how the to properly implement it in my game. I'm also not sure where the methods I have to run the AI logic would go. Another possibility is instead of using ArrayList to hold the players hand, just create a Hand Class. Also removing all the static methods and variables I have in the BridgeConsole class.

I am looking for some suggestions or tips on good design for this particular project. I'm not satisfied by the being able to run the game, I'd for the code to be better so any help would appreciated.

marked as duplicate by TZHX, GlenH7, user40980, user22815, Kilian Foth Jun 4 '15 at 14:40

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • @gnat: What? OP never mentioned SRP. – Robert Harvey May 27 '15 at 4:49
  • 1
    @RobertHarvey: "all the game-related code is in BridgeConsole but I've been told it's too long" sure sounds like a likely violation of the SRP. – Eric Lippert May 27 '15 at 4:52
  • @RobertHarvey what Eric said (god object), plus "...I'm considering separating some of the code in there" – gnat May 27 '15 at 4:55
0

Despite being a god object, if you always feel that the code you're writing is suitable for the role of BridgeConsole, then the problem is with the name! Rename it something more specific, and separate the other tasks into their respective classes. It is easier said than done, so perhaps it would be simply easier to rewrite the class from scratch. Before you do so, definitely keep a backup somewhere you can refer to!

Remove the class and then begin creating classes to perform the tasks that BridgeConsole did , but focused on the individual tasks. Some component ideas include:

  1. An EventBus class which only handles notifying listeners of events. In this way, you can decouple many of the things BridgeConsole does and it would allow you to easily break it down into individual components.
  2. Another trick you can use is to create a class which draws, or is responsible for printing the state onto the screen. It shouldn't contain any logic other than what is required to show the current state on the screen.
  3. A game state class (probably the most similar to your current BridgeConsole class) that only provides information related to the game state.
  4. If the game state class is too large, consider creating also a Board state class which keeps track of the hands, cards played, this sort of thing. The game state can instead focus on overall game state (for example, is the game started? who are the players? How many points does each player have? Who won?).

If you've applied all these changes, and you still find yourself with a class that does too much, list out all the things that it does for you, and strongly consider dividing the class into components which perform each individual item in that list, much like you did for BridgeConsole.

And, if you find yourself pulling out your hair, consider simply rewriting the entire program. It shouldnt' take you as long because you've already written it, plus you have working code to boot. It would also teach you a valuable lesson on how to structure your code better. The fact that you realize the problem with BridgeConsole only means you've grown as a developer since you've first made it, so your second attempt should surely fix many of the issues.

I hope that helps. If you run into any difficulties, please mention them in the comments and I'll try to help.

Not the answer you're looking for? Browse other questions tagged or ask your own question.