Help me find a mistake, pliz. I am trying to write console tic-tac-toe, but one of the cycles does not think to end, although it seems like all the conditions for this are fulfilled. The cycle itself:

do { Scanner coord = new Scanner(System.in); System.out.println(player1Name + ", введите номер ячейки по горизонтали (от 1 до 3):"); x = coord.nextInt() - 1; System.out.println(player1Name + ", введите номер ячейки по вертикали (от 1 до 3):"); y = coord.nextInt() - 1; if (field[x][y] == '_'){ field[x][y] = 'X'; } else { System.out.println("Это поле уже занято, попробуйте снова."); } for (x = 0; x < SIZE; x++) { for (y = 0; y < SIZE; y++) { System.out.print(field[x][y] + " "); } System.out.println(); } System.out.println(); } while (field[x][y] == 'X'); 

The problem is somewhere here while (field[x][y] == 'X'); as I understand it, but what the problem is, it does not reach me. The 3x3 field, if the player enters the coordinates x = 1, y = 2, then the field will be displayed

 _ Х _ _ _ _ _ _ _ 

Those. as it should, but the test at the end of the cycle does not pass and again begins to twist again.

Fully error:

 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at Main.main(Main.java:80) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) 
  • The problem with the endless loop is clear. It is not clear at what step ArrayIndexOutOfBoundsException occurs - default locale
  • at Main.main(Main.java:80) i.e. just where while (field[x][y] == 'X'); - tryhard

2 answers 2

About why the cycle does not end:

 do { //...пропуск if (field[x][y] == '_'){ field[x][y] = 'X'; //Здесь мы заполнили поле x,y } else { System.out.println("Это поле уже занято, попробуйте снова."); } //после этого field[x][y] обязательно Х //...пропуск } while (field[x][y] == 'X'); //а здесь мы проверяем, что х,у заполнено: ответ всегда да 

Those. verification of the selected field is made after its filling.

You can exit the loop in different ways:

  • make a break of if when selecting an empty field;
  • create a flag variable and track whether an empty field has been selected or not;
  • change the structure of the code: in the loop only to check the possibility of filling, and fill out.

Why ArrayIndexOutOfBoundsException occurs: the variables x and y are reused to print the array, after executing the cycles, their values ​​change:

 for (x = 0; x < SIZE; x++) { for (y = 0; y < SIZE; y++) { System.out.print(field[x][y] + " "); } //здесь у = SIZE System.out.println(); } //здесь x=SIZE 

You can fix this if you use local variables for loops.

 for (int i = 0; i < SIZE; i++) { 

or even a separate method for printing.

  • Thanks, it finally came to why the cycle did not end. The printout here is just to check that the field is filled correctly. - tryhard

At the end of the cycles:

 for (x = 0; x < SIZE; x++) { for (y = 0; y < SIZE; y++) { System.out.print(field[x][y] + " "); } System.out.println(); } 

it turns out that x=y=SIZE;

Next, you try to extract field[x][y] , where x, y already lie outside the bounds of the array and naturally get rude in response.

Reconsider the logic.