-1
\$\begingroup\$

I am working on a board game that requires me to color the bottom left corner of the board with random number of rows and columns a different color from the other blocks. Given an m x n grid, how do I calculate the index of the bottom left block? On a 4x4 block, the index is 12, on a 3x3 block the index 6, on a 2x2 block the index is 2, on a 3x4 block the index is 8, and on a 4x3 block the index is 9. Thank you in advance for the help!

I tried the following but it colors the top left box instead of the bottom left:

for(int r = 0; r < numRow; r++){
              for(int c=0; c < numCol; c++){
                  if(r==numRow-1 && c==0){
                      buttons[c].setBackground(new Color(250, 0, 0));
                      buttons[c].setEnabled(false);
                  }
              }
          }
\$\endgroup\$
2

1 Answer 1

1
\$\begingroup\$

The numbers in the leftmost column are all multiples of your column count. Taking your 4x3 case as an example:

0  1  2
3  4  5
6  7  8
9 10 11

The first row starts with zero, and each row after that is numCol more than the previous. So the leftmost number in the nth row is just (n - 1) times your column count:

int bottomLeft = numCol * (numRow - 1);

Draw a few tables of different sizes to convince yourself that this always matches the bottom-left number.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .