Sudoku thing


SUBMITTED BY: Guest

DATE: Sept. 25, 2013, 1:19 p.m.

FORMAT: C#

SIZE: 4.4 kB

HITS: 1059

  1. public static RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider(); //for the random numbers
  2. public static void PrintSudoku(int[,] x) //to Print, obviously
  3. {
  4. for (int i = 0; i < 9; i++)
  5. {
  6. for (int j = 0; j < 9; j++)
  7. {
  8. Console.Write(x[i, j] + " ");
  9. }
  10. Console.WriteLine();
  11. }
  12. }
  13. public static bool RunList(List<int> l, int j) //to check if the place is used already
  14. {
  15. bool li = true;
  16. if (l.IsEmpty() == true) //if no place was used before, then good and be true;
  17. {
  18. li = true;
  19. }
  20. else
  21. {
  22. Node<int> p = l.GetFirst();
  23. while (p.GetNext() != null)
  24. {
  25. if (p.GetInfo() == j) //if this place was used, change to false to keep in the loop
  26. {
  27. li = false;
  28. }
  29. p.GetNext();
  30. }
  31. }
  32. return li;
  33. }
  34. public static void Test(int[,] x, List<int> l)
  35. {
  36. byte[] randomNumber = new byte[1];
  37. rngCsp.GetBytes(randomNumber); //random numbers
  38. bool li = false;
  39. int count = 1;
  40. int j = (byte)((randomNumber[0] % 9 + 1));
  41. while (count < 10) //for 10 numbers 1-9
  42. {
  43. for (int i = 0; i < 9; i++) //for 9 rows of array 0-8
  44. {
  45. while (x[i,j] != 0 || li == false) //to check if the spot is occupied by a number and if there is already the same number above it
  46. {
  47. j = (byte)((randomNumber[0] % 9 + 1)); //new random number
  48. li = RunList(l, j); //to check if there is already the same number above
  49. }
  50. x[i, j] = count; //to make the point a number other than 0
  51. Node<int> p = l.GetFirst();
  52. for (int y = 1; y < count; y++)
  53. {
  54. p.GetNext();
  55. }
  56. l.Insert(p, j);//to put the place on the line that was used in the "memory bank" to check later if this row was used already
  57. li = false;//restart
  58. }
  59. List<int> l1 = new List<int>();
  60. //l = l1; tried to restart the list, I probably need a better method
  61. }
  62. }
  63. static void Main(string[] args)
  64. {
  65. int[,] x = new int[9, 9];
  66. for (int i = 0; i < 9; i++)
  67. {
  68. for (int j = 0; j < 9; j++)
  69. {
  70. x[i, j] = 0;
  71. Console.Write(x[i, j] + " ");
  72. }
  73. Console.WriteLine(); //making everything 0
  74. }
  75. //for (int y = 0; y < 9; y++) IGNORE THIS PLEASE
  76. //{
  77. // for (int i = 0; i < 9; i++)
  78. // {
  79. // for (int j = 0; j < 9; j++)
  80. // {
  81. // VerticalTest(x, i, j);
  82. // HTest(x, i, j);
  83. // }
  84. // }
  85. //}
  86. //for (int y = 0; y < 9; y++)
  87. //{
  88. // for (int j = 0; j < 9; j++)
  89. // {
  90. // for (int i = 0; i < 9; i++)
  91. // {
  92. // VerticalTest(x, i, j);
  93. // }
  94. // }
  95. //} IGNORE THAT ABOVE
  96. List<int> l = new List<int>();
  97. Test(x, l); //to use the function
  98. Console.WriteLine("------------------");
  99. PrintSudoku(x); //to print it
  100. //byte[] randomNumber = new byte[1];
  101. //rngCsp.GetBytes(randomNumber);
  102. //Console.WriteLine((byte)((randomNumber[0] % 9 + 1))); code for me for random numbers
  103. }

comments powered by Disqus