I have a tic-tac-toe game. I need to check if there are 3 identical characters in a row. The problem for me was that the choice of field size is possible in the game. Yes, and even what happened for the 3x3 field seems to me somehow not beautiful.
private static boolean checkWinner(String[][] desc) { return checkWinnerHorizontal(desc) || checkWinnerVertical(desc) || checkWinnerDiagonals(desc); } private static boolean checkWinnerHorizontal(String[][] desc) { return desc[0][0].equals(desc[1][0]) && desc[0][0].equals(desc[2][0]) || desc[0][1].equals(desc[1][1]) && desc[0][1].equals(desc[2][1]) || desc[0][2].equals(desc[1][2]) && desc[0][2].equals(desc[2][2]); } private static boolean checkWinnerVertical(String[][] desc) { return desc[0][0].equals(desc[0][1]) && desc[0][0].equals(desc[0][2]) || desc[1][0].equals(desc[1][1]) && desc[1][0].equals(desc[1][2]) || desc[2][0].equals(desc[2][1]) && desc[2][0].equals(desc[2][2]); } private static boolean checkWinnerDiagonals(String[][] desc) { return desc[0][0].equals(desc[1][1]) && desc[0][0].equals(desc[2][2]) || desc[2][0].equals(desc[1][1]) && desc[2][0].equals(desc[0][2]); } Somehow this is all clumsy, so please help shortly and beautifully, not with the code as an idea as it can be done, given that we do not know the size of the field, it’s just known that this is a square. Somehow cycles need ...
trueorfalsein general (especially considering that a row of empty cells also givestrue)? Is it not necessary to know exactly who won, and that not empty cells “won”? - Regent