Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** Sudoku in Processing
- * This is a start to creating a Sudoku game generator.
- * The idea would be to first create a program that generates
- * a sudoku board. Then we would work on creating a solver for the
- * board given randomly revealed tiles. Then using the solver
- * generate the MINIMUM CELLS REVEALED to create the most difficult
- * sudoku for a person to solve.
- *
- * @Author: Allen Thoe
- * @Lang: Processing (Java)
- * @DOWNLOAD: https://processing.org/download
- */
- ///// FILE 1 /////
- Board board = new Board();
- int w;
- void setup(){
- size(452,452);
- w = width/9;
- textSize(w);
- }
- void draw(){
- board.show();
- }
- ///// FILE 2 //////
- public class Board{
- private Cell[][] cell = new Cell[9][9];
- public Board(){
- //Populate Cells
- for(int i = 0; i < 9; i++){
- for(int j = 0; j < 9; j++){
- cell[i][j] = new Cell(i,j);
- }
- }
- //Make the Sectors
- for(int r = 0; r < 3; r++){
- for(int c = 0; c < 3; c++){
- makeSector(r,c);
- }
- }
- }
- public void show(){
- for(int i = 0; i < 9; i++){
- for(int j = 0; j < 9; j++){
- cell[i][j].show();
- }
- }
- stroke(0);
- strokeWeight(3);
- for(int k = 0; k < 10; k+=3){
- line(0, k*w+1, width, k*w+1);
- line(k*w, 0, k*w, height);
- }
- stroke(255,0,0);
- strokeWeight(1);
- }
- /* In this function the sectors are 3x3 and there are 9 total
- * The function will make a random number and then place it into
- * a cell that is open only if that number has not been used (1-9)*/
- public void makeSector(int r, int c){
- //Multiply by 3 to account for the fact that there are 3x3 cells
- r *= 3;
- c *= 3;
- //Num will store the random integer generated below
- int num;
- //Numbers will contain the list of number 1-9
- int[] numbers = new int[9];
- for(int a = 0; a < 9; a++){
- numbers[a] = a+1;
- }
- //This will be used to break out of the while loop when we find a valid #
- boolean findNum;
- for(int i = 0; i < 3; i++){
- for(int j = 0; j < 3; j++){
- findNum = true;
- while(findNum){
- num = (int)(Math.random()*9+1);
- for(int x = 0; x < 9; x++){
- if(numbers[x] == num){
- //If this num is still in list, change it to -1 so it cannot be used again
- numbers[x] = -1;
- cell[i+r][j+c].num = num;
- findNum = false;
- }
- }
- }
- }
- }
- }
- }
- ///// FILE 3 /////
- public class Cell{
- public int num;
- public boolean isRevealed;
- public int row;
- public int col;
- public Cell(int c, int r){
- this.row = r;
- this.col = c;
- }
- public void show(){
- fill(220);
- rect(row*w, col*w, w, w);
- fill(0);
- text(num, (row+0.2)*w, (col+0.8)*w);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment