简体   繁体   中英

Checking if certain value repeats 5 consecutive times in array

I'm attempting to write a simple m, n, k- game in Java. The game is set on a 10 by 10 "board", which is graphically represented as a grid with 10 rows and 10 columns. To win the game the player must select 5 cells either horizontally, vertically or diagonally.

I'm having issues with writing an algorithm that checks whether a certain player has won the game. The game "board" is internally represented as a two-dimensional array called matrix with 10 elements for each dimension. Since it is an array of int's, it initially is filled with zeroes. I represent the two players as the number 1 and the number 2. I've created a method called checkIfWinOrDraw which has input parameters of the x coordinate, the y coordinate, and the player who last made a move. This is run each time a cell has been selected. I'm currently at the very basic first step of checking whether there are five consecutive values corresponding to the player's number in the column of the last selected cell. I've written some logic that implements this check, but it does not work, and I find myself unable to figure out why, so I was hoping someone here could help me out. To be clear, I've searched around quite extensively, but I've only managed to find solutions which are quite vaguely related to my problem, and I was unable to derive a solution from them, as I am still quite the amateur at programming in general. So my question is this this: what am I doing wrong and what would be the proper solution to this problem?

The code in question:

public void checkIfWinOrDraw(int x, int y, int player){
            // check columns
            for(int i = 0; i < 9; i++) {
                int counter = 1;
                if (matrix[x][i] == player){
                    if(matrix[x][i] == matrix[x][i+1]){
                        counter++;
                    }
                    if (counter == 5) {
                        if(player == 1) {
                            JOptionPane.showMessageDialog(null, "Game over, RED wins!");
                        } else {
                            JOptionPane.showMessageDialog(null, "Game over, BLUE wins!");
                        }
                    }
                } else {
                    counter = 1;
                }
            }  
}

Note: I did not mention the 2D array in the title because I'm only operating with 1 dimension at this stage. My apologies if the omission was confusing.

您的代码在for循环的每一步都将 counter 变量初始化为 1,因此每次您转到下一列时,它都会将 counter 重置为 1。从for主体中删除int counter = 1

You need to check 4 lines passing through the given point (horizontal, vertical and 2 diagonals). And for each line you have to go in both directions from the point. That gives you 8 consecutive checking loops:

public void checkIfWinOrDraw(int x, int y, int player){
    int line = 0;
    for (int i = y + 1; i < 10 && matrix[x][i] == player; i++, line++);
    for (int i = y; i >= 0 && matrix[x][i] == player; i--, line++);
    if (line == 5) {
        win(player);
        return;
    }
    line = 0;
    for (int i = x + 1; i < 10 && matrix[i][y] == player; i++, line++);
    for (int i = x; i >= 0 && matrix[i][y] == player; i--, line++);
    if (line == 5) {
        win(player);
        return;
    }
    line = 0;
    for (int i = x + 1, j = y + 1; i < 10 && j < 10 && matrix[i][j] == player; i++, j++, line++);
    for (int i = x, j = y; i >= 0 && j >= 0 && matrix[i][j] == player; i--, j--, line++);
    if (line == 5) {
        win(player);
        return;
    }
    line = 0;
    for (int i = x - 1, j = y + 1; i >= 0 && j < 10 && matrix[i][j] == player; i--, j++, line++);
    for (int i = x, j = y; i < 10 && j >= 0 && matrix[i][j] == player; i++, j--, line++);
    if (line == 5) {
        win(player);
    }
}

For convenience, I've put win messages into a separate method:

private static void win(int player) {
    if (player == 1) {
        JOptionPane.showMessageDialog(null, "Game over, RED wins!");
    } else {
        JOptionPane.showMessageDialog(null, "Game over, BLUE wins!");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM