Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. package essai;
  2.  
  3. import java.util.Random;
  4.  
  5. public class Essai_checker {
  6.  
  7. static final boolean valide=true;
  8. static final boolean non_valide=false;
  9.  
  10. //check number if not deplicate in row or colomn or sub grid
  11. static boolean checkInt(int a[][],int test,int c_i,int c_j){
  12.  
  13. if(test==0) return non_valide;
  14.  
  15. //check ligne
  16. for(int i=0;i<9;i++){
  17. if(test==a[c_i][i]) return non_valide;
  18. }
  19. //check colomn
  20. for(int i=0;i<9;i++){
  21. if(test==a[i][c_j]) return non_valide;
  22. }
  23.  
  24. //check sub_grid
  25.  
  26. int ii=c_i/3;
  27. int jj=c_j/3;
  28. // this test are for telling wich sub-grid contain the number
  29. if(ii<3) c_i=0;
  30. else
  31. if(ii>1 && ii<2) c_i=3;
  32. else if(ii>2 && ii<=3) c_i=6;
  33.  
  34. if(jj<3) c_j=0;
  35. else
  36. if(jj>1 && jj<2) c_j=3;
  37. else if(jj>2 && jj<=3) c_j=6;
  38.  
  39. for(int i=c_i;i<c_i+3;i++){
  40. for(int j=c_j;j<c_j+3;j++){
  41. if(test==a[c_i][c_j]) return non_valide;
  42. }
  43. }
  44. return valide;
  45. }
  46. public static void main(String[] args) {
  47. int [][] soduko2={
  48. {0,0,0,0,0,0,0,0,0},
  49. {0,0,0,0,0,0,0,0,0},
  50. {0,0,0,0,0,0,0,0,0},
  51. {0,0,0,0,0,0,0,0,0},
  52. {0,0,0,0,0,0,0,0,0},
  53. {0,0,0,0,0,0,0,0,0},
  54. {0,0,0,0,0,0,0,0,0},
  55. {0,0,0,0,0,0,0,0,0},
  56. {0,0,0,0,0,0,0,0,0}
  57. };
  58. Random r=new Random();
  59. for(int i=0;i<9;i++){
  60. for(int j=0;j<9;j++){
  61. int entier=r.nextInt(9)+1;
  62. boolean init =checkInt(soduko2,entier,i,j);
  63. while(init ==non_valide){
  64. entier=r.nextInt(9);
  65. init =checkInt(soduko2,entier,i,j);
  66. }
  67. if(init=true)
  68. soduko2[i][j]=entier;
  69. }
  70. }
  71. for(int i=0;i<9;i++){
  72. for(int j=0;j<9;j++){
  73. System.out.println(soduko2[i][j]+",");
  74. }
  75. }
  76. System.out.println("Done!");
  77.  
  78. }
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement