안녕하세요, 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, 편집하기 전까지 외부 준비에 너무 만족했습니다! –