Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

This has been confusing me for the past week as I am trying to make my first actual game. It's only my 2nd year learning Java so I am really trying to learn how to program like a professional organization and how classes interact and whatnot.

I found that the MVC is generally the pattern for professional programs (although there are different ways). I've been trying to code in this pattern and have been confounded by the connections. I don't understand code-wise how to connect them but I understand the concept.

For example, in my game I have a Card class which contains all the logic for the card but I also have a CardDisplay class which contains the paint override and extends JComponent displaying the image of the card and the listeners. Should I consolidate them together or are they suppose to be separate?

Also how do I connect the view to the controller such as my View class containing the JFrame and update methods + GUI elements. Do I create a instance variable of View in my Game class? Do I Create the variable in the Main method?

Basically, I really am just trying to progress past learning the "actual language" and more of the technique and structure behind professional programs. I've been looking at the structure of Notch's Minicraft (not minecraft!) source code if that helps?

share|improve this question
3  
Be careful trying to learn good structure from other people's source code -- as a relative beginner you can often come to the wrong conclusions. I would strongly recommend avoiding the source code for projects like minicraft, which was made in 48 hours and consequently the focus of the author was probably not on good, scalable software engineering techniques. –  Josh Petrie Mar 8 '12 at 16:00

1 Answer 1

MVC, like all paradigms, is a tool. It has it uses and its problems. The high-caliber professional engineer does not dogmatically adopt MVC but takes from it what he or she needs and discards the rest. The focus should be on building a readable, maintainable piece of software, not on following MVC to the letter.

I have a Card class which contains all the logic for the card but I also have a CardDisplay class which contains the paint override and extends JComponent displaying the image of the card and the listeners. Should I consolidate them together or are they suppose to be separate?

It is generally a good idea to keep game logic (Card) and render logic (CardDisplay) distinct, regardless of whether or not you are trying to employ MVC or not. Consequently I would say this is a good approach.

Also how do I connect the view to the controller such as my View class containing the JFrame and update methods + GUI elements. Do I create a instance variable of View in my Game class? Do I Create the variable in the Main method?

The controller object generally knows about (and thus contains some reference to) the view and model objects so as to facilitate the communication between them. The controller is usually the really vaguely-defined aspect of any particular MVC scenario, especially in games, where most of the controlling comes from a singular input layer that feeds its updates directly into the game logic code. You rarely actually need a distinct controller type for every logic or render type (for example, you should not really need CardController).

The bits of your code responsible for turning user input into commands probably lives somewhere in your game loop and those commands translate to operations on the set of Card objects currently in play. Your card render objects can contain a reference to the corresponding Card object (or be given one immediately to render) such that they always draw the most up-to-date rendition of the card's logical state.

You will probably want to simply create an instance of your overall view (which presumably owns all the CardDisplays and other GUI elements and such, in your game class. You probably will have one instance of your game class created somewhere in your Main function or entry point.

share|improve this answer
    
Thank you so much! –  Rendition Mar 8 '12 at 16:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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