# Fast Help please

• April 25th, 2013, 03:41 PM
Extremist252
Hey guys,

My projects's deadline is 1 hour away and i need to know what are the base cases that i should do while searching in a 2 array for connected pieces so i basically use a DFS but i don't know what's wrong with the base cases so can anyone tell me what are they and how to code them?
• April 25th, 2013, 03:45 PM
Norm
Re: Fast Help please
You need to define what you mean by DFS, 2 array, connected pieces and base case.
• April 25th, 2013, 03:55 PM
Extremist252
Re: Fast Help please
public int connected(Point test){
ArrayList<Point> connection=new ArrayList<Point>();
Point upLeft=new Point(test.getX()-1,test.getY()-1);
Point up=new Point(test.getX(),test.getY()-1);
Point upRight=new Point(test.getX()+1,test.getY()-1);
Point left=new Point(test.getX()-1,test.getY());
Point right=new Point(test.getX()+1,test.getY());
Point downLeft=new Point(test.getX()-1,test.getY()+1);
Point down=new Point(test.getX(),test.getY()+1);
Point downRight=new Point(test.getX()+1,test.getY()+1);

for(int i=0;i<connection.size();i++){
if (connection.get(i).getX()==upLeft.getX() && connection.get(i).getY()==upLeft.getY()){
upLeft=new Point();
this.getBoard()[upLeft.getY()][upLeft.getX()]=0;
}
if (connection.get(i).getX()==up.getX() && connection.get(i).getY()==up.getY()){
up=new Point();
this.getBoard()[up.getY()][up.getX()]=0;
}
if (connection.get(i).getX()==upRight.getX() && connection.get(i).getY()==upRight.getY()){
upRight=new Point();
this.getBoard()[upRight.getY()][upRight.getX()]=0;
}
if (connection.get(i).getX()==left.getX() && connection.get(i).getY()==left.getY()){
left=new Point();
this.getBoard()[left.getY()][left.getX()]=0;
}
if (connection.get(i).getX()==right.getX() && connection.get(i).getY()==right.getY()){
right=new Point();
this.getBoard()[right.getY()][right.getX()]=0;
}
if (connection.get(i).getX()==downLeft.getX() && connection.get(i).getY()==downLeft.getY()){
downLeft=new Point();
this.getBoard()[downLeft.getY()][downLeft.getX()]=0;
}
if (connection.get(i).getX()==down.getX() && connection.get(i).getY()==down.getY()){
down=new Point();
this.getBoard()[down.getY()][down.getX()]=0;
}
if (connection.get(i).getX()==downRight.getX() && connection.get(i).getY()==downRight.getY()){
downRight=new Point();
this.getBoard()[downRight.getY()][downRight.getX()]=0;
}

}

if(upLeft.getX()>=0 && upLeft.getY()>=0 ){
if(this.getColor(upLeft)==this.getColor(test)&& this.getColor(test)!=0){
if(this.getColor(test)==1){
whiteCount++;
connected(upLeft);
}
if(this.getColor(test)==2){
blackCount++;
connected(upLeft);
}
}
}
if (up.getX()>=0 && up.getY()>=0 && up.getX()<8 && up.getY()<8){
if(this.getColor(up)==this.getColor(test)&& this.getColor(test)!=0){
if(this.getColor(test)==1){
whiteCount++;
connected(up);
}
if(this.getColor(test)==2){
blackCount++;
connected(up);
}
}
}
if (upRight.getX()>=0 && upRight.getY()>=0 && upRight.getX()<8 && upRight.getY()<8){
if(this.getColor(upRight)==this.getColor(test)&& this.getColor(test)!=0){
if(this.getColor(test)==1){
whiteCount++;
connected(upRight);
}
if(this.getColor(test)==2){
blackCount++;
connected(upRight);
}
}
}
if (left.getX()>=0 && left.getY()>=0 && left.getX()<8 && left.getY()<8){
if(this.getColor(left)==this.getColor(test)&& this.getColor(test)!=0){
if(this.getColor(test)==1){
whiteCount++;
connected(left);
}
if(this.getColor(test)==2){
blackCount++;
connected(left);
}
}
}
if (right.getX()>=0 && up.getY()>=0 && right.getX()<8 && up.getY()<8){
if(this.getColor(right)==this.getColor(test)&& this.getColor(test)!=0){
if(this.getColor(test)==1){
whiteCount++;
connected(right);
}
if(this.getColor(test)==2){
blackCount++;
connected(right);
}
}
}
if (downLeft.getX()>=0 && up.getY()>=0 && downLeft.getX()<8 && downLeft.getY()<8){
if(this.getColor(downLeft)==this.getColor(test)&& this.getColor(test)!=0){
if(this.getColor(test)==1){
whiteCount++;
connected(downLeft);
}
if(this.getColor(test)==2){
blackCount++;
connected(downLeft);
}
}
}
if (down.getX()>=0 && down.getY()>=0 && down.getX()<8 && down.getY()<8){
if(this.getColor(down)==this.getColor(test)&& this.getColor(test)!=0){
if(this.getColor(test)==1){
whiteCount++;
connected(down);
}
if(this.getColor(test)==2){
blackCount++;
connected(down);
}
}
}
if (downRight.getX()>=0 && up.getY()>=0 && up.getX()<8 && up.getY()<8){
if(this.getColor(downRight)==this.getColor(test)&& this.getColor(test)!=0){
if(this.getColor(test)==1){
whiteCount++;
connected(downRight);
}
if(this.getColor(test)==2){
blackCount++;
connected(downRight);
}
}
}
else if(this.getColor(test)==1){
return whiteCount;
}
else if(this.getColor(test)==2){
return blackCount;
}
return -1;

}

--- Update ---

i'm coding the Lines Of Action game so i'm basically trying to figure out if all the white or black pieces are connected and based on that i can decide the winner so i'm trying to check the 8 possible positions for a connection

--- Update ---

here 0 means that there is no checker.....1 stands for white checkers and 2 stands for black checkers
• April 25th, 2013, 04:12 PM
Norm
Re: Fast Help please
Sorry, I forgot to say: Please edit your post and wrap your code with code tags:
[code=java]
[/code]
to get highlighting and preserve formatting.

Quote:

here 0 means that there is no checker.....1 stands for white checkers and 2 stands for black checkers
Is this important for understanding the code? I'd expect to see it as a comment in the code then.

Do you have a way to test the posted code? A class with a main() and definitions for all the variables that the method needs to execute.

Without code that can be compiled and executed there is no way to test the posted code.

BTW The code has too many "magic" numbers. It should use final variables with readable names like:
final int WhiteChecker = 1;
instead of just using a 1
• April 25th, 2013, 04:29 PM
Extremist252
Re: Fast Help please
public int connected(Point test){
ArrayList<Point> connection=new ArrayList<Point>();/ used to check if i visited these points before or not
Point upLeft=new Point(test.getX()-1,test.getY()-1);
Point up=new Point(test.getX(),test.getY()-1);
Point upRight=new Point(test.getX()+1,test.getY()-1);
Point left=new Point(test.getX()-1,test.getY());
Point right=new Point(test.getX()+1,test.getY());
Point downLeft=new Point(test.getX()-1,test.getY()+1);
Point down=new Point(test.getX(),test.getY()+1);
Point downRight=new Point(test.getX()+1,test.getY()+1);
/ the 8 possible positions

/checking if i visited these points before and if i did the make the points equal to 0 and make their color =0 so that they don't pass by the coing conditions
for(int i=0;i<connection.size();i++){
if (connection.get(i).getX()==upLeft.getX() && connection.get(i).getY()==upLeft.getY()){
upLeft=new Point();
this.getBoard()[upLeft.getY()][upLeft.getX()]=0;
}
if (connection.get(i).getX()==up.getX() && connection.get(i).getY()==up.getY()){
up=new Point();
this.getBoard()[up.getY()][up.getX()]=0;
}
if (connection.get(i).getX()==upRight.getX() && connection.get(i).getY()==upRight.getY()){
upRight=new Point();
this.getBoard()[upRight.getY()][upRight.getX()]=0;
}
if (connection.get(i).getX()==left.getX() && connection.get(i).getY()==left.getY()){
left=new Point();
this.getBoard()[left.getY()][left.getX()]=0;
}
if (connection.get(i).getX()==right.getX() && connection.get(i).getY()==right.getY()){
right=new Point();
this.getBoard()[right.getY()][right.getX()]=0;
}
if (connection.get(i).getX()==downLeft.getX() && connection.get(i).getY()==downLeft.getY()){
downLeft=new Point();
this.getBoard()[downLeft.getY()][downLeft.getX()]=0;
}
if (connection.get(i).getX()==down.getX() && connection.get(i).getY()==down.getY()){
down=new Point();
this.getBoard()[down.getY()][down.getX()]=0;
}
if (connection.get(i).getX()==downRight.getX() && connection.get(i).getY()==downRight.getY()){
downRight=new Point();
this.getBoard()[downRight.getY()][downRight.getX()]=0;
}

}
/the board i'm using is int[8][8] and 1 stands for white and 2 stands for black

/whitecount and blackount are variables that count the number of connections so that i'll compare them later with the number of checkers,and if they are greater or equal than there is a winner in this case

if(upLeft.getX()>=0 && upLeft.getY()>=0 ){
if(this.getColor(upLeft)==this.getColor(test)&& this.getColor(test)!=0){
if(this.getColor(test)==1){
whiteCount++;
connected(upLeft);
}
if(this.getColor(test)==2){
blackCount++;
connected(upLeft);
}
}
}
if (up.getX()>=0 && up.getY()>=0 && up.getX()<8 && up.getY()<8){
if(this.getColor(up)==this.getColor(test)&& this.getColor(test)!=0){
if(this.getColor(test)==1){
whiteCount++;
connected(up);
}
if(this.getColor(test)==2){
blackCount++;
connected(up);
}
}
}
if (upRight.getX()>=0 && upRight.getY()>=0 && upRight.getX()<8 && upRight.getY()<8){
if(this.getColor(upRight)==this.getColor(test)&& this.getColor(test)!=0){
if(this.getColor(test)==1){
whiteCount++;
connected(upRight);
}
if(this.getColor(test)==2){
blackCount++;
connected(upRight);
}
}
}
if (left.getX()>=0 && left.getY()>=0 && left.getX()<8 && left.getY()<8){
if(this.getColor(left)==this.getColor(test)&& this.getColor(test)!=0){
if(this.getColor(test)==1){
whiteCount++;
connected(left);
}
if(this.getColor(test)==2){
blackCount++;
connected(left);
}
}
}
if (right.getX()>=0 && up.getY()>=0 && right.getX()<8 && up.getY()<8){
if(this.getColor(right)==this.getColor(test)&& this.getColor(test)!=0){
if(this.getColor(test)==1){
whiteCount++;
connected(right);
}
if(this.getColor(test)==2){
blackCount++;
connected(right);
}
}
}
if (downLeft.getX()>=0 && up.getY()>=0 && downLeft.getX()<8 && downLeft.getY()<8){
if(this.getColor(downLeft)==this.getColor(test)&& this.getColor(test)!=0){
if(this.getColor(test)==1){
whiteCount++;
connected(downLeft);
}
if(this.getColor(test)==2){
blackCount++;
connected(downLeft);
}
}
}
if (down.getX()>=0 && down.getY()>=0 && down.getX()<8 && down.getY()<8){
if(this.getColor(down)==this.getColor(test)&& this.getColor(test)!=0){
if(this.getColor(test)==1){
whiteCount++;
connected(down);
}
if(this.getColor(test)==2){
blackCount++;
connected(down);
}
}
}
if (downRight.getX()>=0 && up.getY()>=0 && up.getX()<8 && up.getY()<8){
if(this.getColor(downRight)==this.getColor(test)&& this.getColor(test)!=0){
if(this.getColor(test)==1){
whiteCount++;
connected(downRight);
}
if(this.getColor(test)==2){
blackCount++;
connected(downRight);
}
}
}
else if(this.getColor(test)==1){
return whiteCount;
}
else if(this.getColor(test)==2){
return blackCount;
}
return -1;
}
/the problem is that i get a stack overflow so theres an infinite recursion but i don't know where

--- Update ---

this is another code i came up with but the output is always -1
public int getWinner() {
boolean flagWhite=false;
boolean flagBlack=false;
ArrayList<Point> white=new ArrayList<Point>();
ArrayList<Point> black=new ArrayList<Point>();

for(int j=0;j<8;j++){
for(int i=0;i<8;i++){
if (this.getColor(new Point(i,j))==1){
white.add(new Point(i,j));// filling the white checkers array
}
if (this.getColor(new Point(i,j))==2){
black.add(new Point(i,j));//filling the black checkers array
}
} }
/sorting the white checker array
for(int j=0;j<white.size()-1;j++){
for(int i=0;i<white.size()-1-j;i++){

if (white.get(i).isGreater(white.get(i+1))){
Point temp=white.get(i);
white.set(i, white.get(i+1));
white.set(i+1, temp); }
}
}
//sorting the black checkers array
for(int j=0;j<black.size()-1;j++){
for(int i=0;i<black.size()-1-j;i++){

if (black.get(i).isGreater(black.get(i+1))){
Point temp=black.get(i);
black.set(i, black.get(i+1));
black.set(i+1, temp); }
}
}

for(int i=0;i<white.size();i++){
if(white.get(i+1)==null){
break;
}
//checking if the resutl point exists 1 step away from the current point horizontaly vertically or diagonaly
Point result =white.get(i).substract(white.get(i+1));
if (result.getX()==0 || result.getX()==1 && result.getY()==1 || result.getY()==0) {
flagWhite=true; }

else{
flagWhite=false;
break; }
}
for(int i=0;i<black.size();i++){
if(black.get(i+1)==null){
break;
}

Point result = black.get(i).substract(black.get(i+1));
if (result.getX()==0 || result.getX()==1 && result.getY()==1 ||result.getY()==0){
flagBlack=true; }
else{
flagBlack=false;
break;
}
}

if(flagWhite==true)
return 1;

else if(flagBlack==true)
return 2;

else return -1;

}
• April 25th, 2013, 04:30 PM
Norm
Re: Fast Help please
The stack trace should give you some source line numbers. Copy some lines of the trace and paste it here.

Do you have a way to test the posted code? A class with a main() and definitions for all the variables that the method needs to execute.

Please edit your post and wrap your code with code tags:
[code=java]
[/code]
to get highlighting and preserve formatting.
• April 25th, 2013, 04:33 PM
Extremist252
Re: Fast Help please
it always point to the recursive call (connected(up)) and all the other points..
what do u think about the iterative implementation? forget about the recursive one
• April 25th, 2013, 04:35 PM
Norm
Re: Fast Help please
I'm waiting for the code to be wrapped in code tags so its readable.

Do you have a way to test the posted code? A class with a main() and definitions for all the variables that the method needs to execute.
• April 25th, 2013, 04:39 PM
Extremist252
Re: Fast Help please
this is another code i came up with but the output is always -1
public int getWinner() {
boolean flagWhite=false;//checks if the white pieces are connected
boolean flagBlack=false;//checks if the black pieces are connected
ArrayList<Point> white=new ArrayList<Point>();//arrayList that contain the position of all white checkers
ArrayList<Point> black=new ArrayList<Point>();// arraylist that contain the position of all blackcheckers

for(int j=0;j<8;j++){
for(int i=0;i<8;i++){
if (this.getColor(new Point(i,j))==1){
white.add(new Point(i,j));// filling the white checkers array
}
if (this.getColor(new Point(i,j))==2){
black.add(new Point(i,j));//filling the black checkers array
}
} }
/sorting the white checkers array
for(int j=0;j<white.size()-1;j++){
for(int i=0;i<white.size()-1-j;i++){

//isGreater is a method in my point class that checks if the point is greater than the point on which the method was invoked

if (white.get(i).isGreater(white.get(i+1))){
Point temp=white.get(i);
white.set(i, white.get(i+1));
white.set(i+1, temp); }
}
}
//sorting the black checkers array
for(int j=0;j<black.size()-1;j++){
for(int i=0;i<black.size()-1-j;i++){

if (black.get(i).isGreater(black.get(i+1))){
Point temp=black.get(i);
black.set(i, black.get(i+1));
black.set(i+1, temp); }
}
}

for(int i=0;i<white.size();i++){
if(white.get(i+1)==null){
break;
}
//checking if the resuly point exists 1 step away from the current point horizontaly vertically or diagonaly

Point result =white.get(i).substract(white.get(i+1));
if (result.getX()==0 || result.getX()==1 && result.getY()==1 || result.getY()==0) {

flagWhite=true; }

else{
flagWhite=false;
break; }
}
for(int i=0;i<black.size();i++){
if(black.get(i+1)==null){
break;
}

Point result = black.get(i).substract(black.get(i+1));
if (result.getX()==0 || result.getX()==1 && result.getY()==1 ||result.getY()==0){
flagBlack=true; }
else{
flagBlack=false;
break;
}
}

if(flagWhite==true)// if flag==true that means that all white pieces are connected
return 1;

else if(flagBlack==true) if flag==true that means that all black pieces are connected
return 2;

else return -1;

}
• April 25th, 2013, 04:41 PM
Norm
Re: Fast Help please
I'm waiting for the code to be wrapped in code tags so its readable.

Do you have a way to test the posted code? A class with a main() and definitions for all the variables that the method needs to execute.
• April 25th, 2013, 04:46 PM
Extremist252
Re: Fast Help please
I'm afraid not,i don't have a main method to do so and i won't be able to make it because i'm 15 mins away from handing the project over :S

--- Update ---

i commented on what i'm doing beside my code like for example:
black.add(new Point(i,j));//filling the black checkers array
• April 25th, 2013, 04:51 PM
Norm
Re: Fast Help please
Without formatting, the code is not easy to read.
Without a main() method to use for testing, there isn't much I can do in 15 minutes.

Good luck
• April 25th, 2013, 05:01 PM
Extremist252
Re: Fast Help please
Okay Thanks anyway for your help!