Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Connections between classes (confused)

connection

  • Please log in to reply
6 replies to this topic

#1 toto_7

toto_7

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 244 posts

Posted 08 May 2011 - 03:27 AM

Hello,

Im trying to build a board game called "River crossing" in Java. My idea:

Have one board class where there create the main board of the game.
Contains pocket elements

public class board extends JFrame implements ActionListener{}



public class pocket extends JButton{}



public class piece{}



Separate classes per game's levels.

public class level_1{}



There is a way to modify the board from board class depends of in what level you are?

*Any better ideas for structure, are welcomed

thank you

Edited by toto_7, 08 May 2011 - 08:15 AM.

  • 0

#2 ZekeDragon

ZekeDragon

    CC Leader

  • Retired Mod
  • PipPipPipPipPipPipPip
  • 1263 posts

Posted 08 May 2011 - 08:29 AM

You may want to treat all Levels generically (so you'd only need Level to extend JFrame), and keep your individual Level data in a file you load at runtime. This allows you to modify the level without having to recompile your Java program. However, I don't know what data you will need in each Level. If a level will require unique method calls in your code, than clearly yes you will need this structure, I'm just averse to utilizing inheritance for every potential nesting of different objects within one program especially when the change can be represented mostly by differences in internal data rather than usable interface.

Also, I have learned that one of the Ways of Java is to program to Interfaces and abstract classes rather than non-abstract classes:
public abstract class Level extends JFrame
{
    public abstract ActionListener getLevelEventListener();
    ...
}
That way you could make different implementations of the Level class without having to change any of the code that utilizes Level.
public class LevelImpl1 extends Level
{
    // Provide a GUI-based Level implementation
}

public class LevelImpl2 extends Level
{
    // Provide very different implementation
}
You may want to just go with an interface and not extend JFrame if code that uses the Level doesn't call JFrame methods. This way the code that uses the Level object is independent of the visual representation of the data within the Level object, making your program more flexible. You could potentially implement River crossing in any other type of window, in fullscreen, or even on the command-line or over the internet. Can't do that by extending JFrame!

EDIT: AH! You just edited it. It's a good idea to have a separate object that represents a piece, but I don't know for sure about the pocket. If you disconnect your Level/Board from the JFrame, you may also want to disconnect Pocket from JButton. It's always a good idea and in good form in Java to capitalize your class names, too.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#3 toto_7

toto_7

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 244 posts

Posted 08 May 2011 - 08:35 AM

class Level will be a new class, or i can do it with class board?
  • 0

#4 ZekeDragon

ZekeDragon

    CC Leader

  • Retired Mod
  • PipPipPipPipPipPipPip
  • 1263 posts

Posted 08 May 2011 - 08:48 AM

Well, whichever name you choose for it is fine, I just prefer using more generic code that I can change it's behavior with by simply modifying the object state rather than subclassing and writing another class. Code reuse is a wonderful thing, but it should be reserved for when you really need to write new code, not just change what the level looks like or where you can place pieces on the game board. I'm not sure how River Crossing works, could you provide an analogy with another game to give me an idea of how the level board will look and how it changes with different games you play? It really is what you're trying to do that defines how you'd write the class structure.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#5 toto_7

toto_7

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 244 posts

Posted 08 May 2011 - 08:52 AM

I can't think a game similar to this. Here is a link on YouTube:

YouTube - How to Play River Crossing by ThinkFun
  • 0

#6 ZekeDragon

ZekeDragon

    CC Leader

  • Retired Mod
  • PipPipPipPipPipPipPip
  • 1263 posts

Posted 08 May 2011 - 09:24 AM

Okay, here's how I'd structure this game in an OO System:
Game
Has a -> Board
Has a -> Hero
Has a -> ChallengeCard[]

Hero
* Mostly just a placeholder for User input.

Board
Is a -> Serializable
Has a -> ChallengeCard
Has a -> Log[]
* ChallengeCard holds int[] which tells Board locations of
* stumps. Board then generates display of Stump locations
* and places default log locs. Can save game data since
* it is Serializable.

ChallengeCard
Is a -> Serializable
* Simple Dumb Data Type of int[].
* Also needs to have default Log locations in Log[]
* perhaps has an at() method or some wrapper methods.

Log
Is a -> Serializable
* Has a size value, indicating how many stumps a log can traverse
* Holds two integers to refer to which Stumps the log is connecting
This allows you to separate several of your concerns. I wouldn't use too much inheritance with this program since it's not really necessary. Each element in this game could be boiled down to a few simple variables (the locations of the stumps, and the logs and their locations) and then data to represent those variances in a Board called ChallengeCard. All you need to do is write a class that loads ChallengeCard data from a text file and you're golden, you could produce any composition of challenge for the player of the game without recompilation.

I didn't factor it in, but you could also make the board itself variably sized, and allow for more coordinate positions of Stumps. I used a simple int[] since there are only 25 possible stump locations, and that should still work up to millions of potential stump locations, just rendering it might get interesting.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#7 toto_7

toto_7

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 244 posts

Posted 08 May 2011 - 10:05 AM

Thank you, i will analyse the information you gave me and i will modify my code.

Generally, do you know how you can set an imageicon from a class to a JPanel where is to another clasS?
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download