two dimensional arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • estera
    New Member
    • Sep 2007
    • 2

    two dimensional arrays

    I have an assignment that I'm having problems with and I was wondering if you could help me. What I have to do is to generate a 5x5 two-dimmensional array with 25 two-digit numbers, but I have to make sure there are no duplicates. Then I have to cover 10 randomly picked numbers from the array with "XX", but I have to make sure that none of the 10 covered numbers make 5 in a row either horizontally, vertically or diagonally.

    So what I'm having problems with is how to make sure there are no duplicates, and how to make sure that none of the 10 covered numbers make 5 in a row.
    This is the code, what I did so far.
    Thank you.

    [CODE=java]import acm.program.*;
    import acm.graphics.*;
    import acm.io.*;

    class LingoBoard1 extends ConsoleProgram
    {

    void main()
    {

    int[][] matrix = createArray();

    println("The Lingo board is: ");
    displayArray(ma trix);

    println("The board with 10 elements covered is: ");
    matrix = coverArray(matr ix);
    displayArray(ma trix);
    }
    int[][] createArray ()
    {
    int[][] matrix = new int[5][5];
    boolean isDuplicate = true;
    int row, column;

    for (row = 0; row < 5; row++)
    for (column = 0; column < 5; column++)
    while(isDuplica te)
    {
    matrix[row][column] = (int)(Math.rand om() * (99-10+1)) + 10;
    isDuplicate = checkDuplicate( matrix,row,colu mn);

    }
    return matrix;
    }

    void displayArray (int[][] matrix)
    {
    for (int row = 0; row < 5; row++)
    {
    for (int column = 0; column < 5; column++)
    {
    print(matrix[row][column] + " ");
    }
    println();
    }
    }

    boolean checkDuplicate( int[][] matrix,int i, int j)
    {
    boolean b = false;

    for (int row = 0; row < 5; row++)
    for (int column = 0; column < 5; column++)
    if(matrix[row][column] == matrix[i][j])
    b = true;
    else b = false;

    return b;
    }

    int[][] coverArray(int[][] a)
    {

    //int[][] a;
    int i,j;

    for(i = 0; i < 5; i++)
    {
    j = (int)(Math.rand om()*5);
    a[i][j] = 'x';
    }
    for(j = 0; j < 5; j++)
    {
    i = (int)(Math.rand om()*5);
    a[i][j] = 'x';
    }
    return a;
    }
    public static void main(String[] args)
    {
    new LingoBoard1().s tart();
    }
    public void run() { main(); }
    }[/CODE]
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Hi estera! Welcome to TSDN!

    Do I understand correctly, you have to avoid thing like this
    Code:
    xx  xx  xx  xx  xx
    21  22  23  24  25
    31  32  33  34  35
    41  42  43  44  45
    51  52  53  54  55
    or this
    Code:
    xx  12  13  14  15
    xx  22  23  24  25
    xx  32  33  34  35
    xx  42  43  44  45
    xx  52  53  54  55
    or this
    Code:
    xx  12  13  14  15
    21  xx  23  24  25
    31  32  xx  34  35
    41  42  43  xx  45
    51  52  53  54  xx
    or this
    Code:
    11  12  13  14  xx
    21  22  23  xx  25
    31  32  xx  34  35
    41  xx  43  44  45
    xx  52  53  54  55
    Correct?

    If so, you have to check if it would make a row as soon as you're setting #5 - #10. You could have three (or four) methods:
    [CODE=java]
    private boolean checkHorizontal (int x, int y)
    {
    // ...
    }
    [/CODE][CODE=java]
    private boolean checkVertical(i nt x, int y)
    {
    // ...
    }
    [/CODE][CODE=java]
    private boolean checkDiagonal(i nt x, int y)
    {
    // ...
    }
    [/CODE]and use each of those to check, if setting xx at the positon (x,y) would create a line.

    Greetings,
    Nepomuk

    Comment

    • estera
      New Member
      • Sep 2007
      • 2

      #3
      Originally posted by nepomuk
      Hi estera! Welcome to TSDN!

      Do I understand correctly, you have to avoid thing like this
      Code:
      xx  xx  xx  xx  xx
      21  22  23  24  25
      31  32  33  34  35
      41  42  43  44  45
      51  52  53  54  55
      or this
      Code:
      xx  12  13  14  15
      xx  22  23  24  25
      xx  32  33  34  35
      xx  42  43  44  45
      xx  52  53  54  55
      or this
      Code:
      xx  12  13  14  15
      21  xx  23  24  25
      31  32  xx  34  35
      41  42  43  xx  45
      51  52  53  54  xx
      or this
      Code:
      11  12  13  14  xx
      21  22  23  xx  25
      31  32  xx  34  35
      41  xx  43  44  45
      xx  52  53  54  55
      Correct?

      If so, you have to check if it would make a row as soon as you're setting #5 - #10. You could have three (or four) methods:
      [CODE=java]
      private boolean checkHorizontal (int x, int y)
      {
      // ...
      }
      [/CODE][CODE=java]
      private boolean checkVertical(i nt x, int y)
      {
      // ...
      }
      [/CODE][CODE=java]
      private boolean checkDiagonal(i nt x, int y)
      {
      // ...
      }
      [/CODE]and use each of those to check, if setting xx at the positon (x,y) would create a line.

      Greetings,
      Nepomuk
      Thanks. One more question. How would I check to see if the setting XX creates a line? Can you give me an example for one of the methods?

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        Originally posted by estera
        Thanks. One more question. How would I check to see if the setting XX creates a line? Can you give me an example for one of the methods?
        I won't write any of the methods for you, but here's how it would work:
        [CODE=java]
        private boolean checkHorizontal (int x, int y, int[][] matrix)
        // you can leave out the last part, if your matrix is defined globally
        {
        // you have the y-value of the number to be set, so check if there are 5 - 1 = 4 Numbers in that line covered already
        int covered = 0;
        // your code here
        return ...; // What do you return?
        }
        [/CODE]
        Greetings,
        Nepomuk

        Comment

        Working...