Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- class LatinSquare
- {
- static void Main()
- {
- var LatinSquare = new int[9, 9];
- var Pattern = new int[] {1,2,3,4,5,6,7,8,9};
- Pattern = Shuffle(Pattern);
- //Use the first row as a pattern for the rest
- //of the Latin Square
- for (int i = 0; i < 9; i++)
- {
- {LatinSquare[i,0] = Pattern[i];
- }
- for (int x = 0; x < 9; x++)
- {
- for (int y = 1; y < 9; y++)
- {
- LatinSquare[Pattern[y] - 1, y] =
- LatinSquare[LatinSquare[x, 0] - 1, 0];
- }
- Pattern = RotatePattern(Pattern);
- }
- PrintLatinSquare(LatinSquare);
- Console.Read();
- }
- private static T[] Shuffle<T>(T[] OriginalArray)
- {
- var matrix = new SortedList();
- var r = new Random();
- for (int x = 0; x <= OriginalArray.GetUpperBound(0); x++)
- {
- int i = r.Next();
- if (!matrix.ContainsKey(i))
- {
- matrix.Add(i, OriginalArray[x]);
- }
- }
- var OutputArray = new T[OriginalArray.Length];
- var counter = 0;
- foreach (DictionaryEntry entry in matrix)
- {
- OutputArray[counter++] = (T)entry.Value;
- }
- return OutputArray;
- }
- private static int[] RotatePattern(int[] Pattern)
- {
- int temp = Pattern[0];
- ;for (int y = 0; y < 9 - 1; y++)
- {
- Pattern[y] = Pattern[y + 1];
- }
- Pattern[9 - 1] = temp;
- return Pattern;
- }
- private static void PrintLatinSquare(int[,] LatinSquare)
- {
- //Print out the Latin Square
- for (int i = 0; i < 9; i++)
- {
- for (int j = 0; j < 9; j++)
- {
- Console.Write(LatinSquare[j, i].ToString().PadLeft(3));
- }
- Console.WriteLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement