Java 使用数组检测gomoku游戏中的胜利

Java 使用数组检测gomoku游戏中的胜利,java,Java,我做了一个简单的五子棋游戏,玩家通过水平或垂直放置五个棋子来获胜。问题是,我的程序只在玩家从棋盘最外层的行和列开始时检测到胜利。我觉得我的hasHorizontalWin和hasVerticalWin方法是有缺陷的,因为我在某个地方缺少大括号,或者我正在使用的count变量需要以某种方式进行修改。我对编程还是相当陌生的,这个特别的程序是由我的教授指定的。谢谢你们能给我的任何帮助。这是我的密码: import java.util.*; class Board { // I never a

我做了一个简单的五子棋游戏,玩家通过水平或垂直放置五个棋子来获胜。问题是,我的程序只在玩家从棋盘最外层的行和列开始时检测到胜利。我觉得我的hasHorizontalWin和hasVerticalWin方法是有缺陷的,因为我在某个地方缺少大括号,或者我正在使用的count变量需要以某种方式进行修改。我对编程还是相当陌生的,这个特别的程序是由我的教授指定的。谢谢你们能给我的任何帮助。这是我的密码:

import java.util.*;

class Board {
    // I never actually use the EMPTY variable in my code
final int EMPTY = 0;
final int BLACK = 2;
final int WHITE = 1;
int player;
int[][] area = new int[19][19];

/*
 * This is method allows the player to play their piece
 */
void place(int row, int column, int player) {
    area[row][column] = 0;
    if (player == WHITE) {
        area[row][column] = WHITE;
    } else if (player == BLACK) {
        area[row][column] = BLACK;
    } else
        area[row][column] = EMPTY;
}

/*
 * The hasHorizontalWin method uses a count variable to count the pieces in
 * a row, but it appears to only count from row 18 and column 18
 */
boolean hasHorizontalWin(int player) {
    int count = 0;
    boolean isWinner = false;
    for (int row = 0; row < area.length; row++) {
        for (int column = 0; column < area[row].length; column++) {
            if (area[row][column] == player)
                count++;

            else
                count = 0;
        }
    }
    if (count == 5)
        isWinner = true;

    else
        isWinner = false;

    return isWinner;

}

/*
 * The hasVerticalWin method works like the hasHorizontalWin method except
 * it checks the columns, also starting from row 18 and column 18
 */
boolean hasVerticalWin(int player) {
    int count = 0;
    boolean isWinner = false;

    for (int row = 0; row < area.length; row++) {
        for (int column = 0; column < area[row].length; column++) {
            if (area[column][row] == player)
                count++;
            else
                count = 0;
        }
    }
    if (count == 5)
        isWinner = true;
    else
        isWinner = false;

    return isWinner;

}

// My hasWin method uses an if statement to check the two winning conditions
boolean hasWin(int player) {
    boolean isWinner = false;

    if (hasVerticalWin(player) == true || hasHorizontalWin(player) == true) {
        System.out.println("You Win!");
        isWinner = true;
    }

    else
        isWinner = false;

    return isWinner;
}

// My play method
void play() {
    // My scanner object to read input from player
    Scanner sc = new Scanner(System.in);
    int turn = 0;
    while (hasWin(player) == false) {
        turn++;

        System.out.println(this);
        /*
         * By creating a variable called turn then using a modular operator
         * I am able to switch between players
         */
        if (turn % 2 != 0) {
            player = WHITE;
            System.out.println("White to play");

        } else {
            player = BLACK;
            System.out.println("Black to play");

        }

        System.out.print("Row: ");
        int row = sc.nextInt();
        System.out.print("Column: ");
        int column = sc.nextInt();
        // Allows player to place piece
        place(row, column, player);

    }
}

// My toString() method to print out the board
public String toString() {
    String result = "";
    for (int row = 0; row < area.length; row++) {
        result += "\n";
        for (int column = 0; column < area[row].length; column++) {

            if (area[row][column] == WHITE) {
                result += "+o";
            }

            else if (area[row][column] == BLACK) {

                result += "+x";
            }

            else
                result += "+-";
        }
    }

    return result;
}
}

public class GoMoku {

public static void main(String[] args) {
    Board game = new Board();
    game.play();
    System.out.print(game);

}

}

要使代码正常工作,需要进行多个更改:

a在hasWinplayer==false时更改循环以执行。。。而hasWinplayer==false

b更改hasHorizontalWin和hasVerticalWin的逻辑,如下所示:

int count = 0;
boolean isWinner = false;
for (int row = 0; row < area.length; row++) {
    for (int column = 0; column < area[row].length; column++) {
        if (area[row][column] == player && count < 5)
            count++;
        else if(count == 5){
            isWinner = true;
            break;
        }
        else{
            isWinner = false;
            count = 0;
        }
    }

    if(isWinner)
        break;

}


return isWinner;

对不起,我花了这么长时间才回复你。这些if-else语句是否可以在while循环中工作,或者是否需要do-while循环?我知道do-while循环在检查条件之前确保了至少一次循环的迭代。您建议的更改肯定让我走上了正确的方向。新的问题是,除了连接到第18行和第18列之外,该程序现在可以在其他任何地方检测到wins。我感谢您的帮助。通过将else语句向上移动到我的第一个if语句,并将else if语句转换为单独的if语句,我可以使程序正常工作。do while循环是必要的,因为您的第一个HASVIN检测到第0个玩家的win:很高兴知道您的代码在纠正其他问题后工作正常。