Re: Swing Battleship Game




"Andrew Thompson" <u32984@uwe> wrote in message news:72a0cfdde75b8@xxxxxx
KDawg44 wrote:
..
For a class I am taking I have to write a Battleship game. I am using
Swing to program a GUI and I would like to make it so that there are
two panes holding the 11x11 grid for the spots on the computer's and
the player's boards. I would like the position of the ships on the
players board to be marked somehow and I would like the user to be
able to click on the a spot on the other board to choose their shot
when it is there turn. I would like to also prevent them from
choosing the same spot twice.

What should I use to create the boards in Swing?

Layouts (same as you would use in AWT).

I would say the opposite.
For a game board that is anything other than very simple and you know you
won't extend it in the future, then components are fine. But anything else
and pure painting on a Graphics object is the way to go.
This ensures that with a good OO design you have an easily extendable
application.
Otherwise you end up doing a lot of fighting against layout managers and
components. But I guess we all do it once to really appreciate that...
(I've done it several times!)
Choose a null layout for the game board and separate your model and painting
logic. Use a clever set of classes that implement Paintable or something and
collect these together in your main JComponent subclass. In an overidden
paintComponent iterate over your paintables and delegate the painting to
them. You will need a Z order concept implementing so your Paintables may
want to be Comparables also. Your paintables will probably mirror there
Model class counterpart. The Paintable knows about rendering the Model, and
the Model holds the data in nicely designed OO structure. You will likely
have a Board and Pieces classes in both Model and View tiers.

--
Mike W


.