Advice for a simple "Risk" game

I’m a rookie in Java, recently finished my first course and are about to start a second one (concentrating in Network, JDBC and Swing).

However, coding is the best way to improve skills, so I was thinking of start coding a simple game, something like “Risk”.

However, how would I create that playable Map? Should I use an “ugly” map that I don’t show that catches mouseclicks, or is there a more clever solution?

Also, If anyone knows of source somewhere that could be useful to have a look at, please let me know.

Regards

A nice way to do the map with proper irregular outlines is to use an indexing map behind the actual drawn map.

The indexing map “pixels” point directly into an array of the territories (reality is you could do it with just an array of bytes rather than an image).

So to look up the territory clicked on the map graphic you’d do:


territory = territories[indexMap[WIDTH * mouseEvent.getY() + mouseEvent.getX()]];

where territory 0 might be “the sea”, and all the others point to the various other Risk territories.

You can then paint them in PSP or the Gimp or something and save as a raw indexed image.

Cas :slight_smile:

Cas :slight_smile:

Now that sounds like a great advice! Time to bail out from work and do some more serious stuff :wink:

Probing the color (read byte value) of some image would be one solution. The other would be to use polygons to describe the field borders and then iterate a list of polygon objects, testing whether they contain the click position.

That’s not as fast as the bitmap solution, but it should be fast enough for your purpose and you could easily add a “map editor” to your game to let the user create custom maps by providing the borders.

I think, the original Risk has ~50 fields, a loop like

Polygon[] fieldBorders;
for (int i = 0; i  <fieldBorders.length; i++) {
  if (fieldBorders[i].contains(mouseX, mouseY))
    return i;
  }
}

should be fast enough. If not, you could make use of the fact that countries are grouped by continent and check for the continents first. This should reduce the test to 6 continents and ~10 fields. However, I don’t think this would be necessary.

The polygon solution has one additional advantage. You could easily highlight the field under the mouse and/or the field the user clicked.

If you have an image as map because that looks better, you can still draw a polygon over that image, using a translucent color, for example new Color(255, 255, 0, 128).

Sounds interesting, i haven’t had the time to try it out yet, any of the solutions. Have to finish my course first.
I’ve always been thinking about how to do this solutions though, some year ago I started out with the same idea of a game in Python, but never managed to get a good one.
(I was thinking of using SVG graphics but never found a module to render it
:frowning: )