在Java中对角地查找5行值 [英] Finding 5 values in a row diagonally in Java

查看:126
本文介绍了在Java中对角地查找5行值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个10x10数组:

I have this 10x10 array:

private static int[][] intersections = new int[10][10];

我正在使用此代码查找水平行中是否有5个值:

I'm using this code to find if there are 5 values in a row horizontally:

public static int horizontalCheck() {
    String horizontal = "";
    for (int i = 0; i < intersections.length; i++) {
        for (int j = 0; j < intersections[i].length; j++) {
            horizontal += Integer.toString(intersections[i][j]);
        }
        if (horizontal.indexOf("11111") != -1) {
            // White wins.
            return 1;
        } else if (horizontal.indexOf("22222") != -1) {
            // Black wins.
            return 2;
        }
        horizontal = "";
    }
    return 0;
}

和类似的代码垂直完成。但我的问题是,我怎么能找到对角线上是否有5个值?电路板尺寸为10x10,对角线可以在电路板上的任何位置。如果您有任何问题或需要更多关于代码的信息,请务必询问。

And a similar code to do it vertically. But my question is, how could I find if there are 5 values in a row diagonally? The board is sized 10x10 and the diagonals can be both ways anywhere on the board. If you have any questions or need some more information on the code, make sure to ask.

推荐答案

我建议你写一个帮手这个功能。该函数应该采用以下参数:

I suggest you write a helper function for this. The function should take these parameters:


  • r0 - 起始行检查需要开始

  • c0 - 检查需要开始的起始列

  • dr - 从 { - 1,0,1}}的垂直步骤

  • dc - 从 { - 1,0,1}的水平步骤

  • len - 要查找的商品数量

  • num - 要查找的数字。

  • r0 - The starting row from which the check needs to start
  • c0 - The starting column from which the check needs to start
  • dr - The vertical step from {-1, 0, 1}
  • dc - The horizontal step from {-1, 0, 1}
  • len - The number of items to be found
  • num - The number to find.

以下是此函数的外观:

private static boolean checkRow(int r0, int c0, int dr, int dc, int len, int num) {
    for (int k = 0 ; k != len ; k++) {
        int r = r0 + k*dr;
        int c = c0 + k*dc;
        if (r < 0 || c < 0 || r >= intersections.length || c > intersections[r].length || intersections[r][c] != num) {
            return false;
        }
    }
    return true;
}

手头有这个功能,你可以检查 len 您希望在任何方向连续排列的项目:

With this function in hand, you can check for len items in a row in any direction that you wish:

// See if we've got five eights in any direction:
for (int r = 0 ; r != intersections.length ; r++) {
    for (int c = 0 ; c != intersections[r].length ; c++) {
        if (checkRow(r, c, 0, 1, 5, 8)) {
            System.out.println("Horizontal, starting at "+r+" " +c);
        }
        if (checkRow(r, c, 1, 0, 5, 8)) {
            System.out.println("Vertical, starting at "+r+" " +c);
        }
        if (checkRow(r, c, 1, 1, 5, 8)) {
            System.out.println("Diagonal descending right, starting at "+r+" " +c);
        }
        if (checkRow(r, c, 1, -1, 5, 8)) {
            System.out.println("Diagonal descending left, starting at "+r+" " +c);
        }
    }
}

这篇关于在Java中对角地查找5行值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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