title: Graph generation from 2D array in java
body: <p>I want to generate a graph from a 2D array to implement the game 'floodit'(you can play it here <a href="http://unixpapa.com/floodit" rel="nofollow">http://unixpapa.com/floodit</a>). So the logic is that adjacent blocks of same colors are counted as one node. The code is not working as intended(Anything I put as input, it divides it into 36 nodes, meaning the adjacent same colored blocks are not counted into the same node. And then in the graph, all elements of adjacency matrix is 0). The final target is to solve the given board using heuristic methods.</p>
  
  <p>Here's my code. The code is mainly in graphNode and generateGraph function</p>
  
  <pre><code>import java.awt.Color;
  import java.util.ArrayList;
  import java.util.HashSet;
  import java.util.Iterator;
  import java.util.LinkedList;
  import java.util.Queue;
  
  public class Board 
  {
      private int g;
      private int board[][];
      private Board parent = null;
      public Board(int[][] colors, Board parent,int g) 
      {   
          // construct a board from an N-by-N
          this.board = colors;                                            //array of colors   
          this.parent=parent;                                         // (where colors[i][j] = color in                                                   //row i, column j)
          this.g = g;
      }
  
      public int f() 
      {
          return g+heuristic1();
      }
      // returns the estimated distance from current board to final state using heuristic1
      public int heuristic1() 
      {
          return 0;   
      }
      // returns the estimated distance from current board to final state using heuristic2
      public int heuristic2()  
      {
          return 0;
      }
      // is this board the goal board? i.e., all color same. 
      public boolean isGoal() 
      {
          return false;
      }
          public int[] neighbours(int i, int j, Board b)
          {
              int[] n=new int[4];
              if(i&gt;0 &amp;&amp; i&lt;6 &amp;&amp; b.board[i-1][j]==b.board[i][j]) n[0]=1;
              if(i&lt;5 &amp;&amp; i&gt;=0 &amp;&amp; b.board[i+1][j]==b.board[i][j]) n[1]=1;
              if(j&gt;0 &amp;&amp; j&lt;6 &amp;&amp; b.board[i][j-1]==b.board[i][j]) n[2]=1;
              if(j&lt;5 &amp;&amp; j&gt;=0 &amp;&amp; b.board[i][j+1]==b.board[i][j]) n[3]=1;
              return n;
          }
      public int[][] graphNode( int i, int j, Board b)
          {
              int r;
              int[] nb=new int[4];
              Node n=new Node();
              Queue&lt;Node&gt; queue;
              queue= new LinkedList&lt;Node&gt;();
              int[][] temp=new int[6][6];
              temp[i][j]=1;
              n.i=i;
              n.j=j;
              queue.add(n);
  
              Node current;
              System.out.println("node creation started");
              while(!queue.isEmpty()){
                  current = queue.remove();
                  i=current.i;
                  j=current.j;
                  for( r=0;r&lt;4;r++)nb[r]=0;
                      nb=neighbours(i,j,b);
                  System.out.println("neighbours");
                  System.out.println(nb[0]);
                  System.out.println(nb[1]);
                  System.out.println(nb[2]);
                  System.out.println(nb[3]);
  
                      if(nb[0]==1 &amp;&amp; temp[i-1][j]!=1){
                          n.i=i-1;
                          n.j=j;
                          queue.add(n);
                          temp[i-1][j]=1;
                          System.out.println("creating node");
                      }
                      System.out.println(i);
                      System.out.println(j);
                      if(nb[1]==1 &amp;&amp; temp[i+1][j]!=1){
                          n.i=i+1;
                          n.j=j;
                          queue.add(n);
                          temp[i+1][j]=1;
                      }
                      if(nb[2]==1 &amp;&amp; temp[i][j-1]!=1){
                          n.i=i;
                          n.j=j-1;
                          queue.add(n);
                          temp[i][j-1]=1;
                      }
                      if(nb[3]==1 &amp;&amp; temp[i][j+1]!=1){
                          n.i=i;
                          n.j=j+1;
                          queue.add(n);
                          temp[i][j+1]=1;
                      }
  
  
              } 
              return temp;
          }
          public int[][] generateGraph(Board b)
          {
              System.out.println("graph gen started");
              int[][] g=new int[100][100];
              int[][] temp=new int[6][6];
              int[][] gtemp=new int[6][6];
              int[] nb=new int[4];
              int i, j, k, l, top=0, bottom=0, left=0, right=0, current;
              int nodeNo=1;
  
              for( i=0;i&lt;6;i++)
                  for( j=0;j&lt;6;j++)
                  {
                      if(temp[i][j]&gt;0)continue;
                      gtemp=graphNode(i,j,b);
  
                      for(k=0;k&lt;6;k++)
                          for(l=0;l&lt;6;l++)
                              if(gtemp[i][j]==1)temp[i][j]=nodeNo;
                      System.out.println("Nodeno : ");
                      System.out.println(nodeNo);
                      nodeNo++;
                  }
              for(i=0;i&lt;6;i++)
                  for(j=0;j&lt;6;j++){
                      System.out.println(temp[i][j]);
                      current=temp[i][j];
                      if(i!=0)top=temp[i-1][j];
                      if(i!=5)bottom=temp[i+1][j];
                      if(j!=0)left=temp[i][j-1];
                      if(j!=5)right=temp[i][j+1];
                      if(i!=0 &amp;&amp; current!=top)g[current][top]=1;
                      if(i!=5 &amp;&amp; current!=bottom)g[current][bottom]=1;
                      if(j!=0 &amp;&amp; current!=left)g[current][left]=1;
                      if(j!=5 &amp;&amp; current!=right)g[current][right]=1;
                  }
              return g;
          }
  
      public int[][] getCopy(int board[][])
      {
          int [] [] nxtBoard = new int[board.length][board[0].length];
          for (int i=0; i&lt;board.length;i++)
          {
              for(int j=0;j&lt;board[i].length;j++)
              {
                      nxtBoard[i][j]  = board[i][j];
              }
          }
          return nxtBoard;
      }
      // all neighboring boards
      public ArrayList&lt;Board&gt; neighbors() 
      {
          return null;
      }
      // does this board equal y?
      public boolean equals(Object y) 
      {
          return false;
      }
  
      public int get_g()
      {
          return g;
      }
  
      public Board getParent() {
          return parent;
      }
  
      // string representation of the
      //board (in the output format specified below)
      public String toString() 
      {
          String str="\ng: "+g+" h:"+heuristic1()+"\n";
          for (int i=0; i&lt;board.length;i++)
          {
              for(int j=0;j&lt;board[i].length;j++)
              {
                  str += (board[i][j]+" ");
              }
              str +="\n";
          }
          return str;
  //      System.out.println();
  
      }
  
          public static void main(String [ ] args){
              int[][] colors;
              int[][] graph=new int[100][100];
              colors=new int[][]{{1,2,3,4,1,2},{4,3,2,5,6,3},{5,6,1,6,5,4},{4,3,2,1,4,5},{5,6,1,2,3,6},{6,5,4,3,2,1}};
              Board b= new Board(colors, null, 0);
              graph=b.generateGraph(b);
  
              /*for(int i=0;i&lt;30;i++)
                  for(int j=0;j&lt;30;j++)
                      System.out.println(graph[i][j]);*/
          }
      // for testing purpose
      //public static void main(String[] args) 
      //{
  
      //}
  }
  </code></pre>