2014-12-06 4 views
0

그래서 10 진법 테이블처럼 배열하는 2D 배열 코드를 작성하고 있습니다. Xs, Os 및 공백으로 채워집니다. 임계 값이 입력되고 X 또는 O 주변의 색인 백분율도 X 또는 Os가 임계 값보다 큰 경우 스팟은 만족스럽지 않으면 만족하지 않습니다.public static int에 대해 무엇을 반환해야합니까?

그런 다음 빈 자리에 만족하지 않는 X 및 O를 이동하고 전체 보드가 만족되거나 지정된 라운드 수가 초과 될 때까지 반복해야합니다. 나는 모든 것을 썼지 만 moveAllUnsatisfied를 호출하려고 할 때 나는 254를 얻는다 : return 문이 없다.

나는 moveAllUnsatisfied를 호출 할 수 있으며 내 배열을 변경한다고 생각했습니다. 무엇을 반환해야합니까? 아니면 완전히 잘못 되었습니까? 그리고 다시해야합니까? 내가 아마 그냥 "공공 정적 INT randInt"처럼

public class CellSim{ 

public static void main(String[] args){ 

System.out.println("what is the size of your grid?"); 
    char [][] tissue = new char [IO.readInt()][IO.readInt()]; 
System.out.println("How many like agents are needed to be satisfied?"); 
    int threshold = IO.readInt(); 
System.out.println("How many rounds to try and satisfy the board?");  
    int maxRounds = IO.readInt(); 
System.out.println("How often do you want to see the board?"); 
    int frequency = IO.readInt(); 
System.out.println("Percentage of X cells?"); 
    int xCells = IO.readInt(); 
System.out.println("Percentage of blank cells?"); 
    int bCells = IO.readInt(); 
    int roundsDone = 0; 
    assignCellTypes(tissue, bCells, xCells); 
    printTissue(tissue); 
    System.out.println(); 

    boolean boardSat = true; 
    boardSat = boardSatisfied(tissue, threshold); 

    if(boardSat == false){ 
     while(roundsDone <= maxRounds || boardSat == false){ 
      moveAllUnsatisfied(tissue, threshold); 
      roundsDone++; 
       boardSat = boardSatisfied(tissue, threshold); 
       while(roundsDone == frequency || roundsDone == frequency * 2){ 
        System.out.println(); 
        printTissue(tissue); 
        frequency = frequency * 2; 
        } 
     } 
     } 
    if(boardSat == true){ 
     printTissue(tissue);} 

} 


public static void printTissue(char[][] tissue){ 
    for(int row = 0;row < tissue.length;row++){ 
     for(int col = 0;col < tissue[row].length;col++){ 
      System.out.print(tissue[row][col] + "\t"); 
     } 
     System.out.println(); 
    } 
} 


public static void assignCellTypes(char[][] tissue, int percentBlank, int percentX){ 
int n = (tissue.length) * (tissue.length); 
percentBlank = (int) Math.ceil(n * (percentBlank * .01)); 
percentX = (int) Math.ceil((n - percentBlank) * (percentX * .01)); 
int percentO = (int) Math.ceil(n - percentBlank - percentX); 

for(int i = 0; i < percentBlank; i++){ 
while(percentBlank > 0){ 
    int randCell = randInt(0, 9); 
    int randCell2 = randInt(0, 9); 
       if(tissue[randCell][randCell2] == '\u0000'){ 
        tissue[randCell][randCell2] = ' '; 
        break; 
        } 
} 
} 
for(int i = 0; i < percentX; i++){ 
while(percentX > 0){ 
    int randCell = randInt(0, 9); 
    int randCell2 = randInt(0, 9); 
       if(tissue[randCell][randCell2] == '\u0000'){ 
        tissue[randCell][randCell2] = 'X'; 
        break; 
        } 
} 
} 
for(int i = 0; i < percentO; i++){ 
while(percentO > 0){ 
    int randCell = randInt(0, 9); 
    int randCell2 = randInt(0, 9); 
       if(tissue[randCell][randCell2] == '\u0000'){ 
        tissue[randCell][randCell2] = 'O'; 
        break; 
        } 
} 
} 

} 

public static boolean isSatisfied(char[][] tissue, int row, int col, int threshold){ 
    int total = 0; 
    int same = 0; 
    if(tissue[row][col] == 'X'){ 
    total = 0; 
    if(col-1 >= 0 && row+1 < tissue.length && tissue[row + 1][col - 1] == 'X'){ 
     same ++; 
     total ++; 
    }else if(col-1 >= 0 && row+1 < tissue.length && tissue[row + 1][col - 1] == 'O') 
     total ++; 
    if(row+1 < tissue.length && tissue[row + 1][col] == 'X'){ 
     same ++; 
     total ++; 
    }else if(row+1 < tissue.length && tissue[row + 1][col] == 'O') 
     total ++; 
    if(row+1 < tissue.length && col+1 < tissue[row+1].length && tissue[row + 1][col + 1] == 'X'){ 
     same ++; 
     total ++; 
    }else if(row+1 < tissue.length && col+1 < tissue[row+1].length && tissue[row + 1][col + 1] == 'O') 
     total ++; 
    if(col-1 >= 0 && tissue[row][col - 1] == 'X'){ 
     same ++; 
     total ++; 
    }else if(col-1 >= 0 && tissue[row][col - 1] == 'O') 
     total ++; 
    if(col+1 < tissue[row].length && tissue[row][col + 1] == 'X'){ 
     same ++; 
     total ++; 
    }else if(col+1 < tissue[row].length && tissue[row][col + 1] == 'O') 
     total ++; 
    if(row-1 >= 0 && col-1 >= 0 && tissue[row - 1][col - 1] == 'X'){ 
     same ++; 
     total ++; 
    }else if(row-1 >= 0 && col-1 >= 0 && tissue[row - 1][col - 1] == 'O') 
     total ++; 
    if(row-1 >= 0 && tissue[row - 1][col] == 'X'){ 
     same ++; 
     total ++; 
    }else if(row-1 >= 0 && tissue[row - 1][col] == 'O') 
     total ++; 
    if(row-1 >= 0 && col+1 < tissue[row-1].length && tissue[row - 1][col + 1] == 'X'){ 
     same ++; 
     total ++; 
    }else if(row-1 >= 0 && col+1 < tissue[row-1].length && tissue[row - 1][col + 1] == 'O') 
     total ++; 

} 
if(tissue[row][col] == 'O'){ 
total = 0; 
    if(col-1 >= 0 && row+1 < tissue.length && tissue[row + 1][col - 1] == 'O'){ 
     same ++; 
     total ++; 
    }else if(col-1 >= 0 && row+1 < tissue.length && tissue[row + 1][col - 1] == 'X') 
     total ++; 
    if(row+1 < tissue.length && tissue[row + 1][col] == 'O'){ 
     same ++; 
     total ++; 
    }else if(row+1 < tissue.length && tissue[row + 1][col] == 'X') 
     total ++; 
    if(row+1 < tissue.length && col+1 < tissue[row+1].length && tissue[row + 1][col + 1] == 'O'){ 
     same ++; 
     total ++; 
    }else if(row+1 < tissue.length && col+1 < tissue[row+1].length && tissue[row + 1][col + 1] == 'X') 
     total ++; 
    if(col-1 >= 0 && tissue[row][col - 1] == 'O'){ 
     same ++; 
     total ++; 
    }else if(col-1 >= 0 && tissue[row][col - 1] == 'X') 
     total ++; 
    if(col+1 < tissue[row].length && tissue[row][col + 1] == 'O'){ 
     same ++; 
     total ++; 
    }else if(col+1 < tissue[row].length && tissue[row][col + 1] == 'X') 
     total ++; 
    if(row-1 >= 0 && col-1 >= 0 && tissue[row - 1][col - 1] == 'O'){ 
     same ++; 
     total ++; 
    }else if(row-1 >= 0 && col-1 >= 0 && tissue[row - 1][col - 1] == 'X') 
     total ++; 
    if(row-1 >= 0 && tissue[row - 1][col] == 'O'){ 
     same ++; 
     total ++; 
    }else if(row-1 >= 0 && tissue[row - 1][col] == 'X') 
     total ++; 
    if(row-1 >= 0 && col+1 < tissue[row-1].length && tissue[row - 1][col + 1] == 'O'){ 
     same ++; 
     total ++; 
    }else if(row-1 >= 0 && col+1 < tissue[row-1].length && tissue[row - 1][col + 1] == 'X') 
     total ++; 


} 
if(tissue[row][col] == ' '){ 
    return true; 
}if(total == 0){ 
    return false; 
}else if(((same/total) * 100) >= threshold){ 
    return true; 
}else{ return false;} 
}   

public static boolean boardSatisfied(char[][] tissue, int threshold){ 
    boolean isSat = false; 
    boolean bSat = true; 

     for(int row = 0;row < tissue.length;row++){ 
      for(int col = 0;col < tissue[row].length;col++){ 
      if(tissue[row][col] == 'X'){ 
      isSat = isSatisfied(tissue, row, col, threshold); 
       if(isSat == false){ 
       tissue[row][col] = 'U'; 
       bSat = false; 
       } 
      } 
      if(tissue[row][col] == 'O'){ 
      isSat = isSatisfied(tissue, row, col, threshold); 
       if(isSat == false){ 
       tissue[row][col] = 'Q'; 
       bSat = false; 
       } 
      } 
      } 
     } 

     if(bSat == false){ 
     return false; 
     }else{return true;} 
} 


public static int moveAllUnsatisfied(char[][] tissue, int threshold){ 
    for(int row = 0;row < tissue.length;row++){ 
     for(int col = 0;col < tissue[row].length;col++){ 
      if(tissue[row][col] == 'U'){ 
       tissue[row][col]= ' '; 
       int ranCell = randInt(0, 9); 
       int ranCell2 = randInt(0, 9); 
        while(tissue[ranCell][ranCell2] == 'X' || tissue[ranCell][ranCell2] == 'O' || tissue[ranCell][ranCell2] == ' '){ 
        ranCell = randInt(0, 9); 
        ranCell2 = randInt(0, 9); 
         if(tissue[ranCell][ranCell2] == ' '){ 
          tissue[ranCell][ranCell2] = 'X'; 
          break; 
         }    
        } 
      } 
      if(tissue[row][col] == 'Q'){ 
       tissue[row][col]= ' '; 
       int ranCell = randInt(0, 9); 
       int ranCell2 = randInt(0, 9); 
        while(tissue[ranCell][ranCell2] == 'X' || tissue[ranCell][ranCell2] == 'O' || tissue[ranCell][ranCell2] == ' '){ 
        ranCell = randInt(0, 9); 
        ranCell2 = randInt(0, 9); 
         if(tissue[ranCell][ranCell2] == ' '){ 
          tissue[ranCell][ranCell2] = 'X'; 
          break; 
         }    
        } 
      }  

     } 
    } 
} 


public static int randInt(int min, int max){ 

int range = (max - min) + 1;  
return(int)(Math.random() * range) + min; 
} 



} 

답변

1

, 당신은 int를 반환해야 잘 할 수 있다는 것을 알고이 코드의 길이와 추에 대해 사과

. "return 0;"을 추가 할 수 있습니다. 또는 "public static void moveAllUnsatisfied"로 서명을 변경할 수 있습니다.

+0

아 쉽습니다. – Guy2

+0

안녕하세요 @ Guy2이 답변이나 질문에 대한 답변이있는 경우 확인 표시를 클릭하여 수락을 고려해보십시오. 이는 해결책을 찾았으며 응답자와 자신에게 어느 정도의 평판을 제공한다는 것을 더 넓은 커뮤니티에 나타냅니다. 이를 수행 할 의무는 없습니다. – Ranch