Results 1 to 10 of 10
  1. #1
    Davey1043 is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Default I need help incrementing values through a 2d array, please.

    I want to iterate through my array such that when the row index (i) is 0 the second row index (j) is 1. Then increment both values by 1 and repeat. Finally I need to increment the row index to 2, but I need the second row index to = 0 when i = 2. Any advice would be appreciated, thank you in advance.

    This is the part I'm working on to illustrate the problem.
    for(i = 0; i<3; i++)
    j = (i+1)>2 ? 0 : i+1;
    if(grid[i][2].compareTo(token) == 0 && grid[j][2].compareTo(token) == 0)

    I've also tried something like this:
    for(i = 0; i<3; i++)
    j = i+1;
    if(j>2)
    j = 0;

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,549
    Rep Power
    23

    Default Re: I need help incrementing values through a 2d array, please.

    Could you list samples of valid index pairs:
    i in col 1, j in column 2. Here is one that you gave. What would the rest look like?
    0, 1
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Davey1043 is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Default Re: I need help incrementing values through a 2d array, please.

    Sure, the pairings of (i,j) would be as follows: (0,1) then (1,2) then (2,0). Those are the only index pairs i need to iterate through. I feel like this is really simple, but I'm overlooking it.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,549
    Rep Power
    23

    Default Re: I need help incrementing values through a 2d array, please.

    If there are only those three, why iterate?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Davey1043 is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Default Re: I need help incrementing values through a 2d array, please.

    Quote Originally Posted by Norm View Post
    If there are only those three, why iterate?
    1. I need to make changes based on i and j.
    2. I need to make similar comparisons elsewhere in the code. For example if(grid[2][j].compareTo(token) == 0 && grid[2][i].compareTo(token) == 0) with the same index pairs.
    3. In all i need to make roughly 24 comparisons and make changes based on i and j. I could write if statements for each comparison, but then I'd have to fill in the body of each if statement, with a lot of repetition.
    4. It's gotten personal. haha Even if I don't use this technique, I won't stop until I figure out how to do it.

    I'm open to any suggestions that you think might be easier or less expensive. The project itself is to develop a unique TicTacToe AI algorithm from scratch, I don't know if that helps.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,549
    Rep Power
    23

    Default Re: I need help incrementing values through a 2d array, please.

    I'd hard code the three values and forget about loops.

    Post the code (in code tags) that you think you need a loop for.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Davey1043 is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Default Re: I need help incrementing values through a 2d array, please.

    I don't need a loop it's just much simpler that way. It wouldn't only be 3 values I'm hard coding though, it would be about 24. I would have to do something like this:
    Java Code:
    if(grid[1][0].compareTo(token) == 0 && grid[2][0].compareTo(token) == 0)
        {
          i = 0;
          j = 0;
          return compMove;
        }
    for every possible winning play. Which is an option, but clearly not the best. Here's the specific segment I'm working on.
    Java Code:
    private int[] compAi(String letter)
      {
        String token = letter;
        int i = 0;
        int j = 0;
        int[] compMove = {i,j};
        if(grid[1][1].compareTo(token) == 0)
          for(i = 0; i < 3; i++)
            for (j = 0; j < 3; j++)
              if((grid[1][1].compareTo(token) == 0 && grid[i][j].compareTo(token) == 0) && (i!=1 && j!=1))
              {
                if(i==0)
                  i = 2;
                else if (i==2)
                  i = 0; 
                if(j==0)
                  j = 2;
                else if(j==2)
                  j = 0;
                return compMove;
              }
        for(i = 0; i<3; i++)
          if( (i+1) == 3)
              j = 0;
          else
              j = (i+1);
          if(grid[i][2].compareTo(token) == 0 && grid[j][2].compareTo(token) == 0)
          {
                if(i==0)
                  i = 1;
                else if (i==1)
                  i = 2; 
                else
                  i = 0;
                return compMove;
          }
    The first part is a working method that checks if there is a win involving the center after which it will return the indices of the comp's next move. There are other comparisons in the method, but the second one is the type that's giving me trouble. I'd post more code, but I don't want to give too much away.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,549
    Rep Power
    23

    Default Re: I need help incrementing values through a 2d array, please.

    Your code would be easier to read if you used {} with the for loops. The code can work as you have posted it, but it is fragile and easy to break if changes are made to it. Always use {} and you'll not have future problems.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,549
    Rep Power
    23

    Default Re: I need help incrementing values through a 2d array, please.

    If you want a loop try this. Put the values in an array:
    int[][] indexes = {{0,1},{1,2},{(2,0}};
    then use: indexes[i][0] and indexes[i][1]
    the loop: i=0 to 2
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    Davey1043 is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Default Re: I need help incrementing values through a 2d array, please.

    Thanks for your help. =) Just trying to refine it now.

Similar Threads

  1. Values in Array
    By jazob in forum New To Java
    Replies: 2
    Last Post: 12-10-2011, 01:29 AM
  2. Replies: 14
    Last Post: 01-22-2011, 02:36 PM
  3. incrementing array elements
    By porchrat in forum New To Java
    Replies: 2
    Last Post: 10-06-2010, 02:37 PM
  4. incrementing elements in an array
    By JavaNubb in forum New To Java
    Replies: 7
    Last Post: 08-20-2010, 03:15 AM
  5. Same values in an array
    By hawaiifiver in forum New To Java
    Replies: 3
    Last Post: 02-24-2009, 08:33 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
  •