The best data structure for representing the playing field

I am trying to bring a board game to the computer world, and the board consists of 16 places, 6 for each side and 4 in the middle. The board has a diamond shape, and two ends - both command bases. In the game, peices only move forward to the opponent’s base (with special abilities, of course). So here is my question: what do you think is the best data structure to represent the playing field? The first thing that came to my mind was a tree, but I really do not like the idea, because there will be two "roots". Any suggestions?

The fee is as follows:

& & & & & & * * * * $ $ $ $ $ $ 

therefore, the team and the team can only move to the $ team and vice versa, * be neutral territory

+6
language-agnostic data-structures
source share
5 answers

Is this essentially a square? Is a diamond shape just a matter of interpretation? So ..

 MAAA BMAA BBMA BBBM 

if so, maybe just use a 2D array and use your display system to “turn it” into a diamond.

change

I think you can match moving to an array - if your $ figures only move up and down or up and down, it looks like my "B", moving only up or 'right'. Similary, if "&" pieces only move down and down or down and to the right, as the "A" pieces move down or left.

The idea is that, inside the program, think up and down about your data, but when you present your game state to your user, simply draw it in the diamond configuration.

+11
source share

Your concern is not only how to present the board, but also how to represent the parts, because the board and the parts must communicate their mutual presence and effects.

Therefore, how you present your board will determine how you present your pieces and the rules of the game that limit the board and pieces.

Your first problem is how to present the parts.

Part of a game or slot machine is an ideal model for object-oriented programming.

Let me illustrate by discarding ads like public, static, etc.

 abstract class BasicUnit { // determine constraints of movement here. // update position and return new position abstract Position move(Direction d); abstract Position getPosition(); Arsenal arsenal; } class Worker extends BasicUnit { Position move(Direct d) { //whateveer, etc } } class farmer extends Worker { Position move(Direct d) { //whateveer, etc } } class Warrior extends BasicUnit { Position move(Direct d) { //whateveer, etc } } class Sniper extends Warrior { Position move(Direct d) { //whateveer, etc } } 

Now you need to decide whether the position of the parts is on the board

  • central panel: positions of parts are registered only on the board
  • piece in the center: positions are registered only on parts
  • redundant: you need to over-update both the piece and the board when the piece moves.

For most board games, a centrist piece would not be a good idea, because you would have to search every detail to determine if a position was taken.

If you want to navigate the platform, you will need to find each position of the board to find the position of the part.

For redundancy, you must make sure that the positions registered both on board and in pieces are not biased. If you plan to allow the game on the Internet via the Internet, where sessions can be suspended and hibernated, you may encounter problems with synchronization.

So, the answer to your question is a hashed vector to represent the board.

The hashed vector is a collection with two access doors - one access to the position, the second - the key. Your hashed vector will allow you to access

  • panel by position to find out which unit is in position.
  • part identifier to find out where its board is located.

You cannot imagine a board as a tree unless your is a multi-dimensional board game. A tree is needed when you have a tree, staircase or castle that is on the landing, so when the unit reaches the horizontal position of the board, it needs to go into the vertical position of the stairs or castle, And in the castle the block needs to be allocated to several rooms. Or there is a witch on a tree that can grab the device into a bottle with a tangled path. Therefore, using a tree structure to represent your board will be an unnecessary complication in programming your game.

It does not matter the square of a diamond or circle, etc. You just need to list the position of the board. The listing method should be convenient for your rules.

This means that you should not list one part as (1,3), and then list its adjacent figure as (2,7) - this is just common sense. Since the neighbors (1,3) are equal to (0,2), (1,2), (2,2), (0,3), (2,3), (0,4), (1,4) and (2.4), but not (2.7).

Therefore, you need a two-dimensional hashed vector.

To satisfy your need to find out which unit is at the x, y position of your board:

 BasicUnit getPosition(x,y) 

Also, figuring out the position of (x, y) units.

 Position getUnit(BasicUnit unit) 

You can then plan your game for expansion so that the player who has achieved victory can continue to play at the next level, which has a different shape. Your two-dimensional hashed vector will continue to be used as you separate the presentation layer of your software into its data structure.

You simply insert more positions into your vector.

You can view my Java implementation with a 1-dimensional hashed vector at http://code.google.com/p/synthfuljava/wiki/HashVector

translate it into your programming language and add another vector dimension to it.

+4
source share

What about two trees with four four fields? (I mean the link to the same objects)

+1
source share

I would use some kind of schedule. How:

 class Field { private List<Field> neighbours; } 

Or not a list, but an array containing null references for border fields. Any board can be presented with this.

0
source share

Why not use a simple array structure? If the board has a diamond shape, it is square / rectangular, right?

 Dim myBoard(5,5) As Array 

Of course, if the board has a strange shape (for example, there are places and things in the middle), this may not work.

0
source share

All Articles