안녕하십니까.이 사이트에 게시 한 것은 이번이 처음이므로 최대한 명확하게하려고 시도합니다.여러 배열의 개별 값 비교
난 무작위 배 배치를 생성하기 위해 javaScript를 사용하여 전함 게임을 만들고 있습니다. 나는 게임 보드에 두 개의 배열 (X & Y)를 생성 임의의 위치를 생성하는 클래스를 생성하고 다음과 다른 선박 객체 배열 ....
// 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();
어떻게 각각의 값을 확인할 수 있습니다 오버랩을 검사 할 우주선 배열? 여러 방법을 시도했지만 요소를 선택하거나 쉽게 참조 할 수있는 방법을 찾지 못했습니다. 어떤 아이디어?
나는 이것에 익숙하지 않고, 지금까지 codeacademy.com에서 모든 것을 배웠고 이해할 수 있도록 몇 가지 질문을했습니다. 당신은 빈 그리드를 만들었고 랜덤 한 지점이 생성되면 함수는 이미 "! == 0"을 취했는지 확인하고 while 문은 true가 될 때까지 실행합니다. 너무 굉장합니다! 나는 아직 JS의 css와 html에 익숙하지 않아 프린트 보드 기능은 아무런 의미가 없지만 아프게된다. –
맞습니다. 나는 당신의 기존 코드를 무작위로 시작점과 방향을 선택하기 위해 사용했다가 그 격자 중 어떤 것이 "격자"에 아직 점재되어 있는지 확인했다. 이 검사에 실패하면 새로운 임의 배치로 다시 시작해야합니다. 'printBoard()'는 디버그를 목적으로 한 것이므로 걱정하지 마십시오. 행운을 빕니다! – mchail