Search code examples
algorithmdesign-patternsgame-enginegraph-algorithmchess

data structure for representing non square board game


I am trying to design a board game, which is not square. Here we have 2 different types of pieces(attacker & defenders). Both can move to any adjacent free intersection. The attacker player can also jump on defender player if there is empty space in the adjacent intersection in the same line. Considering these cases i can think of storing the board as array. But this is not a right choice, as i need to harcode attackable position from each indexes. need your suggestions for designing this board. One more option is using graph and maintain the direction of the node(Left,Right,Top, Down), but this would require 3 Down nodes on the top vertex of the board. enter image description here


Solution

  • The two times I had to do this, I created a play_line data type. I had the canonical graph with nodes and edges; a play_line is a sequence of edges.

    A legal move to a free, adjacent intersection is a trivial property from the graph alone. A piece A at node m can move to any node n where

    • edge (m,n) exists
    • node n is empty

    A jump from m, over n, to p exists where

    • edge (m,n) exists
    • edge (n,p) exists
    • node n contains a piece D (defender)
    • node p is empty
    • Along the play_line containing edge (m,n), (n,p) is the next edge on that line.

    Does that help?


    Update after OP comment

    There is nothing to maintain for the play_line objects, as the lines of play do not change once initialized. These are hard-coded from the game board, an enhancement of the graph. For instance if the board is labeled

        a
    b c d e f
    g h i j k
    l m n o p
      q r s
    

    then the first full row is a line of play containing five nodes in order, [b, c, d, e, f]. There are corresponding graph edges (correct by construction) of (b, c), (c, d), (d, e), (e, f). Note that your code must either traverse this in either direction, or you make a second play_line in the reverse order.