簡體   English   中英

有人可以幫助我解決 Gomoku 程序中的獲勝方案嗎?

[英]Can someone help me with my win scenario in my Gomoku program?

我為一項任務編寫了一個程序,我們必須編寫一個簡單的 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;


    }


}

您在檢查獲勝條件的所有三個函數中都有問題: isHorizontalWinisVerticalWinisDiagonalWin 所有三個都會增加變量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;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM