2014-10-18 10 views
4

안녕하세요, TicTacToe 프로젝트를 시도했지만 오류가 발생했습니다. 내 오류는 특히 대각선 승리 솔루션을 확인하는 것과 관련이 있습니다.TicTacToe 크기 조정 가능 보드, WIN 메서드 오류

내가 필요한 것은 : 그것은 결국 전체 배열을 스캔 할 때까지 아래 또는 크기에 대각선으로 스캔 할 수 있도록 는 다음을 증가, 대각선으로 배열 아래 루프 중첩 루프를 만듭니다.

에게 내가 무슨 짓을 : 내가 행의 끝에 다음 확인 때까지 카운터가 인라인 동일했다 경우 행을 통해 루프가에서 (카운터에 금액을 추가 할 것이라고 루프 중첩 만들려고 이기는 데 필요한 행). 나는 그것이 행과 열에 효과가 있다고 생각한다.

문제 : 하지만 대각선, 나는 경계 예외 밖으로 배열을 얻을 내 또는 B내가에 추가되어 있기 때문에 생각 될 수있는 게임 판 [3] [4] 3x3 게임 게시판을 말할 때.

시도해보십시오. 나는 당신이 볼 수있는 해결책을 시도했습니다. j와 함께 이상하게 배치되었습니다. 그래서 나는 단지 j에 가야하고 배열 제한을 지나치지 않을 것입니다.

이 논리가 작동하는지 궁금합니다. 당신이 Array Index Out Of Bounds Exception을 받아야 코드가 특히 루프 추가 된 지저분한 경우

죄송합니다 지금까지 내가 코드에서 보는 바와 같이 J에게

/* 
* Method winner will determine if the symbol (X or O) wins 
* 
* @param symbol will be either X or O 
* @return will return true if the symbol has won from any of the methods 
*/ 
public boolean winner(char symbol) { 

    int counter = 0; 

    /* Scan from ROWS for any symbols inline to win */ 

    for (int i = 0; i < gameBoard.length; i++) { // loop through the rows 
    for (int j = 0; j < gameBoard.length; j++) { // Loop through the columns 
     if (gameBoard[i][j] == symbol) { 
     counter++; 
     } 
     if (gameBoard[i][j] != symbol) { // If the next one in the row is not equal then reset counter to 0 
     counter = 0; 
     } 
     if (counter == inline) { // Counter will only equal inline if there is amount of inliine in a row 
     return true; 
     } 
    } 
    } 

    /* Scan and search for winning conditions in COLUMNS */ 
    for (int i = 0; i < gameBoard.length; i++) { // loop through the rows 
    for (int j = 0; j < gameBoard.length; j++) { // Loop through the columns 
     if (gameBoard[j][i] == symbol) { 
     counter++; 
     } 
     if (gameBoard[j][i] != symbol) { // Reset counter to 0 if not equal to symbol 
     counter = 0; 
     } 
     if (counter == inline) { // If counter reached amount of inline then it must have had amount of inline in a row to win 
     return true; 
     } 
    } 
    } 

    /* Scan for RIGHT DIAGONALS for winning conditions */ 

    // a shifts the position of diagonal to the right by one 
    // after diagonally looping through the board 
    for (int a = 0; a < gameBoard.length; a++) { 

    // i loops diagonally through the board 
    for (int j = gameBoard.length; j < 0; j--) { 
     for (int i = 0; i < j; i++) { 
     if (gameBoard[i][i + a] == symbol) { 
      counter++; 
     } 
     if (gameBoard[i][i + a] != symbol) { 
      counter = 0; 
     } 
     if (counter == inline) { 
      return true; 
     } 
     } 
    } 
    } 

    // b shifts the position of the diagonal down by one 
    for (int b = 1; b < gameBoard.length; b++) { 

    for (int j = gameBoard.length - 1; j < 0; j--) 
    // i loops diagonally through the board 
     for (int i = 0; i < j; i++) { 
     if (gameBoard[i + b][i] == symbol) { 
     counter++; 
     } 
     if (gameBoard[i + b][i] != symbol) { 
     counter = 0; 
     } 
     if (counter == inline) { 
     return true; 
     } 
    } 
    } 

    /* Scan for LEFT DIAGONALS for winning conditions */ 

    // a shifts the position of diagonal to the left by one 
    for (int a = gameBoard.length; a >= 0; a--) { 
    for (int j = gameBoard.length; j < 0; j--) { 
     // i loops diagonally through the board 
     for (int i = 0; i < j; i++) { 
     if (gameBoard[i][a - i] == symbol) { 
      counter++; 
     } 
     if (gameBoard[i][a - i] != symbol) { 
      counter = 0; 
     } 
     if (counter == inline) { 
      return true; 
     } 
     } 
    } 
    } 

    // b shifts the position of the diagonal down by one 
    for (int b = 0; b < gameBoard.length; b++) { 
    for (int j = gameBoard.length - 1; j < 0; j--) { 
     // i loops diagonally in the left direction of through the board 
     for (int i = 0; i < j; i++) { 
     if (gameBoard[i + b][gameBoard.length - i] == symbol) { 
      counter++; 
     } 
     if (gameBoard[i + b][gameBoard.length - i] != symbol) { 
      counter = 0; 
     } 
     if (counter == inline) { 
      return true; 
     } 
     } 
    } 
    } 


    return false; // If it reaches here then no one has won yet and the game is ongoing 

} 
+1

+1, 편집하기 전까지 외부 준비에 너무 만족했습니다! –

답변

1

가 포함되어 있습니다. 나는 당신이 고전적인 tic-tac-toe를 구현하려고 노력한다고 가정하기 때문에 우리는 3x3 매트릭스를 다루고 있습니다. 다음은 게임 보드 인덱싱 방법이다

[0.0] [1.0] [2.0]

[0.1] [1.1] [2.1]

[0.2] [1.2] [2.2]

그래서이 오른쪽 대각선 루프에서 일어나는 것입니다 : -> 2
int j이 감소 -
int a 단위로 0> 0
int i 단위를 0 -> 2


그래서 루프는 다음과 같이 진행한다 :
[0.0 + 0] -> 난 ++ [1.1 + 0] -> 난 [2.2 + 0] 을 j-- ++
[0.0 + 0] -> 난 [1.1 + 0]
[0.0 + 0] j-- ++ ++
[0.0 + 1] -> 난 ++ [1.1 + 1] -> 난 [2.2 + 1] + + j-- < - - 여기 어레이에서 나왔어.


는 또한 주요 대각선을 통해 확인한 후, 당신은 전혀 대각선 아니다 [0.0] [1.1]를 ​​통해 가서 당신은 이미 행에 대해 루프에서 이런 짓을. 이전에 루프에서 이미 수행 했으므로 아래쪽 대각선을 통과하는 것도 필요하지 않습니다 ([0.1] [1.2]). 따라서 [0.0] [1.1] [2.2] 당신을 위해 일할 것입니다.


나는 이것이 승리 조건을 확인하는 비효율적 인 방법이라고 생각합니다. 발견 된 요소의 위치를 ​​저장하는 것만으로 3 개의 루프를 제거 할 수 있습니다. 의견의 형식을 가져올 수 없습니다 죄송합니다

+0

귀하의 대답 덕분에이 문제는 정말로 분명합니다. 나는 그 이상한 루프를 추가하기 전에 문제를 겪었고, 내가 얼마나 효과적인지 알지 못했다. 방금 배열을 지나가는 스캔을 수정하기 위해 for 루프를 추가했습니다. for 루프를 사용하는 나의 목표는 각 대각선 행을 스캔하는 것이 었습니다. 그러나 tic tac 발가락은 커스텀 보드 크기와 커스텀 인라인 (이기기 위해 연속적으로 필요한 양)에서 실행됩니다. 이 방법이 커스텀 크기의 tic tac toe 보드에서 작동하는지 궁금합니다. –

0

그래서 내가 여기

/* Scan for RIGHT DIAGONALS for winning conditions */ 

int j = gameBoard.length 

    for (int a = 0; a < gameBoard.length; a++) { 

// i loops diagonally through the board 
    for (int i = 0; i < j; i++) { 
    if (gameBoard[i][i + a] == symbol) { 
     counter++; 
    } 
    if (gameBoard[i][i + a] != symbol) { 
     counter = 0; 
    } 
    if (counter == inline) { 
     return true; 
    } 
    } j--; // Incrementing after the i for loop. 
} 

output: 
a:0 i:0 j:3 
[0.0+0] --> i++ [1.1+0] --> i++ [2.2+0] /*end for loop. i:2 j:3 a: 0 */ 
j-- a++ 
[0.0+1] --> i++ [1.1+1] /*end for loop. i:1 j:2 a: 1*/ 
j-- a++ 
[0.0+2] /* end for loop. i:0 j: 1 a:2 */ 

무엇을 가지고 게시합니다 및 대각선을 확인하면서 배열 범위에서 유지됩니다. 그래서 나는 생각보다 큰 규모로 일할 수 있다고 생각합니다.