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

Thread: Latin square logic

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Latin square logic

    I am trying to test the logic of a particular method included in a project I am working on and so I have created a simple 3 by 3 Latin Square with which to do so. The basics of the program work fine, printing the board, entering numbers, determining when the grid is full and son on. However, the method that evaluates the values actually contained within the squares of the grid. To do so, I drew up a Boolean method which contains a series of nested loops to test the numbers against each other. The desired result would be to have the Boolean variable "checkValue" read true when the game has been won - that is when each row contains a unique value from 1 to 3 and likewise the columns. The grid is a mutli-dimensional array named "board" nad x and y are of course the horizontal and verticle coordinates. Here is my code:
    public boolean check() {
    		for (int x = 0; x < n; x++) {
    			for (int y = 0; y < n; y++) {
     
    				// Check row
    				for (int i = 0; i < n; i++) {
    					if (board[x][i] != board[x][y])
    						checkValue = true;
    				}
     
    				// Check column
    				for (int i = 0; i < n; i++) {
    					if (board[i][y] != board[x][y])
    						checkValue = true;
    				}
    			}
    		}
    		return checkValue;
    	}
    Last edited by Deprogrammer; November 20th, 2010 at 07:44 PM.


  2. #2
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    495
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Latin square logic

    You need to return false as soon as a clash is found, and return true after all checks have passed. Pseudocode:
    boolean someMethod() {
       for outer loop
          for inner loop
             if some condition is not met
                return false
             end if
          end for
       end for
       return true
    }
    db

  3. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Latin square logic

    Thanks for your assistance.

Similar Threads

  1. Finding square root
    By Tracy22 in forum The Cafe
    Replies: 1
    Last Post: October 18th, 2010, 07:18 PM
  2. Need Help with Operators/ logic
    By codekiller in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 3rd, 2010, 10:25 AM
  3. Help with Pig Latin Translator...
    By jchan in forum AWT / Java Swing
    Replies: 3
    Last Post: September 28th, 2009, 03:54 PM
  4. Java program Square root
    By Hey in forum Java Theory & Questions
    Replies: 5
    Last Post: August 16th, 2009, 02:14 AM
  5. Pig Latin Translator
    By BMN901 in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: June 17th, 2009, 04:31 AM