GameLogic, x in a row game

前端 未结 2 1702
耶瑟儿~
耶瑟儿~ 2021-01-27 07:20

I\'m making a game and I need to make a method which checks if the specified cell is part of a horizontal consecutive sequence of cells containing the same character. The sequen

2条回答
  •  野性不改
    2021-01-27 07:57

    You can simply search for both sides using two loops (one per side) and check if the sum of consecutive cells is indeed l. Something along the lines of:

    public static boolean checkPositionRow(char[][] a, int row, int col, int l) {
        int counter = 1; //starting from 1, because for a[row][col] itself
        char charAtPosition = a[row][col];
       //expand to the right as much as possible
        for (int i = col+1; i < a[row].length && a[row][i] == charAtPosition; i++) counter++;
       //expand to the left as much as possible
        for (int i = col-1; i >= 0 && a[row][i] == charAtPosition; i--) counter++;
        return counter >= l;
    }
    

提交回复
热议问题