2014-11-30 3 views
0

나는 인덱스를 벗어난 인덱스에 대해 도움을 받았고, 여러분은 훌륭했습니다. 다음 코드에서 다른 {이상한 (나에게) 루핑 오류

을 코드

if(direction == 1){  

및 }의 다음 행을 추가하여 내 전함 게임의 방향 변화를 설명하기 위해 추가. 그것은 끊임없이 반복적으로 반복 작업을 수행 했음에도 불구하고 왜 이런 일이 일어나고 있는지 아이디어를 줄 수 있습니까?

package outlab6; 

import java.util.Scanner; 

public class Battleship { 
private int rows; 
private int cols; 
private Spot spot[][]; 
Scanner input = new Scanner(System.in); 

public Battleship(int rows, int cols){ 
    this.rows = rows; 
    this.cols = cols; 
} 
public void setBoard(){ 
    spot = new Spot[rows][cols]; 
    for(int i = 0; i < rows; i++){ 
     for(int j = 0; j < cols; j++){ 
      spot[i][j] = new Spot(); 
     } 
    } 
    //setup board to be completely empty 
    for(int i = 0; i < rows; i++){ 
     for(int j = 0; j < cols; j++){ 
      spot[i][j].setShip(0); 
     } 
    } 
//  //test code 
//  for(int i = 0; i < rows; i++){ 
//   for(int j = 0; j < cols; j++){ 
//    System.out.print(spot[i][j].getShip()); 
//   } 
//   System.out.println(); 
//  } 
     setShips(); 
} 
public void printBoard(boolean active){ 


} 
public boolean over() { 

    return false; 
} 
public void makeGuess() { 
    input.nextInt(); 

} 
public void printStatistics() { 


} 
public void setShips(){ 
    //this method creates and places the ships 
    //start with carrier and move on down 
    for(int i = 5; i > 1; i--){ 
     int col; 
     int row; 
     boolean valid = false; 
     //set a direction 
     int direction = (int)(Math.random()*2)+1; 
     //System.out.println(direction); 
     //get a valid spot 
     while(!valid){ 
     //generate a location 
     int chosenRow = (int)(Math.random()* rows); 
     int chosenCol = (int)(Math.random()* cols); 
     System.out.println("Row:" + chosenRow); 
     System.out.println("Col:" + chosenCol); 
     //check to see if spot is open 
     if(direction == 1){ 
      //for horizontal ships 
      if(chosenCol + i < cols){ 
      for(int j = 0; j < i; j++){ 
       if(spot[chosenRow][chosenCol + i].getShip() == 0){ 
        valid = true; 
       }else{ 
        valid = false; 
       } 
      } 
     }else{ 
     //go through again 
     } 
     }else{ 

     } 
    } 
} 
} 
} 
+0

자신에게 질문하십시오 : 방향이 첫 번째가 아닌 경우에는 어떻게됩니까? while 루프가 입력 된 시간은 유효 할 것입니까? –

+0

예, 루프 내에서 임의의 방향을 이동할 수 있습니다. 또한 유효한 "false"줄을 "continue"로 변경했습니다. 우주선을 배치하기 위해 모든 공간이 비어 있어야하기 때문입니다. – Phil

답변

1

while 루프가 종료 될 때를 생각해보십시오. 유효한 것이 사실이되는대로 곧 종료 될 것입니다. 그리고 지금 그것이 언제 실현 될지 생각해보십시오. 이는 방향이 1 인 경우에만 발생합니다. 방향이 설정된 위치와 가능한 방향의 방향을 확인하십시오. 그리고 거기에 짜증나는 고리가 있습니다.

1

* 2 방향에서 * 2를 제거하면 문제가 없습니다. 지금이 2로 설정되어 (붙여 넣기 시도 System.out.println("Dir:" + direction);

당신이 * 2를 제거하는 경우, 당신은 (다음과 같은 출력을 얻을 인쇄 방향) : 또한

Row:2 
Col:3 
Dir:1 
etc... 

하나 더 팁 : setBoard에서 () 당신은 2 개의 루프를 수행합니다 : 생성자에서 빈을 만들거나 동일한 루프에서 빈을 만들지 않겠습니까?

public void setBoard(){ 
spot = new Spot[rows][cols]; 
for(int i = 0; i < rows; i++){ 
    for(int j = 0; j < cols; j++){ 
     spot[i][j] = new Spot(); 
     spot[i][j].setShip(0); 
    } 
    } 
}