# Thread: I need help incrementing values through a 2d array, please.

1. Member
Join Date
Apr 2012
Posts
8
Rep Power
0

## 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. ## 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

3. Member
Join Date
Apr 2012
Posts
8
Rep Power
0

## 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. ## Re: I need help incrementing values through a 2d array, please.

If there are only those three, why iterate?

5. Member
Join Date
Apr 2012
Posts
8
Rep Power
0

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

Originally Posted by Norm
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. ## 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.

7. Member
Join Date
Apr 2012
Posts
8
Rep Power
0

## 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. ## 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.

9. ## 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

10. Member
Join Date
Apr 2012
Posts
8
Rep Power
0

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

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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•