How to write the isGameOver () function to check 5 identical characters in a row? Those. the game must end when a player scores 5 zeros or 5 crosses horizontally, vertically, or diagonally.
package game; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; public class Class1 { static int [] canvas = {0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,}; //начальный массив public static void main(String[] args) throws FileNotFoundException{ boolean b; boolean isCurrentX = false; do { isCurrentX = !isCurrentX; drawCanvas(); System.out.println("Ходит " + (isCurrentX ? "X" : "O")); int n = getNumber(); canvas[n] = isCurrentX ? 1 : 2; b = !isGameOver(n); if (isDraw()){ System.out.println("Draw"); return; } } while (b); drawCanvas(); System.out.println(); System.out.println("Победитель: " + (isCurrentX ? "X" : "O") + "!"); } static int getNumber(){ BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); while (true){ try { int n = Integer.parseInt(reader.readLine()); if (n >= 0 && n < canvas.length && canvas[n]==0){ return n; } System.out.println("Выберите свободное поле и введите его порядковый номер"); } catch (NumberFormatException e) { System.out.println("Пожалуйста, введите порядковый номер клетки"); } catch (IOException e) { } } } static boolean isGameOver(int n){ // /* | | | | | | | | | | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | _____|_____|_____|_____|_____|_____|_____|_____|_____|_____| | | | | | | | | | | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | _____|_____|_____|_____|_____|_____|_____|_____|_____|_____| | | | | | | | | | | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | _____|_____|_____|_____|_____|_____|_____|_____|_____|_____| | | | | | | | | | | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | _____|_____|_____|_____|_____|_____|_____|_____|_____|_____| | | | | | | | | | | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | _____|_____|_____|_____|_____|_____|_____|_____|_____|_____| | | | | | | | | | | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | _____|_____|_____|_____|_____|_____|_____|_____|_____|_____| | | | | | | | | | | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | _____|_____|_____|_____|_____|_____|_____|_____|_____|_____| | | | | | | | | | | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | _____|_____|_____|_____|_____|_____|_____|_____|_____|_____| | | | | | | | | | | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | _____|_____|_____|_____|_____|_____|_____|_____|_____|_____| | | | | | | | | | | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | | | | | | | | | | | */ return false; } static void drawCanvas(){ System.out.println(" | | | | | | | | | |"); for (int i = 0; i < canvas.length; i++) { if (i!=0){ System.out.print("|"); if (i%10==0) { System.out.println(); System.out.println("_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|"); System.out.println(" | | | | | | | | | |"); } } if (canvas[i]==0) { if(i<10) { System.out.print(" " + i + " "); } else { System.out.print(" " + i + " "); } } // int f=0, g=0; if (canvas[i]==1) { System.out.print(" X "); } if (canvas[i]==2) { System.out.print(" O "); } } System.out.print("|"); System.out.println(); System.out.println(" | | | | | | | | | |"); } public static boolean isDraw() { for (int n : canvas) if (n==0) return false; return true; } }
static int[] canvas = new int[100];and all, all zeros - Flippy