MrThoe

Sudoku part 1

Sep 8th, 2022
1,272
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.90 KB | None | 1 0
  1. /** Sudoku in Processing
  2.   * This is a start to creating a Sudoku game generator.
  3.   * The idea would be to first create a program that generates
  4.   * a sudoku board.  Then we would work on creating a solver for the
  5.   * board given randomly revealed tiles.   Then using the solver
  6.   * generate the MINIMUM CELLS REVEALED to create the most difficult
  7.   * sudoku for a person to solve.
  8.   *
  9.   * @Author:  Allen Thoe
  10.   * @Lang:  Processing (Java)
  11.   * @DOWNLOAD:  https://processing.org/download
  12.   */
  13.  
  14.  
  15. ///// FILE 1 /////
  16. Board board = new Board();
  17. int w;
  18.  
  19. void setup(){
  20.   size(452,452);
  21.   w = width/9;
  22.   textSize(w);
  23. }
  24.  
  25. void draw(){
  26.   board.show();
  27. }
  28.  
  29.  
  30. ///// FILE 2 //////
  31. public class Board{
  32.  
  33.   private Cell[][] cell = new Cell[9][9];
  34.  
  35.   public Board(){
  36.     //Populate Cells
  37.     for(int i = 0; i < 9; i++){
  38.       for(int j = 0; j < 9; j++){
  39.         cell[i][j] = new Cell(i,j);
  40.       }
  41.     }
  42.     //Make the Sectors
  43.     for(int r = 0; r < 3; r++){
  44.       for(int c = 0; c < 3; c++){
  45.         makeSector(r,c);
  46.       }
  47.     }
  48.   }
  49.  
  50.   public void show(){
  51.     for(int i = 0; i < 9; i++){
  52.       for(int j = 0; j < 9; j++){
  53.         cell[i][j].show();
  54.       }
  55.     }
  56.     stroke(0);
  57.     strokeWeight(3);
  58.     for(int k = 0; k < 10; k+=3){
  59.       line(0, k*w+1, width, k*w+1);
  60.       line(k*w, 0, k*w, height);
  61.     }
  62.     stroke(255,0,0);
  63.     strokeWeight(1);
  64.   }
  65.  
  66.   /* In this function the sectors are 3x3 and there are 9 total
  67.    * The function will make a random number and then place it into
  68.    * a cell that is open only if that number has not been used (1-9)*/
  69.   public void makeSector(int r, int c){
  70.    
  71.     //Multiply by 3 to account for the fact that there are 3x3 cells
  72.     r *= 3;
  73.     c *= 3;
  74.    
  75.     //Num will store the random integer generated below
  76.     int num;
  77.     //Numbers will contain the list of number 1-9
  78.     int[] numbers = new int[9];
  79.     for(int a = 0; a < 9; a++){
  80.       numbers[a] = a+1;
  81.     }
  82.     //This will be used to break out of the while loop when we find a valid #
  83.     boolean findNum;
  84.    
  85.     for(int i = 0; i < 3; i++){
  86.       for(int j = 0; j < 3; j++){
  87.          findNum = true;
  88.          while(findNum){
  89.            num = (int)(Math.random()*9+1);
  90.            for(int x = 0; x < 9; x++){
  91.              if(numbers[x] == num){
  92.                
  93.                //If this num is still in list, change it to -1  so it cannot be used again
  94.                numbers[x] = -1;  
  95.                cell[i+r][j+c].num = num;
  96.                findNum = false;
  97.              }
  98.            }
  99.          }
  100.       }
  101.     }
  102.   }
  103. }
  104.  
  105.  
  106. ///// FILE 3 /////
  107. public class Cell{
  108.  
  109.   public int num;
  110.   public boolean isRevealed;
  111.   public int row;
  112.   public int col;
  113.  
  114.   public Cell(int c, int r){
  115.     this.row = r;
  116.     this.col = c;
  117.   }
  118.  
  119.   public void show(){
  120.     fill(220);
  121.     rect(row*w, col*w, w, w);
  122.     fill(0);
  123.     text(num, (row+0.2)*w, (col+0.8)*w);
  124.   }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment