Java对角线检查

时间:2014-01-19 02:09:49

标签: java arrays

我试图识别数字模式,其中一个数字在一个二维数组中连续重复4次,我只想出水平和垂直识别,有人可以帮我识别对角线和数字的数字模式在6行和7列的表中出现多次(垂直与水平或反之)的模式

数字模式是

pattern1.txt

0 1 0 3 1 6 1

0 1 6 8 6 0 1

5 6 2 1 8 2 9

6 5 3 3 3 3 1

1 3 6 1 4 0 7

6 1 1 9 4 0 7

pattern2.txt

0 1 0 3 1 6 1

0 1 6 5 6 8 1

5 0 2 5 8 1 9

6 2 6 5 1 1 1

1 9 6 5 4 1 7

3 0 3 3 4 0 7

pattern3.txt

0 1 0 3 1 6 1

0 1 6 8 6 0 1

5 6 2 1 6 2 9

6 5 6 6 1 9 1

1 3 6 1 4 0 7

3 6 3 3 4 0 7

pattern4.txt

0 1 0 3 1 6 1

0 1 6 8 6 0 1

9 6 2 1 8 2 9

6 9 6 1 1 9 1

1 3 9 1 4 0 7

3 3 3 9 4 0 7

pattern5.txt

0 1 0 3 1 6 1

0 1 6 8 6 0 1

9 6 2 1 8 2 9

6 0 6 1 1 9 1

1 3 9 1 4 0 7

3 3 6 7 4 0 7

pattern6.txt

0 1 0 3 1 6 1

0 1 6 8 3 0 1

9 6 2 1 8 3 9

6 0 6 1 1 9 1

1 2 9 1 4 0 7

3 3 6 7 4 0 7

import java.io.*;
import java.util.Scanner;

public class petternrcognation
{
public static void main (String[]args) throws Exception
{
    Scanner keyboard = new Scanner(System.in);

    System.out.print("Enter the filename : ");
    String filename = keyboard.next();

    File myFile = new File (filename);

    Scanner dataFromTxt = new Scanner(myFile);

    int [] [] pattern = new int [6] [7];

    while (dataFromTxt.hasNext())
    {
        pattern[0][0] = dataFromTxt.nextInt();
        pattern[0][1] = dataFromTxt.nextInt();
        pattern[0][2] = dataFromTxt.nextInt();
        pattern[0][3] = dataFromTxt.nextInt();
        pattern[0][4] = dataFromTxt.nextInt();
        pattern[0][5] = dataFromTxt.nextInt();
        pattern[0][6] = dataFromTxt.nextInt();

        pattern[1][0] = dataFromTxt.nextInt();
        pattern[1][1] = dataFromTxt.nextInt();
        pattern[1][2] = dataFromTxt.nextInt();
        pattern[1][3] = dataFromTxt.nextInt();
        pattern[1][4] = dataFromTxt.nextInt();
        pattern[1][5] = dataFromTxt.nextInt();
        pattern[1][6] = dataFromTxt.nextInt();

        pattern[2][0] = dataFromTxt.nextInt();
        pattern[2][1] = dataFromTxt.nextInt();
        pattern[2][2] = dataFromTxt.nextInt();
        pattern[2][3] = dataFromTxt.nextInt();
        pattern[2][4] = dataFromTxt.nextInt();
        pattern[2][5] = dataFromTxt.nextInt();
        pattern[2][6] = dataFromTxt.nextInt();

        pattern[3][0] = dataFromTxt.nextInt();
        pattern[3][1] = dataFromTxt.nextInt();
        pattern[3][2] = dataFromTxt.nextInt();
        pattern[3][3] = dataFromTxt.nextInt();
        pattern[3][4] = dataFromTxt.nextInt();
        pattern[3][5] = dataFromTxt.nextInt();
        pattern[3][6] = dataFromTxt.nextInt();

        pattern[4][0] = dataFromTxt.nextInt();
        pattern[4][1] = dataFromTxt.nextInt();
        pattern[4][2] = dataFromTxt.nextInt();
        pattern[4][3] = dataFromTxt.nextInt();
        pattern[4][4] = dataFromTxt.nextInt();
        pattern[4][5] = dataFromTxt.nextInt();
        pattern[4][6] = dataFromTxt.nextInt();

        pattern[5][0] = dataFromTxt.nextInt();
        pattern[5][1] = dataFromTxt.nextInt();
        pattern[5][2] = dataFromTxt.nextInt();
        pattern[5][3] = dataFromTxt.nextInt();
        pattern[5][4] = dataFromTxt.nextInt();
        pattern[5][5] = dataFromTxt.nextInt();
        pattern[5][6] = dataFromTxt.nextInt();
    }

    for (int r = 0; r < pattern.length; r++)
    {
        for (int c = 0; c < pattern[r].length; c++)
        {
            System.out.print(pattern[r][c] + " ");
        }
        System.out.println(" ");
    }

    if (checkHorizontal(pattern) == true )
    {
        System.out.println("Horizontal Checked true");
    }
    else if (checkVertical(pattern) == true )
    {
        System.out.println("Vertical Checked true");
    }
}

public static boolean isConsecutiveFour(int[] values)
{
    for (int i = 0; i < values.length - 3; i++)
    {
        boolean isEqual = true;

        for (int j =i; j < i + 3; j++)
        {
            if (values[j] != values[j + 1])
            {
                isEqual = false;
                break;
            }
        }
        if (isEqual)
        {
            return true;
        }
    }
    return false;
}

public static boolean checkHorizontal(int[][] values)
{
    int numberOfRows = 6;
    int numberOfColumns = 7;

    for (int i = 0; i < numberOfRows; i++)
    {
        if (isConsecutiveFour(values[i]))
        {
            return true;
        }
    }
    return false;
}

public static boolean checkVertical(int [][] values)
{
    int numberOfRows = 6;
    int numberOfColumns = 7;

    for (int j =0; j < numberOfColumns; j++)
    {
        int [] column = new int[numberOfRows];

        if (isConsecutiveFour(column))
        {
            return true;
        }
    }
    return false;
}
}

2 个答案:

答案 0 :(得分:0)

要检查对角线,对于\ diagonals,您将增加行和列。对于/ diagonals,您将增加行和减量列。否则它们将与您已实施的非常相似。

答案 1 :(得分:0)

首先,您应该再次检查checkVertical,创建一个正确长度的数组,但不要将values[][]中的任何值分配给它。

您在isConsecutiveFour中所做的工作可能会引导您实现目标。让我解释一下你的所作所为,以帮助你理解你必须做的事情:

从一行中的每个可能位置开始,检查以下3个是否相同。然后你对每一行做同样的事情。 如果您不检查行中的下一行(values[c+1]基本上是values[r][c+1]),还是下一列(values[r+1][c])或垂直行(values[r+1][c+1] / {{ 1}})是相同的,你得到其他模式。

我的提示:摆脱不同的功能,迭代所有可能的位置[r] [c]并尝试这样的事情:

values[r+1][c-1]

我认为应该很容易弄清楚其他模式的代码应该是什么样的。