连接四个条件检查 [英] Connect Four Check Conditions

查看:118
本文介绍了连接四个条件检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

像这个网站很多其他用户我创建一个程序,它连接四个。我怎么知道原来。但是我都喜欢我卡住我的检查条件。

Like many other users on this website I am creating a program that does Connect Four. How original I know. However like them all I get stuck on my check conditions.

的游戏键盘是一个简单的6行和7列阵列。我的第一个问题是阵列中的每个位置,我喜欢把它看作是一个矩阵,它被标记为

The gameboard is a simple 6 rows and 7 column array. My first question is for each position in the array, I like to think of it as a Matrix, is it labeled as

00 01 02 03 04 05 06

00 01 02 03 04 05 06

10 11 12 13 14 15 16

10 11 12 13 14 15 16

等等,等等

作为坐标系?这是我相信我的问题在于我如何去检查程序条款。注意,我的检查功能。 (我省略checkDiag因为一旦我找出排,并检查它应该是两者的结合,这一点,我还没有真正尝试去想它尚未所以我不想要的答案)。

as the coordinate system? This is where I believe my problem lies in terms of how I go about checking the program. Observe, my check functions. (I have omitted checkDiag because once I figure out row and check it should be some combination of the two, that and I haven't really tried to think about it yet so I don't want the answer).

public static boolean checkRow(){
    //for each row in gameboard check if there are 4 consecutive 1's or 2's in each row
    for(int i=0, count=0; i<gameboard.length-1; i++){
        for(int j=0; j<gameboard[i].length-1; j++){
            if((gameboard[i][j]==gameboard[i][j+1])&&(gameboard[i][j]!=0)){
                    count++;
            }
            if(count==3){
                return true;

            }

        }
    }
    return false;
}

这并不根据我打印出来,甚至工作。它初始化,但不会返回true时,它应该。

This doesnt even work according to my print out. It initializes, but doesn't return true when it should.

public static boolean checkColumn(){
    //for each column in gameboard check if there are 4 consecutive 1's or 2's in each row
    for(int i=0, count=0;i<gameboard.length-1;i++){
        for(int j=0;j<gameboard[i].length-1;j++){
        //System.out.println(i+", "+j);
            if((gameboard[i][j]==gameboard[i+1][j])&&(gameboard[i][j]!=0)){
                count++;
            }
            if(count==3){
                return true;

            }

        }
    }
    return false;
}

这工作,但有一个轻微的问题。举例来说,如果我有一列4 1的(表示1玩家点),它变成了为真。不过,如果我有这样的事情,

This works, but there is a slight issue. For example, if I have 4 1's (denoting player 1 spot) in a column, it turns up as true. But then if I have something like this,

0000000

0000000

0000000

0000000

1110000

1111000

山坳

(我已经实现了我返回赢怎么看它是如何工作的系统。)所以这这里由一列赢得的东西,这样上面的第三个1第4行的基础上,协调描述的系统中,输入了最后。我知道我在这里的问题是,既然计数为3后最后的1的投入,就返回true。所以,问题是,我在这里重新初始化计数?我想我能做到这一点的第一个for循环,但由于看到了这是不正确。

(I have implemented a system that returns how I won to see how it worked.) So this here won by a column thing such that the third 1 in row 4, based on above coordinate system described, was entered last. I know my issue here is that since count is now 3 after that last "1" put in, it returns true. So the issue is where do I re-initialize count? I thought I could do it in the first for loop, but as seen this is incorrect.

感谢大家。

推荐答案

我已经想通了这一点,现在到checkDiag

I have figured out this, now on to checkDiag

这篇关于连接四个条件检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆