检查二维数组对角线? [英] check 2d array diagonally?

查看:48
本文介绍了检查二维数组对角线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对角搜索3x3 2d数组,如下所示:

I'm trying to search a 3x3 2d array diagonally, like this:

我想检查对角线中的所有框是否都具有相同的值.这是我尝试执行的操作:

I want to check if all boxes in the diagonal have the same value. Here is how I try to do it:

thisOne = board[0][2];    //set to 'X'
    for(i = 0; i<3; i++) {
        for(j = 3; j>0; j--){
            if(board[i][j-1] != thisOne) {
                thisOne= '\0';
            }
        }
    }
//since all boxes were 'X', thisOne is still set to 'X'
if(thisOne != '\0') {
    winner = thisOne;
    printf("vinnare på nördöst\n");
}

因此,运行此代码后,如果所有方框均为X,则 winner 应该为"X".但是代码没有做到这一点,为什么呢?

So after running this code, winner should be 'X', if all boxes are X's. But the code does not do that, why is that?

推荐答案

您只需要检查对角线单元格,而不需要检查所有单元格.

You need to check only diagonal cells instead of checking all the cells.

这篇关于检查二维数组对角线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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