0

I am attempting to make a program that allows two player checkers. The teacher said it must use a 2d array but when it runs it says the array is out of bounds.

int[][] pieceStatus[3][7];

it is a four by eight because half of the squares on the chessboard (in this case black) are not used.

    int j=0;
    for (int i = 0; i<=7; i++) 
    {
        if(i<3)
        {
            pieceStatus[j][i]=1;
        }
        if(i>2&&i<5)
        {
            pieceStatus[j][i]=0;
        }
        if(i>4)
        {
            pieceStatus[j][i]=2;
        }
        if(i==7&&j<=3)
        {
            i=0;
            j++;
        }
    }

Zero means that there is no piece there.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Hobson
  • 11
  • 2
  • Don't use [magic numbers](http://stackoverflow.com/questions/47882/what-is-a-magic-number-and-why-is-it-bad) for this sort of thing. Arrays have a length property that will help you prevent running into this type of error. – Hovercraft Full Of Eels Nov 04 '16 at 01:56
  • The array should be 4×8, not 3×7. Array *indices* start at zero, and when you loop you'll get indices from 0-3 or 0-7. The *dimensions* should not be minus-one. – John Kugelman Nov 04 '16 at 01:57
  • You say its 4x8 but the question shows 3x7. – J Reid Nov 04 '16 at 01:58
  • Read the error message. It doesn't say what it says in your title. – user207421 Nov 04 '16 at 03:53

0 Answers0