I'm making a quiz-game based on map territories with a system similar to the Risk game (except there are no tanks but tickets).

The map isn't the world but a custom map that I designed and I'll finish in the future, I just made the logic links among territories.

There are 10 territories who represent 10 different matters. The game I'm developing is for Android (Java), but I'm writing a prototype of the map in C# with Visual Studio to see if the idea of the map is good.

Now, I'm not asking for C# code to use in my app, I'm asking for a programming strategy I should use to implement my map in Code.

I wrote the class Zone to define each territory. I also wrote the class GameMap which implements 10 different Zones.

In the game logic, it's expected that just as risk, to attack 1 territory, you must have one near it. How can I implement at best this concept, of near and far territories in a dynamic and not long-static way?

The final result of what I want would be: I decide which territory attack in order to my owned territories. How can I proceed being as much as dynamic as I can and not write really long and in-efficient code?

EDIT: I'm no more looking for C# help. I'm directly implementing the map in Java and Javascript in both Client and Server sides.

  • \$\begingroup\$ I'm still unsure what you want to do. What do “near” and “far” territories mean? (Maybe you're looking for a graph data structure?) \$\endgroup\$ – Anko Aug 16 '15 at 0:27
  • \$\begingroup\$ Yeah I actually do! Any suggestions? \$\endgroup\$ – user1714647 Aug 16 '15 at 0:32

Like @Anko suggested, you should use a graph as data structure for your game map. If you use the this implementation of a graph in Java as a starting point you can represent your map as follows:

Graph<Zone> gameMap = new Graph<>();

Zone zone1 = new Zone("Zone1");
Zone zone2 = new Zone("Zone2");
Zone zone3 = new Zone("Zone3");
Zone zone4 = new Zone("Zone4");
Zone zone5 = new Zone("Zone5");
Zone zone6 = new Zone("Zone6");
Zone zone7 = new Zone("Zone7");
Zone zone8 = new Zone("Zone8");
Zone zone9 = new Zone("Zone9");
Zone zone10 = new Zone("Zone10");

gameMap.addEdge(zone1, zone2);
gameMap.addEdge(zone2, zone3);
gameMap.addEdge(zone2, zone4);

If you want to check if you can attack zone2 from zone1 you can just do

// can attack zone2 from zone1
gameMap.hasEdge(zone1, zone2);

Edit: I reimplemented the Java version in Javascript. You can use it like the Java version.

Here you can find the complete implementation of the above example in Java and Javascript.

| improve this answer | |
  • \$\begingroup\$ You sir helped me a lot. 10/10 would upvote (if only I had 15 rep) \$\endgroup\$ – user1714647 Aug 16 '15 at 16:24
  • \$\begingroup\$ May I ask how do you get this class to work? I don't find in Java SDK in Android Studio and the class I tried to import from that link has various erros. \$\endgroup\$ – user1714647 Aug 16 '15 at 16:56
  • \$\begingroup\$ There are too many things to implement for Generics. You forgot whole ST.java class and Graph constructor which is also based on strings. I think I'll stick to gameMap.addEdge(zone1.getName(), zone2.getName());. Should work anyway, shouldn't it? \$\endgroup\$ – user1714647 Aug 16 '15 at 19:04
  • \$\begingroup\$ I just used a HashMap and HashSet from the java standard library instead of the ST and SET classes from the original implementation to make things easier. If you use it with Graph<String> you get the same functionality. \$\endgroup\$ – tryzor Aug 16 '15 at 19:52
  • \$\begingroup\$ Hey man, you've been great. I completely integrated this in my app and I'm working now to make it .. work (that repetition) in a client/server environment. To do so, both Client and Server shall know how does the graph map work. Actually for the backend I'm using Parse that uses JS, you got any JS solution to do a thing like this? If you have, you totally gained this answer as accepted. \$\endgroup\$ – user1714647 Sep 10 '15 at 19:55

I think you would have to manually define which zones are neighbouring each other - unless you can programmatically parse your map to figure this out automatically.

For example with a class attribute like int neighbouringZonesIds[]; that you have to fill.

| improve this answer | |
  • \$\begingroup\$ Assuming that a zone is a class, how can I programmatically differ them? I forgot to say that it's true that map has 10 territories, but can be played among 5 and 8. I designed it in a way that it's still possible to play in any case (the 3 central territories which are static, and the other at sides who can vary between 2 and 5). \$\endgroup\$ – user1714647 Aug 16 '15 at 0:24

Here's an alternative to the graph approach, if your map is made up of large grids (I've never heard of Risk played similar games to what you've described).

Have a World containing Zone objects. Each Zone has an X and Y coordinate. This saves you the trouble of having to add edges.

If Zone 1,1 only has the neighbour Zone 1,2 then the player may only cross between these two - you get the idea.

| improve this answer | |
  • \$\begingroup\$ The map is not a grid :) Anyway tryzor was really helpful to me. I'll accept his answer where I'll be done with Graph Map in JS too. I'm currently working on doing it in a structure array-like, a kind of raw representation for graphs which seems to work. \$\endgroup\$ – user1714647 Sep 11 '15 at 22:52
  • \$\begingroup\$ Ah, I see, looks a bit like the Game of Thrones map. That approach seems good. If you had units walking from one area to another, you could have defined the paths for zone edges more easily too. \$\endgroup\$ – Lolums Sep 12 '15 at 7:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.