In class we are currently making lottery tickets as a project, I'm currently stuck on a question with equals: The same rows are not allowed to appear multiple times on the same ticket.
I have tried a couple of things, but nothing has been working yet.
public class TalRækker //rows
{
static Random rnd = new Random();
public static int[,] Line()
{
int[,] line = new int[10, 7];
//int x = rnd.Next(1, 36);
for (int i = 0; i < line.GetLength(0); i )
{
for (int j = 0; j < line.GetLength(1); j )
{
line[i, j] = rnd.Next(1, 36);
}
}
return line;
}
public static void Print2DArray<T>(T[,] line)
{
var lineNumber = 1;
for (int i = 0; i < line.GetLength(0); i )
{
Console.Write(" {0:D2}.\t", lineNumber);
for(int j = 0; j < line.GetLength(1); j )
{
Console.Write("{0:D2} ", line[i, j]);
}
Console.WriteLine();
lineNumber ;
}
}
CodePudding user response:
I would be tempted to use a jagged array to make this a little easier to handle. I'd then split the functionality required into separate methods to allow recursion if there are duplicates:
static Random rnd = new Random();
public static int[][] GenerateTicket(int numberOfRows, int rowLength)
{
var lines = new int[numberOfRows][];
for (int i =0; i < lines.Length; i )
{
lines[i] = GenerateLine(rowLength, lines);
}
return lines;
}
public static int[] GenerateLine(int rowLength, int[][] existingLines)
{
var newLine = new int[rowLength];
for (int j = 0; j < rowLength; j )
{
newLine[j] = GenerateNumber(newLine);
}
var orderedLine = newLine.OrderBy(n => n).ToArray();
var exists = existingLines.Where(line => line != null).Any(line => line.SequenceEqual(orderedLine));
// If a line already exists, call the method again to generate a new line, otherwise return the line.
return exists ? GenerateLine(rowLength, existingLines) : orderedLine;
}
public static int GenerateNumber(int[] line)
{
var number = rnd.Next(1, 36);
// Check if the line already contains the number, if so, call the method again to generate a new number, otherwise return the number.
return line.Contains(number) ? GenerateNumber(line) : number;
}
public static void Print2DArray<T>(T[][] lines)
{
var lineNumber = 1;
for (int i=0; i < lines.Length; i )
{
Console.Write(" {0:D2}.\t", lineNumber);
lineNumber ;
for (int j = 0; j < lines[i].Length; j )
{
Console.Write("{0:D2} ", lines[i][j]);
}
Console.WriteLine();
}
}
Update
I've sorted the newLine before using SequenceEqual, so that this will compare previously ordered lines with this and therefore return true/false correctly.
CodePudding user response:
I assume that this is an array exercise. Otherwise I would prefer to use a List.
So you could do something similar to the following. Please note how each function follows the single responsibility principle and usage of access modifiers.
It is a good use case here for Do-While loop as your condition depends on the first outcome of row generation.
I did not check the occurrence of the same number in a row as you did not state that in your question.
public class TalRækker
{
static Random rnd = new Random();
public static int[,] GenerateLotteryTicket(int totalLines, int lineLength)
{
int[,] lines = new int[totalLines, lineLength];
int[] newLine = new int[lineLength];
for (int i = 0; i < totalLines; i )
{
do
{
newLine = GenerateLine(lineLength);
}
while (ExistInTicket(lines,newLine));
for (int j = 0; j < lineLength; j )
{
lines[i,j] = newLine[j];
}
}
return lines;
}
private static int[] GenerateLine(int lineLength)
{
int[] row = new int[lineLength];
for (int i = 0; i < lineLength; i )
{
row[i] = rnd.Next(1, 36);
}
return row;
}
private static bool ExistInTicket(int[,] ticket, int[] line)
{
bool rowMatchFound;
for(int i = 0; i < ticket.GetLength(0); i )
{
rowMatchFound = true;
for (int j = 0; j < ticket.GetLength(1); j )
{
if(ticket[i,j] != line[j])
{
rowMatchFound = false;
break;
}
}
if(rowMatchFound) return true;
}
return false;
}
public static void Print2DArray<T>(T[,] line)
{
var lineNumber = 1;
for (int i = 0; i < line.GetLength(0); i )
{
Console.Write(" {0:D2}.\t", lineNumber);
for (int j = 0; j < line.GetLength(1); j )
{
Console.Write("{0:D2} ", line[i, j]);
}
Console.WriteLine();
lineNumber ;
}
}
}