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]
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]
Comment