2014-06-04 3 views
-3

tic tac toe 게임을위한 간단한 AI 디자인에 도움이 필요합니다. 나는 그의 차례에 다음 사용 가능한 슬롯에 2를 놓을 컴퓨터가 필요합니다. 그것은 초보자를위한 일식 수업이지만, 교수는 병가를 가지고 질문에 답하는 것을 멈췄습니다! 어떤 도움이라도 대단히 감사하겠습니다! 나는 ComputerInput [] [] 끝에있는 메서드로 문제가 있습니다.기본 AI 수행 방법

package lab15; 

public class WinnerCheck { 

    public static int PL_ONE = 1; 
    public static int PL_TWO = 2; 
    static Console console = new Console(); 
    static final int PAUSE  = 500; 

    public static int[][] b = { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } }; 


    public static void main(String[] args) { 
     boolean done = false; 

     while (!done) { 
      printBoard(b); 
      playerOneInput(); 
      pause(PAUSE); 
      detectWinner(b); 
      console.clear(); 
     } 
    } 

    public static void pause(int ms) { 
     try { 
      Thread.sleep(ms); 
     } catch (Exception ex) { 

     } 
    } 


    public static void placePiece(int[][] b, int r, int c, int player) { 
     b[r][c] = player; 
    } 

    public static boolean isWinner(int[][] b, int player) { 
     // check for a horizontal winner 
     for (int i = 0; i < b.length; i++) { 
      int count = 0; 
      for (int j = 0; j < b[i].length; j++) { 
       if (b[i][j] == player 
        || b[j][i] == player) { 
        count++; 
       } 
      } 
      if (count == b[i].length) 
       return true; 
     } 
     return false; 
    } 

    public static int detectWinner(int[][] b) { 
     if (isWinner(b, PL_ONE)) { 
      return 1; 
     } else if (isWinner(b, PL_TWO)) { 
      return 2; 
     } else { 
      return 0; 
     } 
    } 

    public static void printBoard(int[][] b) { 
     System.out.println("------"); 
     for (int[] row : b) { 
      for (int element : row) { 
       System.out.print(element + " "); 
      } 
      System.out.println(""); 
     } 
     System.out.println("------"); 
     System.out.println("winner: " + detectWinner(b)); 
    } 


    public static void playerOneInput() { 
     switch (console.getKey()) { 
     case '1': 
      placePiece (b, 0, 0, 1); 
      break; 
     case '2': 
      placePiece (b, 0, 1, 1); 
      break; 
     case '3': 
      placePiece (b, 0, 2, 1); 
      break; 
     case '4': 
      placePiece (b, 1, 0, 1); 
      break; 
     case '5': 
      placePiece (b, 1, 1, 1); 
      break; 
     case '6': 
      placePiece (b, 1, 2, 1); 
      break; 
     case '7': 
      placePiece (b, 2, 0, 1); 
      break; 
    case '8': 
      placePiece (b, 2, 1, 1); 
      break; 
    case '9': 
      placePiece (b, 2, 2, 1); 
      break; 
     } 
    } 

    public static int computerInput(int[][]b) { 
     int a[] = {-1, -1}; 
     for (int i = 0; i < b.length; i++) { 
      for (int j = 0; j < b[i].length; j++) { 
       if (b(0)) { 

       } 
      } 

     } 
    } 

} 

답변

0

는 그냥 [난, j]가 0과 동일 b를 그 다음에 2를 넣고이 코드를 사용하여 그것을 시도

+0

을 반환하는 경우 있는지 확인해야하지만 나는 오류를 얻을 수 A는 [0] = A [1] \t 공용 static computerInput INT (INT [] [] b) { \t INT \ [η] = {-1}; \t \t 가 (; 나는 b.length를을 <; I = 0 int로 난 ++) {위한 \t \t (INT의 J = 0; J user3709108