Results 1 to 13 of 13

Thread: loop program

  1. #1
    javanovoice is offline Member
    Join Date
    Sep 2011
    Posts
    6
    Rep Power
    0

    Default loop program

    Hi,

    I'm novoice to java. I'm trying a program for place 8 X’s on the board with 64 boxes(8*8 matrix) in such a way that:
    Last edited by javanovoice; 09-09-2011 at 06:20 AM.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: loop program

    Quote Originally Posted by javanovoice View Post
    Java Code:
    public static int testconstraints(char[][] board,int rowCheck, int colCheck)
    {
    System.out.println("Row :" + rowCheck + " Column : " + colCheck);
    
    if(board[rowCheck][0]=='X' || board[rowCheck][1]=='X' ||board[rowCheck][2]=='X' ||board[rowCheck][3]=='X' ||board[rowCheck][4]=='X' ||board[rowCheck][5]=='X' ||board[rowCheck][6]=='X' ||board[rowCheck][7]=='X' )
    {
    if(board[0][colCheck]=='X' || board[1][colCheck]=='X' || board[2][colCheck]=='X' || board[3][colCheck]=='X' || board[4][colCheck]=='X' || board[5][colCheck]=='X' || board[6][colCheck]=='X' || board[7][colCheck]=='X' )
    {
    res= 1;
    }
    }
    else
    {
    res = 0;
    }
    return 0;
    }
    }
    That method basically does this:

    Java Code:
    public static int testconstraints(char[][] board,int rowCheck, int colCheck) {
       // al whole lot of mumbo jumbo and finally:
       return 0;
    }
    That isn't of much use is it? To speed your development up a bit, google for 'eight queens problem' because that's what you're trying to solve.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: loop program

    Can I suggest you learn about indenting your code correctly?
    That's painful to read at the moment.

    At least you've got your naming mostly correct.

  4. #4
    javanovoice is offline Member
    Join Date
    Sep 2011
    Posts
    6
    Rep Power
    0

    Default Re: loop program

    Hi JosAH,

    Thanks for your suggestion. But i couldn't understand that 8 queen program code.

    Please help me to solve my program..

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,560
    Rep Power
    25

    Default Re: loop program

    The queen is the most powerful chess piece. The 8 queens problem is about how to place the queens on the board so none of them threaten another one.

    To solve your problem you need a method that tests if a square is on the same row, column or diagonal of an occupied square.

  6. #6
    javanovoice is offline Member
    Join Date
    Sep 2011
    Posts
    6
    Rep Power
    0

    Default Re: loop program

    Can you please suggest any idea about the method to test the constraints?

    I have been trying to find that trick for last two days..

    Please help me sir...

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,560
    Rep Power
    25

    Default Re: loop program

    Given a square's x,y
    Compute all the squares on the same row (ie row = x)
    Compute all the squares on the same col (ie col = y)
    Compute all the squares on the upperleft-lowerrright diag (take a piece of paper and work it out
    0,0 -> 1,1 -> 2,2 -> ... 7,7
    Compute all the squares on the upperright-lowerleft diag (same idea as above)
    0,7 -> 1,7 -> ... 7,0
    Last edited by Norm; 09-08-2011 at 03:27 PM. Reason: Board is 8x8

  8. #8
    javanovoice is offline Member
    Join Date
    Sep 2011
    Posts
    6
    Rep Power
    0

    Default Re: loop program

    is this code somewhat correct?

    it checks the rows and columns
    if(board[rowCheck][0]=='X' || board[rowCheck][1]=='X' ||board[rowCheck][2]=='X' ||board[rowCheck][3]=='X' ||board[rowCheck][4]=='X' ||board[rowCheck][5]=='X' ||board[rowCheck][6]=='X' ||board[rowCheck][7]=='X' )
    {
    if(board[0][colCheck]=='X' || board[1][colCheck]=='X' || board[2][colCheck]=='X' || board[3][colCheck]=='X' || board[4][colCheck]=='X' || board[5][colCheck]=='X' || board[6][colCheck]=='X' || board[7][colCheck]=='X' )
    {
    sorry, if my question is silly...

    actually, row and column is selected randomly and then checks for the constraints to place 'X' in that place...

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,560
    Rep Power
    25

    Default Re: loop program

    Write a test program to compile and execute it and let the computer tell you if it is correct.
    I might use a loop vs having the long conditions in the if statements

  10. #10
    javanovoice is offline Member
    Join Date
    Sep 2011
    Posts
    6
    Rep Power
    0

    Default Re: loop program

    till now, I didn't get any idea for solving this problem.

    I lost.........

  11. #11
    javanovoice is offline Member
    Join Date
    Sep 2011
    Posts
    6
    Rep Power
    0

    Default Re: loop program

    Hi Norm,

    Can you give the code for this problem?

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,560
    Rep Power
    25

    Default Re: loop program

    Your code will work for the rows and columns, now you need to work out the algorithm for the diagonals.
    I have never written code for this problem so I have nothing to give except some ideas.
    Take a piece of paper and see how to vary the row and column values to go on each diagonal and how to detect when you are at the edge of the board.

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

Similar Threads

  1. How to loop this program??
    By 7auz in forum New To Java
    Replies: 4
    Last Post: 01-01-2011, 03:08 AM
  2. Replies: 15
    Last Post: 11-06-2009, 10:31 AM
  3. Loop Program Help Please?
    By rjones215 in forum New To Java
    Replies: 2
    Last Post: 10-07-2009, 10:50 PM
  4. Trouble with For loop and variables in a program
    By dablyz in forum New To Java
    Replies: 12
    Last Post: 05-06-2008, 04:25 AM
  5. Replies: 2
    Last Post: 05-05-2008, 01:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •