Convert 2d array to a graph with nodes and edges

Viewed 244

My problem is, for my pacman board creation I am using a 1d array, but I want to implement the BFS algorithm to the ghosts and to my understanding I can't perform that unless I have a graph with nodes and edges... So in my code I am trying to convert a newly created 2d array into a hash map and using an iterator for the Key (node) and that node contains the "Value" which is the specific index on the board. I hope I am on the right track, but am struggling with this.

package pacman;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.stream.Collectors;

import org.jgrapht.Graph;
import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.graph.DefaultUndirectedGraph;
import org.jgrapht.graph.SimpleWeightedGraph;

/**
 * This class is used to create a graph from a 2d array
 * 
 * @author Kody Berry
 *
 */
public class GraphCreation {
    private final static short levelData[] = { 19, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 22, 17, 16, 16, 16, 16,
            24, 16, 16, 16, 16, 16, 16, 16, 16, 20, 25, 24, 24, 24, 28, 0, 17, 16, 16, 16, 16, 16, 16, 16, 20, 0, 0, 0,
            0, 0, 0, 17, 16, 16, 16, 16, 16, 16, 16, 20, 19, 18, 18, 18, 18, 18, 16, 16, 16, 16, 24, 24, 24, 24, 20, 17,
            16, 16, 16, 16, 16, 16, 16, 16, 20, 0, 0, 0, 0, 21, 17, 16, 16, 16, 16, 16, 16, 16, 16, 20, 0, 0, 0, 0, 21,
            17, 16, 16, 16, 24, 16, 16, 16, 16, 20, 0, 0, 0, 0, 21, 17, 16, 16, 20, 0, 17, 16, 16, 16, 16, 18, 18, 18,
            18, 20, 17, 24, 24, 28, 0, 25, 24, 24, 16, 16, 16, 16, 16, 16, 20, 21, 0, 0, 0, 0, 0, 0, 0, 17, 16, 16, 16,
            16, 16, 20, 17, 18, 18, 22, 0, 19, 18, 18, 16, 16, 16, 16, 16, 16, 20, 17, 16, 16, 20, 0, 17, 16, 16, 16,
            16, 16, 16, 16, 16, 20, 17, 16, 16, 20, 0, 17, 16, 16, 16, 16, 16, 16, 16, 16, 20, 25, 24, 24, 24, 26, 24,
            24, 24, 24, 24, 24, 24, 24, 24, 28 };

    public static Graph<Integer, DefaultEdge> undirectedGraph = new DefaultUndirectedGraph<Integer, DefaultEdge>(
            DefaultEdge.class);

    public static Graph<Integer, DefaultEdge> createGraph(int[][] s) {
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        int k = 1;
        // Getting an iterator
        Iterator<?> mapIterator = map.entrySet().iterator();

        // Adding values to hash map
        for (int i = 0; i < s.length; i++) {
            for (int j = 0; j < s[i].length; j++) {
                if (s[i][j] == 16) {
                    map.put(k, s[i][j]);
                    k++;
                }
            }
        }

        while (mapIterator.hasNext()) {
            Map.Entry mapElement = (Map.Entry)mapIterator.next();
            int t = (int) mapElement.getKey();
            
            undirectedGraph.addVertex(t);
            
        }
        
        return undirectedGraph;
    }

    /**
     * Method used to convert the 1d array game board to a 2d array board
     * 
     * @param s
     * @return
     */
    public static int[][] oneDtoTwoDConverter(short[] s) {

        int array2d[][] = new int[15][15];


        for(int i=0; i<15;i++)
           for(int j=0;j<15;j++)
               array2d[i][j] = s[(j*15) + i]; 

        return array2d;// Returns new 2d array.
    }
    
    // Test output
    public static void main(String[] args) {
        int[][] s = oneDtoTwoDConverter(levelData);
        System.out.println(createGraph(s));
    }
}

My output is as follows currently.

([], [])

I am also using the JGraphT library. I have yet to figure out how to create edges, so if you have any suggestions on that I would greatly appreciate it.

PS. If you have a better Idea on how to do this let me know! Thank you :)

1 Answers

There might be more problems, but the most obvious for me is, that you create the Iterator before filling the map. When you iterate over it it will give you the contents from before filling it so it's empty.

If you move Iterator<?> mapIterator = map.entrySet().iterator(); to below the loop already more should be happening.

But this check for "s[i][j] == 16" looks a bit suspicious to me as well but this I haven't thought through enough yet but somehow I expect your map to look like {1: 16, 2: 16, 3: 16, ...} to write it in python terms.

Related