0

to test some things in javascript I am building a small minesweeper.

The following code is the initialization of the two-dimension array P.field. Afterwards a number of random fields are getting filled with an x, symbolizing that there's a mine on this field.

P.field = new Array(num);
for (var i = 0; i < P.field.length; i++)
    P.field[i] = new Array(num);

$.each(P.field, function(index, key) {
    $.each(key, function(i, k) {
        P.field[index][i] = '-';                
    });
});

var arr = [];
while (arr.length < 10) {
    var found = false;
    var randomnumber = Math.ceil(Math.random()*(num*num-1));
    for (var i = 0; i < arr.length; i++)
    if (arr[i] == randomnumber) { found = true; break; }
    if (!found) arr[arr.length] = randomnumber;
}

for (var i = 0; i < arr.length; i++) {
    P.field[ Math.floor(arr[i]/num) ][ Math.floor(arr[i]-Math.floor(arr[i]/num)*num)-1 ] = 'x';
}

However, in my algorithm for counting the mines in surrounding fields, I get the console error TypeError: P.field[(r+1)] is undefined. Every field (except of those from the last row) returns this error, which is something I can't quite understand.

P.field[rows][columns] has a length of 10 per dimension in my tests ([10][10]). When I try to get the value of P.field[9][0] to P.field[9][9] there's nothing wrong. However when I adress any smaller row, this exception kicks in (P.field[0 + 1][0], P.field[3 + 1][6], and what so ever)...

I hope someone can tell me why.

edit

More code:

onReady: function() {
    $('#sweeper table').on('click', 'td', function(e) {
        var row = $(this).parent().attr('class'); // Class represents the index of the array
        var column = $(this).attr('class'); // Class represents the index of the array

        P.openField(row, column, $(this));
    });
},

openField: function(r, c, e) {
    if ( P.field[r][c] == 'x' ) {
        e.addClass('mine');
    } else {
        e.html( P.surroundingMineCount(r, c) );
        e.addClass('opened');
    }
},

surroundingMineCount: function(r, c) {
    var count = 0;
    if ( P.field[r][c-1] == 'x' ) count++;
    if ( P.field[r-1][c-1] == 'x' ) count++;
    if ( P.field[r][c+1] == 'x' ) count++;
    if ( P.field[r-1][c] == 'x' ) count++;
    if ( P.field[r-1][c+1] == 'x' ) count++;
    if ( P.field[r+1][c] == 'x' ) count++;  
    if ( P.field[r+1][c-1] == 'x' ) count++;
    return count;
},

Right now I have no validation if the r+1 or r-1 is actually a valid index for that array (I had one in, but removed it for testing). However that can't really be the error, because I even get errors in the middle of the table.

5
  • better P.field = [num]; Commented Dec 19, 2013 at 13:14
  • 4
    Those do fundamentally different things @PraveenJeganathan, new Array(num) will create an array with num entries, [num] will make an array with one entry that has the value of num Commented Dec 19, 2013 at 13:17
  • you use P.field for initialization and p.field for reading, Javascript is case-sensitive language Commented Dec 19, 2013 at 13:26
  • I just mentioned that in the comment on Dolondro's answer. I didn't pay attention to whether the P's in the inline code blocks are upper or lower case. I corrected it. Commented Dec 19, 2013 at 13:29
  • I'd suggest to log r, c (maybe also typeof r), r-1 and c-1 to console in surroundingMineCount() before using them. Then you can see, if these variables have expected values. Commented Dec 19, 2013 at 15:07

1 Answer 1

1

Looking at the code you've provided versus the errors you've had thrown, I'm skeptical about your suspected cause as the code used does indeed generate the correct set of arrays.

I suspect it may be a slightly simpler issue, the example generation code you've provided uses:

P.field = new Array(num);

which has P as a capital, whereas the error that you've had thrown uses it as lowercase:

TypeError: p.field[(r+1)] is undefined

Are you sure you haven't accidentally just used the incorrect case when testing?

Sign up to request clarification or add additional context in comments.

3 Comments

I am sorry, I didn't pay attention to the cases of the error. I didn't just copy and paste it, I wrote it down here and used the lowercase p instead of uppercase. Inside my javascript every of those P's is uppercase.
In which case I think we probably need more code, what you've got there should hypothetically work, so the problem is elsewhere.
I added some to my question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.