如何通过循环检查connect 4中的获胜者

时间:2013-12-14 02:06:41

标签: java multidimensional-array 2d-games

我正在制作一个连接4程序,我遇到了检查获胜者的问题。我知道自己需要做什么。用于找到垂直赢家的'for'循环,用于水平赢家的循环和用于对角赢家的循环。我只是想不出如何让它寻找胜利者。

这是我到目前为止所拥有的

import javax.swing.JOptionPane;
public class connect4
{
    public static void main(String [] args)//[row][column]
    {
        String grid[][] = new String[7][8];
        for(int x = 1; x <= 6; x++)
        {
            for(int y = 1; y <= 7; y++)
            {
                grid[x][y] = " ";
            }
        }
        String p1 = JOptionPane.showInputDialog("Player 1 pick a single letter for your piece.");
        String p2 = JOptionPane.showInputDialog("Player 2 pick a single letter for your piece.");
        board(grid);
        playerinterface(grid, p1, p2);
    }

    public static void board(String grid[][])
    {
        String board = 
            (" 1  2  3  4  5  6  7" + "\n" +
                "|    "+grid[1][1]+"    |    "+grid[1][2]+"    |    "+grid[1][3]+"    |    "+grid[1][4]+"    |    "+grid[1][5]+"    |    "+grid[1][6]+"    |    "+grid[1][7]+"    |    " +"\n" +
                "|    "+grid[2][1]+"    |    "+grid[2][2]+"    |    "+grid[2][3]+"    |    "+grid[2][4]+"    |    "+grid[2][5]+"    |    "+grid[2][6]+"    |    "+grid[2][7]+"    |    " +"\n" +
                "|    "+grid[3][1]+"    |    "+grid[3][2]+"    |    "+grid[3][3]+"    |    "+grid[3][4]+"    |    "+grid[3][5]+"    |    "+grid[3][6]+"    |    "+grid[3][7]+"    |    " +"\n" +
                "|    "+grid[4][1]+"    |    "+grid[4][2]+"    |    "+grid[4][3]+"    |    "+grid[4][4]+"    |    "+grid[4][5]+"    |    "+grid[4][6]+"    |    "+grid[4][7]+"    |    " +"\n" +
                "|    "+grid[5][1]+"    |    "+grid[5][2]+"    |    "+grid[5][3]+"    |    "+grid[5][4]+"    |    "+grid[5][5]+"    |    "+grid[5][6]+"    |    "+grid[5][7]+"    |    " +"\n" +
                "|    "+grid[6][1]+"    |    "+grid[6][2]+"    |    "+grid[6][3]+"    |    "+grid[6][4]+"    |    "+grid[6][5]+"    |    "+grid[6][6]+"    |    "+grid[6][7]+"    |    " 
            );
        JOptionPane.showMessageDialog(null, board);
    }

    public static void playerinterface(String grid[][], String p1, String p2)
    {
        boolean done = false;
        while(!done)
        {
            int player = 1;        
            if(player%2 != 0)//player1
            {
                player++;
                String q = JOptionPane.showInputDialog("                PLAYER 1"+"\n"+"Put in the number of the column that"+"\n"+"you woulf like to drop your piece in.");
                int choice1 = Integer.parseInt(q);

            }
            else//player2
            {
                player++;
                String q2 = JOptionPane.showInputDialog("                PLAYER 2"+"\n"+"Put in the number of the column that"+"\n"+"you woulf like to drop your piece in.");
                int choice2 = Integer.parseInt(q2);
            }
        }
    }

    public static void drop(int choice1, int choice2, int player)
    {
        while()
        {
        }
    }

    public static void check()
    {
        for(int x = 1; x <= 6; x++)//rows
        {
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您可以分享您已有的任何代码吗?我只是在脑海中喋喋不休,但我怎么能想象呢......

你可以通过有限的方式在连接4中获胜(取决于你的电路板有多大),一种方法是使用一种方法来检查所有垂直行并寻找“赢”,一种方法可以看到水平行,一个看对角线'胜利'(双向)。然后在我的代码中我可以调用

checkVertial()
checkHorizontal()
checkDiagonalRight()
checkDiagonalLeft()

并检查是否返回true。在这些方法中,只需循环遍历x和y行并查看是否存在(如果标记放置在斑点中,则为布尔值?)

如果棋盘尺寸大于获胜所需的连续标记数量,这可能会变得棘手。你必须检查彼此相邻的那些是否在那时。

只是一个想法,不知道什么是最好的解决方案而不看你的代码。

编辑: 我有另一个想法 2)每当玩家在棋盘上添加标记片时,将1加到你跟踪的值上。因此,例如在游戏的第一步中,我将一个标记向下放置一个值为player1获得值1.现在,如果我在下一个转弯处放置一个新的标记块,那么我可以:

1. check if its next to a red piece 
2. if it is add 1 to that value.
3. if it isn't next to a red piece create a new value

所以最终如果该值达到5,那么我们就赢了。这里的诀窍是如果我在两个回合的两端添加两个部分,我需要制作两个变量来跟踪。在我看来,这是棘手的,但不需要每次都通过检查循环。我想如果你这样做就会有一堆创建的变量,但你只关心达到最终目标的变量。