• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

OO design - event handling

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all, quick question on event handling as it relates to OO design. I'm a relative novice to Java (and programming in general) and am building a card game program. Without going into specifics, I'm struggling with how to handle the flow of the game from player, to computer, and back again. For instance, the user would choose a card in his or her hand to play. Then, the computer will play a card. Now, it's the user's turn to play another card, and so on and so forth.
I've got a number of classes in my program, one being the GUI class and another being the gameFlow() class which handles (or is supposed to handle) the back and forth of the flow of the game. I've got the actionPeformed() method in my GUI class - but the only event handling I know how to do is the basic:
...if (event.getEvent() == startbutton) {
do this;
do that;
}
if (event.getEvent() == exitbutton) {
do this;
do that;
}
etc, etc.
This is how I was taught event-handling - which works fine for simple things like starting or ending the game, but it doesn't seem to be appropriate for situations in which the program is waiting for some type of response from the user (choosing a card to play, for instance). I would think there's a better way to do this, but I just don't know how.
Then again, maybe that IS how I'm supposed to do things.... Any help or advice would be greatly appreciated. I realize my question is pretty generic, but I didn't want to post a bunch of code when that might not really be necessary. In addition to creating a working program, I really want to strive to use good OO design wherever possible. This is a learning experiance, and I'd rather take the time to do it right, rather than cobble something together that works but is architected poorly.
TIA!!
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did similar thing when I took the SCJD assignment. In such case, you can make use of MVC model. Your Java Applet or application is the view, and your cards are data, while you need to have a controller class.
Thus, for any response from the GUI, the event handler captures it, however, it only extracts necessary data from the event, say, which card is shows by the player, and pass all data to the controller, and let the controller to determine whether the do this or do that, whether the system should wait or give response.
Nick
 
blacksmith
Posts: 1332
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Alex McCormick:
This is how I was taught event-handling - which works fine for simple things like starting or ending the game, but it doesn't seem to be appropriate for situations in which the program is waiting for some type of response from the user (choosing a card to play, for instance). I would think there's a better way to do this, but I just don't know how.


The applications I've written have basically done what you post, except that instead of including the execution code in the actionPerformed() method - which quickly leads to major spaghetti code - it calls an appropriate function, instead.
The way I would do it is to call a function within the GUI class that would handle the immediate changes in the GUI (e.g., highlighting the selected card), then call the game flow class if appropriate to do the appropriate things with respect to the game. The game flow class might then call back to the GUI class if some of the game results needed to be displayed.
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The way I would do it is to call a function within the GUI class that would handle the immediate changes in the GUI


Huh... Maybe it is better to have a controller class, which knows how to handle all possible events, instead of putting all things in the GUI class.
In the point of view of code reuse and maintanence, it is easier to reuse, as maybe we will have another card game that can use some pieces of code.
This approach is similar to MVC model 1, which the view also performs the controller tasks.
For coding issues, both methods work. But for design (or OO) issues, seems applying patterns will be better. Of course, for simple systems, patterns may not be that useful. But in the view of a learning process, it is a good chance.
Nick
 
Alex McCormick
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the replies guys. Maybe I need to learn more about the MVC philosophy... Any good links you could recommend?
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Alex McCormick:
Thanks for the replies guys. Maybe I need to learn more about the MVC philosophy... Any good links you could recommend?


You might try searching at javaworld in addition to here on the ranch
[ April 20, 2004: Message edited by: Richard Quist ]
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may read this link:
http://ootips.org/mvc-pattern.html
Nick
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic