简体   繁体   中英

Comparing individual values of multiple arrays

Hello this is the first time I,ve posted on this site so I will attempt to be as clear as possible.

I'm creating a Battleship game using javaScript to generate random ship placement. I created two arrays for the game board (X & Y), created a class to generate random placement, and a object array for the different ships as follows....

// game board
var xAxis = ["a","b","c","d","e","f","g","h","i","j"];
var yAxis = [1,2,3,4,5,6,7,8,9,10];

// Location Class decleration //

function Location (size,name){
//// Parameters ///////////////////////////////////////////
    this.size = size;
    this.name = name;    
//// Class functions //////////////////////////////////////

// sets the ship to a random position
this.shipPosition = function(){
    var orientation = Math.random();        
    var area = [];
    if (orientation < 0.5){
        x = Math.floor(Math.random()*(xAxis.length-size+1));
        y = Math.floor(Math.random()*(yAxis.length));
        for (i=x; i < (size + x); i++){
            area.push([xAxis[i],yAxis[y]]);
        }
    } else {
        x = Math.floor(Math.random()*(xAxis.length));
        y = Math.floor(Math.random()*(yAxis.length-size+1));
        for (i=y; i < (size + y); i++){
            area.push([xAxis[x],yAxis[i]]);
        }
    }
    return area;
};
///// Stored variables /////////////////////////////////////////

//stores position
    var positionArray = this.shipPosition();
//assigns value to each element in array for checking 
//and makes the private value public. ex. a = ['h',1]      
    var a = positionArray[0];
    var b = positionArray[1];
    var c = positionArray[2];
    var d = positionArray[3];
    var e = positionArray[4];
    this.getA = a;    
    this.getB = b;        
    this.getC = c;
    this.getD = d;
    this.getE = e;

// locator console logging function

    this.whereIs = function(){
        console.log(this.name);
        console.log("ship size is "+ size);    
        console.log("-- X - Y --");
        console.log(a);
        console.log(b);
        if (size > 2){console.log(c);}
        if (size > 3){console.log(d);}
        if (size > 4){console.log(e);}
        console.log("    ***********************************     ");
    };
}
//ship array
var computerShip = new Array();
computerShip[0] = new Location(2, "SHIP 1");
computerShip[1] = new Location(3, "SHIP 2");
computerShip[2] = new Location(3, "SHIP 3");
computerShip[3] = new Location(4, "SHIP 4");
computerShip[4] = new Location(5, "SHIP 5");

//used to call whereIs method for each ship

var genetateComputerShip = function(){
    for(i=0; i<computerShip.length; i++){          
        computerShip[i].whereIs();        
    }
};    
genetateComputerShip();

How can I check each value of the ship arrays to check for overlap? I've tried multiple ways but have had trouble either singling out elements or finding a way to easily cross reference them. Any ideas?

I made some changes to your code in order to maintain a global "board" concept, which is represented as a multi-dimensional array. See the code here:

http://jsfiddle.net/mchail/ytwyS/1/

After hitting "run", check your browser's javascript console, and you should see a printout at the end of the full board with each ship placed. I modified shipPosition (now called setShipPosition ) to check for conflicts on the board before placing the ship.

Nice work, by the way!

UPDATE

Modified the jsfiddle to print the board out to the screen as well. Keep hitting "run" to see more randomly-generated boards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM