//// Suduko // ?? solver String title="SuDuKo", author="Professor BAM", subtitle="r to reset; q to quit", news="+/- to change difficulty"; int ni=3, nj=3, nr=3, nc=3; int left=50, top=50; int w=50, h=50, wide=w*nc*nj, high=h*nr*ni; boolean waiting=false; // Waiting for input. boolean debug=true; Grid g; int difficulty=50; void setup() { size(600, 600); reset(); } void reset() { if (difficulty<0) difficulty=10; if (difficulty>100) difficulty=90; g = new Grid(); printDigits( g ); println( g.count, "/", g.many, difficulty+"%" ); } void draw() { background(220); fill(255); g.show(); messages(); } void messages() { fill(#0000FF); textSize(20); text( title, width/3, 20 ); textSize(16); text( subtitle, width*2/3 + 40, 32 ); text( news, 20, 44 ); text( author, 20, height-20 ); // text( g.count +" / "+ g.many + " " + difficulty + "%", width-150, height-25 ); float fraction= 100 - 100*( (1.0*g.count) / (1.0*g.many) ); textSize(12); text( fraction, width-80, height-8 ); showDigits( g ); } void showDigits( Grid g ) { int tx=width-30, ty=100; text( "DIGITS:", tx-30, ty-20 ); for (int n=0; nleft+wide || mouseYtop+high) { news= "NOT on a valid cell. ("+ mouseX +","+ mouseY +")"; return; } // Calculate cell placement (on 9x9 grid). int rr=0, cc=0; rr = int( 9.0 * (mouseY-top) / high ); cc = int( 9.0 * (mouseX-left) / wide ); //-- println( rr, cc, mouseY, mouseX ); if (rr<0 || rr>8 || cc<0 || cc>8) { news= "Click was not on a valid cell. ["+ rr +","+ cc +"]"; return; } news= "Clicked on cell "+ rr +","+ cc; g.click( rr, cc ); // Set clicked cell in grid. waiting=true; } void keyPressed() { println( "KEY: ", key, "." ); news= "KEY: " + key + "."; if (key=='q') exit(); if (key=='r') reset(); if (key=='-') { difficulty--; reset(); } if (key=='+') { difficulty++; reset(); } if (key=='=') { difficulty++; reset(); } if (key=='_') { difficulty=50; reset(); } if (key=='<') { difficulty-=5; reset(); } if (key=='>') { difficulty+=5; reset(); } if (key=='\\') debug = ! debug; // if (waiting) { if (key>='0' && key<='9') { int d = key - '0'; g.setCell( d ); g.unclick( ); } waiting=false; } if (key == CODED) { if (keyCode == UP) { g.up(); } else if (keyCode == DOWN) { g.down(); } else if (keyCode == LEFT) { g.left(); } else if (keyCode == RIGHT) { g.right(); } } } class Cell { int digits[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'X' }; int nd = digits.length -2; private int value=0; private int[] choices = new int[nd]; Cell () { for (int n=0; n9) digits[i] = 'A' -1 + i; if (i>36) digits[i] = 'a' -1 + i; } } void reset() { int r, c; int pick= 0; count=0; for (r=0; rdifficulty) { //?? r=int( random(0,nrows) ); //-- c=int( random(0,ncols) ); pick= digits[ iran(nd)+1 ]; println( "iran: ", iran(nd), pick ); // if (debug) println( chance, " > ", difficulty ); if (debug) println( " g[", r, c, "] = ", pick ); grid[r][c] = -pick; ++count; if (debug) println( "count: ", count, chance, difficulty ); } else { if (debug) println( chance, "<", difficulty ); } } } println( "DONE! Count=", count, many ); unclick(); } void unclick() { rclick=cclick= -1; } void click( int rr, int cc ) { if (debug) println( "click(", rr, cc, ")" ); if (rr<0 || rr>nr*nj || cc<0 || cc>nc*nj) { println( "Bad click: ", rclick, cclick ); return; } cclick= cc; rclick= rr; } void show() { int x=left, y=top; for (r=0; r9) t = t/2; fill(#0000FF); if (d<0) { fill(0); t=t-2; } textSize(t); text( abs(d), x+20, y+15+tsize ); } int t=tsize/2; textSize(t); fill(#009900); text( "["+abs(d)+"]", x+5, y+10 ); } boolean selected( int r, int c) { if ( r != rclick ) return false; if ( c != cclick ) return false; return true; } void setCell( int d ) { if (grid[rclick][cclick] < 0) { background(0,0,0); rclick=cclick=-1; // Given; flash black. return; } if ( ! check( rclick, cclick) ) { background(#FF0000); // ERROR; flash RED. rclick=cclick=-1; return; } grid[rclick][cclick] = d; } void up() { if (rclick > 0 && rclick < nr*nj) --rclick; println("UP", rclick); } void down() { if (rclick >= 0 && rclick < nr*nj-1) ++rclick; println("DOWN", rclick); } void left() { } void right() { } //// CHECK FOR ERRORS //// boolean check( int r, int c ) { // Return false if any row, column, or box contains the same digit. int d= abs( grid[r][c] ); for (int j=0; j0) { // check if this digit is unique! // ???? } } } // ONE BOX IS OK // +++++++ return true; } boolean badRow( int r ) { // THIS ROW IS OK // +++++++ return true; } }// class Grid //