This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Where to store the game state. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Where to store the game state." Watch "Where to store the game state." New topic
Author

Where to store the game state.

Timmy Ryan
Ranch Hand

Joined: Dec 09, 2007
Posts: 63

I have a board game in which a human can play against the computer.

The details about an on-going human v computer game are stored in a Game class.
My problem is that I dont know where is the correct place to store this game object should be, which must persist between human moves.
As the view should know nothing about the model or the controller.
When a human takes there move I have to pass the start and end squares of the move to some class that will validate the move.
Timmy Ryan
Ranch Hand

Joined: Dec 09, 2007
Posts: 63

Maybe I didnt explain the situation in the original post great.

In the Jpanel which recieves the users moves it needs to call another class to verify the users move and to calculate the computers response.
Today this it needs a copy of the game state object, but where should this game object be stored.
In the Jpanel class that controls the user interface? Does this not mean that the UI knows about the game when it should not.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
sounds like it would be part of the control mechanism, along with listeners etc
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

it depends on how your game is structured,
if it is MVC then in the model

if it is split into 2
1) listeners,game logic
2) gui
then with the listeners/game logic.

Timmy Ryan
Ranch Hand

Joined: Dec 09, 2007
Posts: 63

Does it really matter if the listeners are added in the view code or the controller code eg in the constructor of the JPanel to have

or is it better to pass the JPanel reference to the controller and add the listnerers there.
Keeing the view code strictly for updating the UI.
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

Timmy Ryan wrote:Does it really matter if the listeners are added in the view code or the controller code eg in the constructor of the JPanel to have

or is it better to pass the JPanel reference to the controller and add the listnerers there.
Keeing the view code strictly for updating the UI.


The bit in bold is the official line, but i never really achieved it, so will pass the baton along
Timmy Ryan
Ranch Hand

Joined: Dec 09, 2007
Posts: 63

I have carried out a bit of refactoring on the project.

The controller will create the view through composition.
Currently I have a number of sub panels in the view and these are implemented as singletons.
So when one panel wanted to change the appearance of a component on another panel they would call.
from within its own panel.

Q1. Should each panel be a singleton, I am thinking not.
Q1a. If one component wants to change another should this done through the controller?

In the view there are JSplitPane which contain JSplitPane and so on.
Q2. In the top level view creation class GameView should I have a reference to all of these sub components, e.g


GameView contains all the sub containers and components,
Q3. In the code below should I have the addMouseListener for a panel.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Where to store the game state.