fork(3) download
  1. /**
  2.  *
  3.  *
  4.  */
  5. import java.util.Random;
  6. public class Sudoku {
  7. public static void main(String args[]){
  8.  
  9. Random generator = new Random();
  10.  
  11. int edge=3, rowbase=0, colbase=0;
  12. int[][] sudoku = new int [9][9];
  13. int i=0, j=0, temp=0, l=0, m=0, n=0, count=0, rcounter=0;
  14. int[] regionCheck = new int [9];
  15. for(i=0; i<9; i++){
  16. for (j=0; j<9; j++){
  17. sudoku[i][j]=0;
  18. }
  19. }
  20. boolean isUsed;
  21.  
  22. //Everything above is initialization, icky.
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48. for (i=0; i<9; i++){
  49. for(j=0; j<9; j++){
  50. isUsed=false;
  51. temp = generator.nextInt(9)+1;
  52.  
  53. for(l=0; l<9; l++){//Makes all vertical work.
  54. if(sudoku[l][j] == temp){
  55. isUsed=true;
  56. }
  57. }
  58. for(m=0; m<9; m++){//makes all horizontal work
  59. if(sudoku[i][m] == temp){
  60. isUsed=true;
  61. }
  62. }
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73. if(count >= 100){
  74. for(l=0; l<9; l++){
  75. sudoku[i][l]=0;
  76. }
  77. j=0;
  78. }
  79. if (count >1000){
  80. count=0;
  81. i--;
  82. break;
  83. }
  84. rowbase = i-(i%3);
  85. if(i==2 || i==5 || i==8 ){
  86. if(rowbase == 0 || rowbase == 3 || rowbase == 6){
  87. isUsed= RegionCheck.RegCheck(rowbase, sudoku);
  88. }
  89. }
  90.  
  91. if(isUsed==false){
  92. sudoku[i][j]=temp;
  93. }
  94. else{
  95. count++;
  96. j--;
  97. }//end else
  98. }//End inner for loop
  99. }//End outer for loop
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112. for(i=0; i<9; i++){
  113. for (j=0; j<9; j++){
  114. System.out.print(sudoku[i][j] + " | ");
  115. }
  116. System.out.println();
  117. System.out.println("-----------------------------------");
  118. }
  119.  
  120. }//End main
  121. }//end class
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133. //////////////////////
  134. ///RegionCheck.java
  135. //////////////////////
  136. /**
  137.  *
  138.  *
  139.  */
  140. public class RegionCheck {
  141. public static boolean RegCheck(int regionY, int[][] sudoku){
  142. boolean okay = false;
  143. int[] regionUsed = new int[9];
  144. int i=0, j=0, regionTester=0, counter=0, numcount;
  145. for (i=regionTester; i<regionTester+3; i++){
  146. for (; j<3; j++){
  147. regionUsed[counter]=sudoku[i][j];
  148. counter++;
  149. }
  150. }
  151. for(i=0; i<9; i++){
  152. numcount=regionUsed[i];
  153. for(j=0; j<9; j++){
  154. if(j==i){
  155. //null
  156. }
  157. else if(numcount == regionUsed[j]){
  158. okay=false;
  159. }
  160. }
  161. }
  162.  
  163. return okay;
  164. }//End Method
  165.  
  166. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty