Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Fast Help please

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Fast Help please

    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?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,977
    Thanks
    56
    Thanked 2,359 Times in 2,330 Posts

    Default Re: Fast Help please

    Post your code and ask your questions about the problems you are having.
    You need to define what you mean by DFS, 2 array, connected pieces and base case.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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++;
    connection.add(upLeft);
    connected(upLeft);
    }
    if(this.getColor(test)==2){
    connection.add(upLeft);
    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){
    connection.add(up);
    whiteCount++;
    connected(up);
    }
    if(this.getColor(test)==2){
    connection.add(up);
    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){
    connection.add(upRight);
    whiteCount++;
    connected(upRight);
    }
    if(this.getColor(test)==2){
    connection.add(upRight);
    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){
    connection.add(left);
    whiteCount++;
    connected(left);
    }
    if(this.getColor(test)==2){
    connection.add(left);
    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){
    connection.add(right);
    whiteCount++;
    connected(right);
    }
    if(this.getColor(test)==2){
    connection.add(right);
    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){
    connection.add(downLeft);
    whiteCount++;
    connected(downLeft);
    }
    if(this.getColor(test)==2){
    connection.add(downLeft);
    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){
    connection.add(down);
    whiteCount++;
    connected(down);
    }
    if(this.getColor(test)==2){
    connection.add(down);
    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){
    connection.add(downRight);
    whiteCount++;
    connected(downRight);
    }
    if(this.getColor(test)==2){
    connection.add(downRight);
    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

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,977
    Thanks
    56
    Thanked 2,359 Times in 2,330 Posts

    Default Re: Fast Help please

    Sorry, I forgot to say: Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    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
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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++;
    connection.add(upLeft);
    connected(upLeft);
    }
    if(this.getColor(test)==2){
    connection.add(upLeft);
    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){
    connection.add(up);
    whiteCount++;
    connected(up);
    }
    if(this.getColor(test)==2){
    connection.add(up);
    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){
    connection.add(upRight);
    whiteCount++;
    connected(upRight);
    }
    if(this.getColor(test)==2){
    connection.add(upRight);
    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){
    connection.add(left);
    whiteCount++;
    connected(left);
    }
    if(this.getColor(test)==2){
    connection.add(left);
    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){
    connection.add(right);
    whiteCount++;
    connected(right);
    }
    if(this.getColor(test)==2){
    connection.add(right);
    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){
    connection.add(downLeft);
    whiteCount++;
    connected(downLeft);
    }
    if(this.getColor(test)==2){
    connection.add(downLeft);
    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){
    connection.add(down);
    whiteCount++;
    connected(down);
    }
    if(this.getColor(test)==2){
    connection.add(down);
    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){
    connection.add(downRight);
    whiteCount++;
    connected(downRight);
    }
    if(this.getColor(test)==2){
    connection.add(downRight);
    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;

    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,977
    Thanks
    56
    Thanked 2,359 Times in 2,330 Posts

    Default 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]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,977
    Thanks
    56
    Thanked 2,359 Times in 2,330 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Apr 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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;

    }

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,977
    Thanks
    56
    Thanked 2,359 Times in 2,330 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Apr 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,977
    Thanks
    56
    Thanked 2,359 Times in 2,330 Posts

    Default 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
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Apr 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Fast Help please

    Okay Thanks anyway for your help!

Similar Threads

  1. Need Help Fast - Please Help in Java ArrayList
    By anonb in forum Collections and Generics
    Replies: 6
    Last Post: November 5th, 2012, 05:26 PM
  2. need HELP FAST PLEASE HELP ME
    By britben95 in forum Member Introductions
    Replies: 1
    Last Post: November 14th, 2011, 08:05 PM
  3. need help fast
    By coke32 in forum Loops & Control Statements
    Replies: 6
    Last Post: November 1st, 2011, 12:04 AM
  4. need help fast!!
    By nwollis in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 27th, 2010, 06:12 PM
  5. Code stopping, need help fast.
    By aussiemcgr in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 11th, 2010, 10:00 AM