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: TicTacToe problem

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default TicTacToe problem

    Hi guys, hopefully this is an easy one for you.

    Given a grid of nxn squares, where each square has an id, the first(top left) square has id 0 (so a 5x5 grid will have ids 0-24) like below:
    grid.png

    I need to generate all diagonal solutions of length Y. So if Y is 3, then some of the solutions will be:
    grid1.png
    and
    grid2.png
    and
    grid3.png

    but obviously NOT
    grid4.png


    Any ideas how these solutions can be generated?

    This is what Iv got so far (dimension = 5, inARow = 3):

    public ArrayList<int[]> getSolutions(int dimension, int inARow) {

    ArrayList<int[]> solutions = new ArrayList<int[]>();

    //create row solutions
    for(int i=0; i<dimension*dimension; i = i+dimension) {
    for(int j=i; j<=i+dimension - inARow; j++){
    int[] row = new int[inARow];
    int counter = 0;
    for(int k=j; k<j+inARow; k++){
    row[counter++] = k;
    }
    solutions.add(row);
    }
    }

    //create column solutions
    for(int i=0;i<dimension;i++){
    for(int j=i; j<(dimension*dimension)-(dimension*inARow)+dimension;j=j+dimension){
    int[] col = new int[inARow];
    int counter = 0;
    for(int k=j;k<j+(dimension*inARow);k=k+dimension){
    col[counter++] = k;
    }
    solutions.add(col);
    }
    }

    //create diagonals
    for(int i=0; i<dimension*dimension; i++){
    for(int j=i; j<i+(dimension * inARow); j = j+dimension+1){
    System.out.println(j);
    }
    }

    return solutions;
    This gives me all the diagonal solutions but also gives me the bad ones like 3,9,15. Im having trouble eliminating those.

    Anti-diagonals are also solutions so 2,6,10 would also be a solution but if I get normal diagonals working I can probably do the same for anti-diagonals.
    Last edited by Kratos; March 13th, 2012 at 01:28 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,298
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: TicTacToe problem

    Recommended reading: How to Program (stuck on homework? READ THIS BEFORE POSTING)

    It's almost impossible to answer "how do I do this" type questions other than to point you to google or the basic tutorials. Break your problem up into smaller pieces, attempt a piece, and ask a specific question when you get stuck.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,152
    Thanks
    48
    Thanked 2,158 Times in 2,131 Posts

    Default Re: TicTacToe problem

    How about a pair of methods: (left & right) that given a square, check if the square in their direction is good and returns the good square number or -1 if its not good.

  4. #4
    Junior Member
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: TicTacToe problem

    Quote Originally Posted by Norm View Post
    How about a pair of methods: (left & right) that given a square, check if the square in their direction is good and returns the good square number or -1 if its not good.
    I think doing that Norm will work but it will change the foundation of my program too much. I think I may have worked it out otherwise, but thanks for your help!

Similar Threads

  1. TicTacToe 2D Array help
    By steel55677 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: July 9th, 2011, 05:01 PM
  2. [SOLVED] TicTacToe program - Space taken?
    By Actinistia in forum Java Theory & Questions
    Replies: 6
    Last Post: May 2nd, 2011, 11:06 PM
  3. TicTacToe
    By Zerro900 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 27th, 2011, 08:29 AM
  4. Tictactoe problem
    By Nostromo in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 26th, 2011, 03:38 PM
  5. i neeeed help in tictactoe in java !!
    By yinky in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 29th, 2009, 10:17 PM