3D Matrix Game - Python Code Examples

Create an equal sided 3D matrix of nodes

id       = 100
mtrx     = []
mtrxedge = 5

for x in range(0,mtrxedge):
    mtrx.append([])
    for y in range(0,mtrxedge):
        mtrx[x].append([])
        for z in range(0,mtrxedge):
            mtrx[x][y].append(Node(x,y,z,id))
            id += 1

Display 3D matrix of nodes

mtrx     = [......]
mtrxedge = 5

for x in range(0,mtrxedge):
    for y in range(0,mtrxedge):
        for z in range(0,mtrxedge):
            mtrx[x][y][z].display()

Matrix node class

class Node:
    '''
    The node at each cell in the 3D matrix.

    Attributes:
      x                 node's x coordinate
      y                 node's y coordinate
      z                 node's z coordinate
      up                y axis (y+1) adjacent node, if any
      down              y axis (y-1) adjacent node, if any
      forward           z axis (z+1) adjecent node, if any
      backward          z axis (z-1) adjecent node, if any
      left              x axis (x-1) adjecent node, if any
      right             x axis (x+1) adjecent node, if any
      wormhole          wormhole node, if any
      id                node's ID (integer)
    '''

    def __init__(self,x,y,z,id):
        '''
        Initialize a matrix node.
        '''
        self.x        = x
        self.y        = y
        self.z        = z
        self.up       = None
        self.down     = None
        self.forward  = None
        self.backward = None
        self.left     = None
        self.right    = None
        self.wormhole = None
        self.id       = id

    def get_id(self):
        '''
        Return a Node's id.
        '''
        return self.id

    def get_coords(self):
        '''
        Return a node's coordinates as a tuple (x,y,z).
        '''
        return (self.x,self.y,self.z)

    def get_nav_info(self):
        '''
        Return a node's navigation information as a tuple.

        The returned navigation tuples are (x,y,z) coordinates of the
        destination node or 'None' if there is no destination.

        The order of the list is
        [ up, down, forward, backward, left, right, wormhole ].
        '''
        navup       = \
            'None' if not self.up else self.up.get_coords()
        navdown     = \
            'None' if not self.down else self.down.get_coords()
        navforward  = \
            'None' if not self.forward else self.forward.get_coords()
        navbackward = \
            'None' if not self.backward else self.backward.get_coords()
        navleft     = \
            'None' if not self.left else self.left.get_coords()
        navright    = \
            'None' if not self.right else self.right.get_coords()
        navwormhole = \
            'None' if not self.wormhole else self.wormhole.get_coords()

        return [ navup,navdown,navforward,navbackward,navleft,
                 navright,navwormhole ]

    def set_up(self,node):
        '''
        Set the up link.
        '''
        self.up = node

    def set_down(self,node):
        '''
        Set the down link.
        '''
        self.down = node

    def set_forward(self,node):
        '''
        Set the forward link.
        '''
        self.forward = node

    def set_backward(self,node):
        '''
        Set the backward link.
        '''
        self.backward = node

    def set_left(self,node):
        '''
        Set the left link.
        '''
        self.left = node

    def set_right(self,node):
        '''
        Set the right link.
        '''
        self.right = node

    def set_wormhole(self,node):
        '''
        Set the wormhole link.
        '''
        self.wormhole = node

    def minimum_display(self):
        '''
        Display basic node information (x, y, z, and id)
        '''
        print('node: {},{},{} - {}'.
              format(self.x,self.y,self.z,self.id))