Jump to content

Java Programming Problem


danielbrthwt

Recommended Posts

I am new to java programming so i am trying to consolidate what i have learnt and i am writing a bot for a game called Flood-it and i encountered a problem where a variable was be incremented when it shouldn't, i narrowed it down to a line of code that from what i could see has no connection to the variable it is changeing.

im really sorry if my code is hard to understand but if you need me to explain something i will try, help would be much appreciated.

what i believe to be the error is in line 72 of the board class.

Somehow the line group[numOfSquares][1] = b; is affecting the priGroup varible in one of the Square class instances

Here are the links to the files ( the form wouldn't let me upload them )

www.megaupload.com/?d=QD09MU4R

www.megaupload.com/?d=YFH9VMV9

www.megaupload.com/?d=Y0E0MJAK

www.megaupload.com/?d=2OZ3APM9

Board class

public class Board 
{
	static int[] blacklist = new int[196];
	static int curGroup = 0;
	static int isGroup = 0;
	static int[][] group;
	static int[][] reset = new int[1][2];
	static int numOfSquares = 0;

	public static Row[] board = new Row[14];
	static int up;
	static int down;
	static int left;
	static int right;

	static int col;
	static int gid;
	static int multi;


	public Board( String[][] allColours )
	{

		for ( int i = 0; i < 14; i++ )
		{
			board[i] = new Row();
		}

		for ( int i = 0; i < allColours.length; i++ )
		{
			board[i].setState(allColours[i]);
		}
	}

	public static void printBoardNum()
	{
		System.out.println("--------------------------------------------------------");
		for ( int i = 0; i < board.length; i++ )
		{
			System.out.println(board[i].printStringNum());
			System.out.println("--------------------------------------------------------");
		}
	}

	public static void printBoardGroupIds()
	{
		System.out.println("--------------------------------------------------------");
		for ( int i = 0; i < board.length; i++ )
		{
			System.out.println(board[i].printGroupIds());
			System.out.println("--------------------------------------------------------");
		}
	}

	public static void findGroups()
	{
		for ( int a = 0; a < board.length; a++ )
		{
			for ( int b = 0; b < board.length; b++ )
			{
				numOfSquares = 0;
				//int[][] startingGroup = board[0].getGroupArray(0);
				//startingGroup = reset;
				group = reset;
				//startingGroup = board[0].getGroupArray(0);
				//startingGroup = reset;

				group[numOfSquares][0] = a;
				int[][] startingGroup = board[0].getGroupArray(0);
				startingGroup = reset;
				group[numOfSquares][1] = b;
				startingGroup = board[0].getGroupArray(0);
				startingGroup = reset;
				numOfSquares++;	
				//startingGroup = board[0].getGroupArray(0);
				//startingGroup = reset;

				int col = board[a].getSquareCol(B);
				//startingGroup = board[0].getGroupArray(0);
				//startingGroup = reset;

				blacklist[(a*13)+b] = 1;
				//startingGroup = board[0].getGroupArray(0);
				//startingGroup = reset;
				isGroup = findAround(a, b, col);
				//startingGroup = board[0].getGroupArray(0);
				//startingGroup = reset;

				if ( isGroup == 1 && board[a].getGroupId(B) == -1 )
				{
					board[a].addToGroup(b, curGroup);
					//startingGroup = board[0].getGroupArray(0);
					//startingGroup = reset;
					for ( int i = 0; i < group.length; i++ )
					{
						board[group[i][0]].setGroupArray(group[i][1], group);
						//startingGroup = board[0].getGroupArray(0);
						//startingGroup = reset;
						//System.out.printf(" *| %d | %d |*", group[i][0], group[i][1]);
					}
					//System.out.println();
					curGroup++;
					numOfSquares = 0;
					//startingGroup = board[0].getGroupArray(0);
					//startingGroup = reset;
				}

				int[][] test = board[0].getGroupArray(0);
				System.out.println("Test ( should be 0 ) : " + test[0][1]);

				//for ( int i = 0; i < startingGroup.length; i++ )
				//{
				//	System.out.println(startingGroup[i][0] + " + " + startingGroup[i][1]);
				//}
				//System.out.println();

			}
		}
	}

	private static int findAround(int a, int b, int col)
	{
		int[] around = new int[4];
		int inGroup = 0;

		if ( a > 0 ) around[0] = board[a-1].getSquareCol(B);
		else around[0] = 0;

		if ( a < 13 ) around[1] = board[a+1].getSquareCol(B);
		else around[1] = 0;

		if ( b > 0 ) around[2] = board[a].getSquareCol(b-1);
		else around[2] = 0;

		if ( b < 13 ) around[3] = board[a].getSquareCol(b+1);
		else around[3] = 0;

		if ( around[0] == col && blacklist[((a-1)*13)+b] != 1)
		{
			group = extendArray(group);
			group[numOfSquares][0] = a-1;
			group[numOfSquares][1] = b;
			numOfSquares++;

			blacklist[((a-1)*13)+b] = 1;
			board[a-1].addToGroup(b, curGroup);
			inGroup = 1;
			findAround( a-1, b, col );
		}

		if ( around[1] == col && blacklist[((a+1)*13)+b] != 1)
		{
			group = extendArray(group);
			group[numOfSquares][0] = a+1;
			group[numOfSquares][1] = b;
			numOfSquares++;

			blacklist[((a+1)*13)+b] = 1;
			board[a+1].addToGroup(b, curGroup);
			inGroup = 1;
			findAround( a+1, b, col );
		}

		if ( around[2] == col && blacklist[(a*13)+(b-1)] != 1)
		{
			group = extendArray(group);
			group[numOfSquares][0] = a;
			group[numOfSquares][1] = b-1;
			numOfSquares++;

			blacklist[(a*13)+(b-1)] = 1;
			board[a].addToGroup(b-1, curGroup);
			inGroup = 1;
			findAround( a, b-1, col );
		}

		if ( around[3] == col && blacklist[(a*13)+(b+1)] != 1)
		{
			group = extendArray(group);
			group[numOfSquares][0] = a;
			group[numOfSquares][1] = b+1;
			numOfSquares++;

			blacklist[(a*13)+(b+1)] = 1;
			board[a].addToGroup(b+1, curGroup);
			inGroup = 1;
			findAround( a, b+1, col );
		}

		return inGroup;

	}

	public static void scoreOptions()
	{
		int startingCol = board[0].getSquareCol(0);

		if ( board[0].getGroupId(0) != -1 )
		{
			int pinkC = 0;
			int pinkM = 0;

			int purpleC = 0;
			int purpleM = 0;

			int yellowC = 0;
			int yellowM = 0;

			int redC = 0;
			int redM = 0;

			int blueC = 0;
			int blueM = 0;

			int greenC = 0;
			int greenM = 0;

			int[][] startingGroup = board[0].getGroupArray(0);

			for ( int i = 0; i < startingGroup.length; i++ )
			{	
				if ( startingGroup[i][0] > 0 ) 
				{
					col = board[startingGroup[i][0]-1].getSquareCol(startingGroup[i][1]);
					gid = board[startingGroup[i][0]-1].getGroupId(startingGroup[i][1]);
					if ( gid != -1 ) multi = board[startingGroup[i][0]-1].getGroupArray(startingGroup[i][1]).length;
					else multi = 1;

					if ( col != startingCol )
					{
						if ( col == 1 ) 
						{
							pinkC++;
							pinkM += multi;
						}
						if ( col == 2 ) 
						{
							purpleC++;
							purpleM += multi;
						}
						if ( col == 3 ) 
						{
							yellowC++;
							yellowM += multi;
						}
						if ( col == 4 )
						{
							redC++;
							redM += multi;
						}
						if ( col == 5 )
						{
							blueC++;
							blueM += multi;
						}
						if ( col == 6 ) 
						{
							greenC++;
							greenM += multi;
						}
					}
				}

				if ( startingGroup[i][0] > 13 ) 
				{
					col = board[startingGroup[i][0]+1].getSquareCol(startingGroup[i][1]);
					gid = board[startingGroup[i][0]+1].getGroupId(startingGroup[i][1]);
					int multi;
					if ( gid != -1 ) multi = board[startingGroup[i][0]+1].getGroupArray(startingGroup[i][1]).length;
					else multi = 1;

					if ( col != startingCol )
					{
						if ( col == 1 ) 
						{
							pinkC++;
							pinkM += multi;
						}
						if ( col == 2 ) 
						{
							purpleC++;
							purpleM += multi;
						}
						if ( col == 3 ) 
						{
							yellowC++;
							yellowM += multi;
						}
						if ( col == 4 )
						{
							redC++;
							redM += multi;
						}
						if ( col == 5 )
						{
							blueC++;
							blueM += multi;
						}
						if ( col == 6 ) 
						{
							greenC++;
							greenM += multi;
						}
					}
				}

				if ( startingGroup[i][1] > 0 ) 
				{
					col = board[startingGroup[i][0]].getSquareCol(startingGroup[i][1]-1);
					gid = board[startingGroup[i][0]].getGroupId(startingGroup[i][1]-1);
					if ( gid != -1 ) multi = board[startingGroup[i][0]].getGroupArray(startingGroup[i][1]-1).length;
					else multi = 1;

					if ( col != startingCol )
					{
						if ( col == 1 ) 
						{
							pinkC++;
							pinkM += multi;
						}
						if ( col == 2 ) 
						{
							purpleC++;
							purpleM += multi;
						}
						if ( col == 3 ) 
						{
							yellowC++;
							yellowM += multi;
						}
						if ( col == 4 )
						{
							redC++;
							redM += multi;
						}
						if ( col == 5 )
						{
							blueC++;
							blueM += multi;
						}
						if ( col == 6 ) 
						{
							greenC++;
							greenM += multi;
						}
					}
				}

				if ( startingGroup[i][0] > 0 ) 
				{
					int col = board[startingGroup[i][0]].getSquareCol(startingGroup[i][1]+1);
					int gid = board[startingGroup[i][0]].getGroupId(startingGroup[i][1]+1);
					if ( gid != -1 ) multi = board[startingGroup[i][0]].getGroupArray(startingGroup[i][1]+1).length;
					else multi = 1;

					if ( col != startingCol )
					{
						if ( col == 1 ) 
						{
							pinkC++;
							pinkM += multi;
						}
						if ( col == 2 ) 
						{
							purpleC++;
							purpleM += multi;
						}
						if ( col == 3 ) 
						{
							yellowC++;
							yellowM += multi;
						}
						if ( col == 4 )
						{
							redC++;
							redM += multi;
						}
						if ( col == 5 )
						{
							blueC++;
							blueM += multi;
						}
						if ( col == 6 ) 
						{
							greenC++;
							greenM += multi;
						}
					}
				}
			}
		}

		else
		{

		}

	}



	public static int[][] extendArray( int[][] array )
	{
		int[][] newArray = new int[array.length+1][2];

		for ( int i = 0; i < array.length; i++ )
		{
			newArray[i] = array[i];
		}

		return newArray;
	}

	public static int[][] getGroup( int a, int B)
	{
		return board[a].getGroupArray(B);
	}

}

Square Class

public class Square 
{
	private String colour;
	private int colournum;
	private int groupid = -1;
	private int[][] priGroup;

	public void setState( String col )
	{
		colour = col;

		if (col.equals("pink")) colournum = 1;
		if (col.equals("purple")) colournum = 2;
		if (col.equals("yellow")) colournum = 3;
		if (col.equals("red")) colournum = 4;
		if (col.equals("blue")) colournum = 5;
		if (col.equals("green")) colournum = 6;
	}

	public String getColour()
	{
		return colour;
	}

	public int getNum()
	{
		return colournum;
	}

	public void addToGroup( int id )
	{
		groupid = id;
	}

	public int getGroupid()
	{
		return groupid;
	}

	public void setGroupArray( int[][] g )
	{
		priGroup = g;
	}

	public int[][] getGroupArray()
	{
		return priGroup;
	}
}

Edited by danielbrthwt
Link to comment
Share on other sites

Set a watch on priGroup. In the code from your post, the only place that member gets modified is in Square::setGroupArray, so also consider placing a breakpoint on that method and see how/where it gets called. HTH :)

i used the folowing statments to debug my program when i was trying to find the problem :

//startingGroup = board[0].getGroupArray(0);

//startingGroup = reset; ( had a breakpoint on this line to view the startingGroup variable before its reset )

and the only line that changed the variable once it was set was group[numOfSquares][1] = b;

Is this what you ment or did you mean something else

Link to comment
Share on other sites

I suspect the issue you are having is because some variables are been passed by reference (or pointer, depending on your language). In Java variables are often implicit passed by reference.

Instead of

group = reset;

try:

group = new int[1][2];

Thank you so much that fixed the problem i was having

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...