Генерация графика из 2D массива в Java
Я хочу сгенерировать график из двухмерного массива для реализации игры 'floodit'(вы можете поиграть в нее здесь http://unixpapa.com/floodit). Таким образом, логика заключается в том, что смежные блоки одного цвета считаются одним узлом. Код работает не так, как задумано (все, что я положил в качестве входных данных, делит его на 36 узлов, что означает, что смежные блоки одного цвета не учитываются в одном узле. А затем на графике все элементы матрицы смежности равны 0). Конечная цель - решить данную доску, используя эвристические методы.
Вот мой код Код в основном в функции GraphNode и generateGraph
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>0 && i<6 && b.board[i-1][j]==b.board[i][j]) n[0]=1;
if(i<5 && i>=0 && b.board[i+1][j]==b.board[i][j]) n[1]=1;
if(j>0 && j<6 && b.board[i][j-1]==b.board[i][j]) n[2]=1;
if(j<5 && j>=0 && 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<Node> queue;
queue= new LinkedList<Node>();
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<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 && 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 && temp[i+1][j]!=1){
n.i=i+1;
n.j=j;
queue.add(n);
temp[i+1][j]=1;
}
if(nb[2]==1 && temp[i][j-1]!=1){
n.i=i;
n.j=j-1;
queue.add(n);
temp[i][j-1]=1;
}
if(nb[3]==1 && 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<6;i++)
for( j=0;j<6;j++)
{
if(temp[i][j]>0)continue;
gtemp=graphNode(i,j,b);
for(k=0;k<6;k++)
for(l=0;l<6;l++)
if(gtemp[i][j]==1)temp[i][j]=nodeNo;
System.out.println("Nodeno : ");
System.out.println(nodeNo);
nodeNo++;
}
for(i=0;i<6;i++)
for(j=0;j<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 && current!=top)g[current][top]=1;
if(i!=5 && current!=bottom)g[current][bottom]=1;
if(j!=0 && current!=left)g[current][left]=1;
if(j!=5 && 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<board.length;i++)
{
for(int j=0;j<board[i].length;j++)
{
nxtBoard[i][j] = board[i][j];
}
}
return nxtBoard;
}
// all neighboring boards
public ArrayList<Board> 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<board.length;i++)
{
for(int j=0;j<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<30;i++)
for(int j=0;j<30;j++)
System.out.println(graph[i][j]);*/
}
// for testing purpose
//public static void main(String[] args)
//{
//}
}