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 4 of 4

Thread: java Loop program to place distinct value in a row, column and diagonal

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default java Loop program to place distinct value in a row, column and diagonal

    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:
    1. there is at the most one X in any box
    2. no two X’s are on the same row
    3. no two X’s are on the same column
    4. no two X’s are on the same diagonal
    Write a Java application program that takes a brute-force approach using random
    number generation to find one such placement and display it on the screen.
    Basically, the program will do the following until it finds a solution:
    1. set n = 1
    2. For n =1 to 8
    a. check if there are any other boxes where an X can be placed without violating any
    of the constraints that we have specified above. if no, go to step 1
    b. choose a random box to place the nth X
    c. find out if placing an X in that box violates any of the constraints that we have
    specified above.
    If no, place the nth X in that box, increment n by 1.

    Below is the code, that i have tried so far...Please help me to finish this program as soon as possible...Please Help me...It's very urgent

    import java.util.Random;
     
    public class Assignment1_HondalCarlos {
    static int res;
     
    public static void main(String[] args)
    {
     
    char[][] board = new char[8][8];
    Random randomGenerator = new Random();
    int randomRow =0;
    	int randomColumn=0;
    for(int a=0;a<8;a++)
    {
    	for(int b=0;b<8;b++)
    	{
    		board[a][b]='-';
     
    	}
     
    }
    for(int a=0;a<8;a++)
    {
    	for(int b=0;b<8;b++)
    	{
    		System.out.print(board[a][b] +"  ");
    	}
    	System.out.println("  ");
    }
    for(int n = 0; n < 8; n++)
    {
    	 randomRow = randomGenerator.nextInt(8);
    	 randomColumn = randomGenerator.nextInt(8);
    	boolean test = false;
     
    testconstraints(board, randomRow, randomColumn);
    	if(res==0)
    	{
    		board[randomRow][randomColumn] = 'X';
     
    	}
     
     
    }
     
    for(int j=0;j<8;j++)
    {
    	for(int k=0;k<8;k++)
    	{
     
    		System.out.print(board[j][k] +"  ");
    	}
    	System.out.println("  ");
    }
     
    }
     
    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;
    }
    }


  2. #2
    Junior Member
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java Loop program to place distinct value in a row, column and diagonal

    Hello Experts,

    Please reply me...I really need to solve this program soon...
    Please somebody guide to finish this program as soon as possible..

    Please experts help me..It is very very urgent..

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: java Loop program to place distinct value in a row, column and diagonal

    What is your problem exactly? What does work? What doesn't work?

    You need to take things one step at a time.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: java Loop program to place distinct value in a row, column and diagonal

    loop program

    Duplicate post
    Improving the world one idiot at a time!

Similar Threads

  1. Which is the best place to get java tutorial online??
    By texsas12 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 2nd, 2012, 07:45 PM
  2. Get out of loop to finish program?
    By Doofy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 17th, 2011, 08:34 AM
  3. Java Program Loop Possibly needed
    By mikec2500 in forum Loops & Control Statements
    Replies: 1
    Last Post: January 25th, 2011, 01:35 AM
  4. Print Distinct Numbers help
    By Fordy252 in forum Collections and Generics
    Replies: 0
    Last Post: November 16th, 2010, 12:10 AM
  5. Best way to learn java for beginners
    By JavaPF in forum The Cafe
    Replies: 0
    Last Post: May 8th, 2008, 04:37 AM

Tags for this Thread