All Downloads are FREE. Search and download functionalities are using the official Maven repository.

pacman.game.internal.Node Maven / Gradle / Ivy

There is a newer version: 2.0.1.0
Show newest version
package pacman.game.internal;

import pacman.game.Constants.MOVE;

import java.util.EnumMap;

/*
 * The class is a data structure used to represent a node in the graph. Each maze is a set of connected nodes and 
 * each node has some adjacent nodes that correspond to the enumeration MOVE. Each node stores all the information 
 * required to check and update the current state of the game.
 */
public final class Node {
    public final int x, y, nodeIndex, pillIndex, powerPillIndex, numNeighbouringNodes;
    // Each move, give the node it leads to?
    public final EnumMap neighbourhood = new EnumMap(MOVE.class);

    public EnumMap allPossibleMoves = new EnumMap(MOVE.class);
    public EnumMap allNeighbouringNodes = new EnumMap(MOVE.class);
    public EnumMap> allNeighbourhoods = new EnumMap>(MOVE.class);

    /*
     * Instantiates a new node.
     */
    public Node(int nodeIndex, int x, int y, int pillIndex, int powerPillIndex, int[] _neighbourhood) {
        this.nodeIndex = nodeIndex;
        this.x = x;
        this.y = y;
        this.pillIndex = pillIndex;
        this.powerPillIndex = powerPillIndex;

        MOVE[] moves = MOVE.values();

        for (int i = 0; i < _neighbourhood.length; i++) {
            if (_neighbourhood[i] != -1) {
                neighbourhood.put(moves[i], _neighbourhood[i]);
            }
        }

        numNeighbouringNodes = neighbourhood.size();

        for (int i = 0; i < moves.length; i++) {
            if (neighbourhood.containsKey(moves[i])) {
                EnumMap tmp = new EnumMap(neighbourhood);
                tmp.remove(moves[i]);
                allNeighbourhoods.put(moves[i].opposite(), tmp);
            }
        }

        allNeighbourhoods.put(MOVE.NEUTRAL, neighbourhood);

        int[] neighbouringNodes = new int[numNeighbouringNodes];
        MOVE[] possibleMoves = new MOVE[numNeighbouringNodes];

        int index = 0;

        for (int i = 0; i < moves.length; i++) {
            if (neighbourhood.containsKey(moves[i])) {
                neighbouringNodes[index] = neighbourhood.get(moves[i]);
                possibleMoves[index] = moves[i];
                index++;
            }
        }

        for (int i = 0; i < moves.length; i++)//check all moves
        {
            if (neighbourhood.containsKey(moves[i].opposite()))//move is part of neighbourhood
            {
                int[] tmpNeighbouringNodes = new int[numNeighbouringNodes - 1];
                MOVE[] tmpPossibleMoves = new MOVE[numNeighbouringNodes - 1];

                index = 0;

                for (int j = 0; j < moves.length; j++)//add all moves to neighbourhood except the one above
                {
                    if (moves[j] != moves[i].opposite() && neighbourhood.containsKey(moves[j])) {
                        tmpNeighbouringNodes[index] = neighbourhood.get(moves[j]);
                        tmpPossibleMoves[index] = moves[j];
                        index++;
                    }
                }

                allNeighbouringNodes.put(moves[i], tmpNeighbouringNodes);
                allPossibleMoves.put(moves[i], tmpPossibleMoves);
            }
        }

        allNeighbouringNodes.put(MOVE.NEUTRAL, neighbouringNodes);
        allPossibleMoves.put(MOVE.NEUTRAL, possibleMoves);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy