Java 有人能帮我在我的Gomoku项目中实现双赢吗?

Java 有人能帮我在我的Gomoku项目中实现双赢吗?,java,Java,我已经为一项作业编写了一个程序,我们必须编写一个简单的Gomoku程序。我以为我已经拥有了一切,但当我编译并运行时,即使我只有4个同类,即使它们不是相邻的,也会引发赢家场景。(只有在一种类型中连续有五个…X或O的情况下,才应该引发一场胜利)。我觉得我应该在每次转弯前将计数器重置回0,但我不确定应该在哪里执行此操作。任何提示都将不胜感激 import java.util.Scanner; public class Gomoku1 { public static void main (St

我已经为一项作业编写了一个程序,我们必须编写一个简单的Gomoku程序。我以为我已经拥有了一切,但当我编译并运行时,即使我只有4个同类,即使它们不是相邻的,也会引发赢家场景。(只有在一种类型中连续有五个…X或O的情况下,才应该引发一场胜利)。我觉得我应该在每次转弯前将计数器重置回0,但我不确定应该在哪里执行此操作。任何提示都将不胜感激

import java.util.Scanner;

public class Gomoku1
{
    public static void main (String[] args)
    {
        Scanner input = new Scanner(System.in);

        char[][] map = new char [19][19];

        int row = 0;
        int column = 0;

        //fill game with dots
        for (int i = 0; i < map.length; i++)
        {
            for (int j = 0; j < map[i].length; j++)
            {
                map[i][j] = '.';
            }
        }

        printMap(map);

        char player1Choice = 'X';
        char player2Choice = 'O';

        int [] place;
        while (true)
        {

            System.out.println("Player 1's turn!");
            place = userTurn(map, player1Choice);

            if (isValidMove(map, place[0], place[1]) == false)
            {
                System.out.println("Invalid move! Try again!");
                place = userTurn(map, player1Choice);
            }
            if (isValidMove(map, place[0], place[1])) {
                map[place[0]][place[1]] = player1Choice;
                printMap(map);
            }
            if (isBoardFull(map) == true)
            {
                System.out.println("Board is full. Tied game.");
                break;
            }

            if (hasPlayerWon(map, player1Choice) == true)
            {
                System.out.println("Player 1 Wins!");
                break;
            }

            else
            {

                System.out.println("Player 2's turn!: ");
                place = userTurn(map, player2Choice);

                //System.out.println(isValidMove(map, row, column));

                if (isValidMove(map, place[0], place[1]) == false)
                {
                    System.out.println("Invalid move! Try again!");
                    place = userTurn(map, player2Choice);
                }
                if (isValidMove(map, place[0], place[1])) {
                    map[place[0]][place[1]] = player2Choice;
                    printMap(map);
                }
                if (isBoardFull(map) == true)
                {
                    System.out.println("Board is full. Tied game.");
                    break;
                }
                if (hasPlayerWon(map, player2Choice) == true)
                {
                    System.out.println("Player 2 Wins!");
                    break;
                }
            }


        }
    }

    public static void printMap (char[][] map)
    {
        for (int i = 0; i < map.length; i++)
        {
            for (int j = 0; j < map[i].length; j++)
            {
                System.out.printf("%2c", map[i][j]);
            }
            System.out.println();
        }
    }

    public static int [] userTurn (char[][] map, char playerChoice)
    {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter row: ");
        int row = input.nextInt();

        System.out.print("Enter column: ");
        int column = input.nextInt();

        int place [] = {row, column};
        return place;
    }

    public static boolean isValidMove (char[][] map, int row, int column)
    {
        //System.out.println ("n is valid move");
        if (row < 0 || row > 18 || column < 0 || column > 18 || map[row][column]=='O' || map[row][column]=='X')
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    public static boolean isBoardFull (char[][] map)
    {
        int openSpots = 0;
        for (int i = 0; i < map.length; i++)
        {
            for (int j = 0; j < map.length; j++)
            {
                if (!(map[i][j]=='.'))
                    openSpots++;
            }
        }
        if (openSpots == 361)
        {
            return true;
        }
        return false;
    }

    public static boolean hasPlayerWon(char[][] map, int player)
    {
        if (isHorizontalWin(map, player) == true || isVerticalWin(map, player) == true || isDiagonalWin(map, player) == true)
        {
            return true;
        }
        return false;
    }

    public static boolean isHorizontalWin(char[][] map, int player)
    {
        int count = 0;

        int r;
        int c;



        for (int i = 0; i < map.length; i++)
        {
            for (int j = 0; j < map.length; j++)
            {
                if (map[i][j]==(player))
                {
                    r = i;
                    c = j;
                    while (r >= 0 && r <= 18 && c >= 0 && c <= 18 && map[r][c] == player)
                    {
                        count ++;
                        r += 0;
                        c += 1;
                    }
                }
            }
        }
        if (count == 5)
        {
            return true;

        }
        return false;


    }

    public static boolean isVerticalWin(char[][] map, int player)
    {
        int count = 0;

        int r;
        int c;

        for (int i = 0; i < map.length; i++)
        {
            for (int j = 0; j < map.length; j++)
            {
                if (map[i][j]==(player))
                {
                    r = i;
                    c = j;
                    while (r >= 0 && r <= 18 && c >= 0 && c <= 18 && map[r][c] == player)
                    {
                        count ++;
                        r += 1;
                        c += 0;
                    }
                }
            }
        }
        if (count == 5)
        {
            return true;

        }
        return false;

    }
    public static boolean isDiagonalWin(char[][] map, int player)
    {
        int count = 0;
        int r;
        int c;

        for (int i = 0; i < map.length; i++)
        {
            for (int j = 0; j < map.length; j++)
            {
                if (map[i][j]==(player))
                {
                    r = i;
                    c = j;
                    while (r >= 0 && r <= 18 && c >= 0 && c <= 18 && map[r][c] == player)
                    {
                        count++;
                        r += 1;
                        c += 1;
                    }
                }
            }
        }
        if (count == 5)
        {
            return true;

        }
        return false;


    }


}
import java.util.Scanner;
公共类Gomoku1
{
公共静态void main(字符串[]args)
{
扫描仪输入=新扫描仪(System.in);
char[][]map=新字符[19][19];
int行=0;
int列=0;
//用圆点填充游戏
对于(int i=0;i18 | |列<0 | | |列>18 | | |映射[行][列]='O'| |映射[行][列]='X')
{
返回false;
}
其他的
{
返回true;
}
}
公共静态布尔值isBoardFull(char[][]映射)
{
int openSpots=0;
对于(int i=0;i虽然(r>=0&&r=0&&c=0&&r=0&&c=0&&r=0&&c检查win条件的所有三个函数都有问题:
isHorizontalWin
isVerticalWin
isDiagonalWin
。所有这三个函数都会增加变量
count
,但此变量永远不会设置回零。此外,检查查看是否应在循环内设置
count==5
。下面是一个关于如何修复
isHorizontalWin
的示例:

    public static boolean isHorizontalWin(char[][] map, int player)
    {
        int count = 0;

        int r;
        int c;

        for (int i = 0; i < map.length; i++)
        {
            for (int j = 0; j < map.length; j++)
            {
                if (map[i][j]==(player))
                {
                    r = i;
                    c = j;
                    while (r >= 0 && r <= 18 && c >= 0 && c <= 18 && map[r][c] == player)
                    {
                        count ++;
                        r += 0;
                        c += 1;
                    }
                    if (count == 5)
                    {
                        return true;
                    } else {
                        count = 0;
                    }
                }
            }
        }
        return false;
    }
publicstaticbooleanishOrizontalWin(char[]]map,int-player)
{
整数计数=0;
INTR;
INTC;
对于(int i=0;iwhile(r>=0&&r=0&&c…我现在明白了!我一直试图在while循环之外重置计数。它现在工作得很好。谢谢!太好了!如果这样的话