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

Thread: Problem with loop theory

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Problem with loop theory

    Hi, I have a project for University which involves making a basic version of the game Futoshiki. This game is a lot like Sudoku except you can only use the numbers 1 to 5 and there are arithmic operators between the boxes.

    I have already completed most of the program, the problem I am having is the print puzzle method which should print the whole puzzle in character format.

    So far, the whole program works and it does print the puzzle. My problem is that I need to also print the arithmic operators and I am kind of confused how to do this without a mass amount of loops which I don't think I'm supposed to do.

    public class FutoshikiPuzzle
    {
            int[][] grid = new int[5][5];
            String[][] rowConstraints = new String[5][4];
            String[][] columnConstraints = new String[5][4];
            Random generator = new Random();
     
        public FutoshikiPuzzle()
        {    
            for(int i = 0; i < 5; i++)
                for(int j = 0; j < 4; j++)
                    rowConstraints[ i ][ j ] = " ";
     
            for(int i = 0; i < 5; i++)
                for(int j = 0; j < 4; j++)
                    columnConstraints[ i ][ j ] = " ";
        }
     
        public void setSquare()
        {
            int num = generator.nextInt(5) + 1;
            int i = generator.nextInt(5);
            int j = generator.nextInt(5);
     
            grid[i][j] = num;
        }
     
        public void setRowConstraint()
        {
            int i = generator.nextInt(5);
            int j = generator.nextInt(4);
            String[] chars = new String[]{">","<"};
            int rndChar = generator.nextInt(chars.length);
            String constraint = chars[rndChar];
     
            rowConstraints[i][j] = constraint;
        }
     
        public void setColumnConstraint()
        {
            int i = generator.nextInt(5);
            int j = generator.nextInt(4);
            String[] chars = new String[]{"^","v"};
            int rndChar = generator.nextInt(chars.length);
            String constraint = chars[rndChar];
     
            columnConstraints[i][j] = constraint;
        }
     
        public void fillPuzzle()
        {
            setSquare();
            setRowConstraint();
            setColumnConstraint();
        }
     
        public void printPuzzle()
        {
            int count = 1;
            for(int i = 0; i < grid.length; i++)
                for(int j = 0; j < grid.length; j++) //
     
                    if (count < 5 ){
                    System.out.print(" | " + grid[i][j] + " | " );
                    count++;
                    }
                   else {
                    System.out.println(" | " + grid[i][j] + " | ");
                    count = 1;
                }
        }
    }


  2. #2
    Junior Member
    Join Date
    Oct 2017
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with loop theory

    I don't know what you mean witth "arithmetic operators". I think they're relational operators between the boxes.

    Below is a code that puts the relational operators between the columns. The code that does the same between the rows is missing.

    public void printPuzzle() {
            // ---------------------------------------------------------------
            // print the grid
            // ---------------------------------------------------------------
            for (int i = 0; i < grid.length; i++) {
                    // -----------------------------------------------------------
                    // print a row
                    // -----------------------------------------------------------
     
                    // -----------------------------------------------------------
                    // lines above the boxes
                    // -----------------------------------------------------------
                        System.out.println(" +---+   +---+   +---+   +---+   +---+ ");
     
                    // -----------------------------------------------------------
                    // print each box in the row
                    // -----------------------------------------------------------
                        for (int j = 0; j < grid.length; j++) {
                                // ----------------------------------------------
                                // get row constrain
                                // ----------------------------------------------
                                    String rowConstraint = " ";
     
                                    if (j < grid.length - 1) {
                                        rowConstraint = rowConstraints[ i ][ j ];
                                    }
     
                                // ----------------------------------------------
                                // print a box an it's row constraint
                                // ----------------------------------------------
                                    System.out.print(" | " + grid[i][j] + " | " + rowConstraint  );
                        }
     
                         System.out.println();
                    // -----------------------------------------------------------
     
                    // -----------------------------------------------------------
                    // lines below the boxes
                    // -----------------------------------------------------------
                        System.out.println(" +---+   +---+   +---+   +---+   +---+ ");
     
                    // -----------------------------------------------------------
                    // an empty row to separate boxes vertically
                    // -----------------------------------------------------------
                        System.out.println();
     
            }
    }
    Last edited by German M; October 27th, 2017 at 09:00 PM.

Similar Threads

  1. advanced java networking help/theory
    By wolfgar in forum Java Theory & Questions
    Replies: 2
    Last Post: February 7th, 2010, 07:02 PM
  2. Theory behind 2d Game making?
    By DarrenReeder in forum Java Theory & Questions
    Replies: 3
    Last Post: January 28th, 2010, 02:54 AM
  3. Little Loop Problem
    By chronoz13 in forum Loops & Control Statements
    Replies: 1
    Last Post: October 17th, 2009, 05:40 AM
  4. [SOLVED] Array loop problem which returns the difference between the value with fixed value
    By uplink600 in forum Loops & Control Statements
    Replies: 5
    Last Post: May 15th, 2009, 05:31 AM
  5. [SOLVED] Java for loop problem and out put is not coming
    By thewonderdude in forum Loops & Control Statements
    Replies: 9
    Last Post: March 15th, 2009, 03:31 PM