Javascript - why am I getting duplicates?

This is another in my continuing saga to create a new Battleship game which could be included as part of a distro's minimal game set.

I have tried 2 approaches, the first using the "some" method, and the second using a loop to "compare array elements directly" with newly created element to avoid adding a duplicate set of coordinates.

My browser Console is reporting my coded messaging as follows:

Targeting Pattern:  Random About Target
Grid Coordinates:  [ 20 , 10 ]
Salvo Coordinates:
21,12,
21,11,
17,8,
19,7,
21,12,  <<== duplicate
20,12,
20,7,
18,12,
21,8,
22,11   <<== duplicate

May I invite those who are JavaScript adepts to review my question on StackOverflow and offer some insights, preferrably here ... or on StackOverflow if you prefer.

Thank you in advance.

Provided your code @ stackoverflow, you do compare arrays. Please note that target is array actually. Hopefully, Comparing Arrays in JavaScript – How to Compare 2 Arrays in JS may help.

3 Likes

Thank you very much, Eugene! You gave me the lead that got me that final solution. I had to dig deep in myself to fully understand the proper handling of the JavaScript coding to identify the proper solution. The key was the method used to specifically compare each coordinate element individually for the two sets of coordinates. It is apparently not the most compact form for what is being done, but it is what most clearly coded for someone to review for learning to emulate, which is my whole intent with my "little" project. :slight_smile:

What finally worked is as follows:

function firePattern_onTargRand( col, row ){
    var firePattern = 'Random About Target' ;
    var target = [] ;
    var targets = [] ;
    var munitionsCost = 10 ;
    var alreadyTargeted = false ;
    var enumerateTargets = "" ;

    for( i=1 ; i <= munitionsCost ; i++ ){
        do {
            target = [ 
                col + getRandomInteger( 0, 6 ) -3 ,
                row + getRandomInteger( 0, 6 ) -3
            ] ;
            alreadyTargetted = false ;
            for( let j = 0 ; j < targets.length ; j++ ){
                if(
                    targets[j][0] === target[0] &&
                    targets[j][1] === target[1]
                ) {
                    alreadyTargetted = true ;
                    console.log( "duplicate flagged at " + `${j}` + " vs " + `${i}` ) ;
                } ;
            } ; 
        } while( alreadyTargetted === true ) ;

        targets.push( target ) ;
        enumerateTargets = enumerateTargets + "\n[" + target + "]" ;
        } ;

    console.log( "Targetting Pattern:  " + `${firePattern}` + "\nGrid Coordinates:  [" + ` ${col} , ${row} ` + "]\nSalvo Coordinates:\n" + enumerateTargets ) ;
    return targets ;
} ;

Here is the log from a test run with multiple calls to assess the function's output quality:

duplicate flagged at 2 vs 5    (discarded)
duplicate flagged at 5 vs 8    (discarded) 
duplicate flagged at 2 vs 9    (discarded)
duplicate flagged at 3 vs 9    (discarded)
Targetting Pattern:  Random About Target
Grid Coordinates:  [ 20 , 10 ]
Salvo Coordinates:

[20,12]
[17,12]
[20,7]
[19,11]
[20,9]
[19,8]
[19,12]
[22,8]
[22,10]
[22,7] 



duplicate flagged at 0 vs 5      (discarded)
duplicate flagged at 6 vs 10    (discarded)
Targetting Pattern:  Random About Target
Grid Coordinates:  [ 20 , 10 ]
Salvo Coordinates:

[21,10]
[17,11]
[19,9]
[21,12]
[17,12]
[20,7]
[18,9]
[20,11]
[22,10]
[17,10]


As an aside, on the StackOverflow posting, I added an observation regarding the frequency of duplicates being generated for candidate coordinates being somewhat excessive for the short runs being performed. Not sure why that would be happening.

3 Likes