Open main menu

CDOT Wiki β

Changes

Sudoku Generator (Example for the OSGi Lab)

5,003 bytes added, 20:35, 13 February 2011
Design and Implementation
First, let me say that I followed Jordan's Voting System example, for both the code as well as the writeup on this wiki.
<h4>1. Define the Service</h4>
Create the bundle ''ca.on.senecac.scs.sudoku''
<h6>1.1 Create SudokuGenerator.java</h6>
<source lang="java">
package ca.on.senecac.scs.sudoku;
}
</source>
<h6>1.2 Define the MANIFEST.MF for the service</h6>
<source lang="xml">
Manifest-Version: 1.0
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Export-Package: ca.on.senecac.scs.sudoku
</source>
<h4> 2. Define the Service Provider</h4>
Create the bundle ''ca.on.senecac.scs.sudoku.provider''
<h6> 2.1 Create SudokuActivator.java</h6>
<source lang="java">
package ca.on.senecac.scs.sudoku.provider;
 
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
 
import ca.on.senecac.scs.sudoku.SudokuGenerator;
import ca.on.senecac.scs.sudoku.provider.internals.*;
 
/**
* The Sudoku Generator provider bundle is registered here.
*
* @author John Selmys
*
*/
public class SudokuActivator implements BundleActivator {
 
private static BundleContext context;
static BundleContext getContext() {
return context;
}
 
public void start(BundleContext bundleContext) throws Exception {
SudokuActivator.context = bundleContext;
SudokuGenerator sg = new SimpleSudokuGenerator();
context.registerService(SudokuGenerator.class.getName(), sg, null);
System.out.println("Sudoku Generator Registered !");
}
 
public void stop(BundleContext context) throws Exception {
SudokuActivator.context = null;
System.out.println("Sudoku Generator Stopped !");
 
}
 
}
</source>
<h6> 2.2 Create SimpleSudokuGenerator.java</h6>
<source lang="java">
package ca.on.senecac.scs.sudoku.provider.internals;
import java.util.Random;
import ca.on.senecac.scs.sudoku.SudokuGenerator;
 
/**
* The code to generate a Sudoku grid is here. Note that the
* method "generate(int,int)" is recursive.
*
* @author John Selmys
*
*/
public class SimpleSudokuGenerator implements SudokuGenerator {
static Grid g = new Grid(9);
static int counter = 0;
/**
* The computation of a random valid (9x9) Sudoku puzzle
* starts here.
*/
public void generate(int row, int col) {
if(row > 8) {
puzzle();
System.out.println("All Done!");
g.display();
System.exit(0);
}
int[] a = {1,2,3,4,5,6,7,8,9};
get9(a);
for(int i=0;i<9;i++) {
int m = a[i];
counter++;
if(available(row,col,m)) {
g.setGrid(row, col, m);
if(col < 8) {
generate(row,col+1);
} else {
generate(row+1,0);
}
}
}
if(col > 0) {
col--;
} else {
col = 8;
row--;
}
g.setGrid(row, col, 0);
}
/**
* Make a 3x3 Sudoku puzzle and place into grid.
*
*/
static void puzzle() {
for(int r=0;r<7;r+=3) {
for(int c=0;c<7;c+=3) {
int x[] = {1,2,3,4,5,6,7,8,9};
get9(x);
for(int row=0;row<3;row++) {
for(int col=0;col<3;col++) {
int R = row+r;
int C = col+c;
int p = row * 3 + col + 1;
if(p==x[0] || p==x[1] || p==x[2] || p==x[3]) {
g.setGrid(R, C, 0);
}
}
}
}
}
}
 
/**
* Checks the grid to see if value 'm' can be placed at
* position (r,c)
*
*/
static boolean available(int r, int c, int m) {
for(int col=0;col<9;col++) {
if(g.getGrid(r, col) == m) return false;
}
for(int row=0;row<9;row++) {
if(g.getGrid(row, c) == m) return false;
}
r = r/3 * 3;
c = c/3 * 3;
for(int row=0;row<3;row++) {
for(int col=0;col<3;col++) {
int R = row + r;
int C = col + c;
if(g.getGrid(R, C) == m) return false;
}
}
return true;
}
/**
* Get an array containing integers [1..9] in random order.
*
*/
static void get9(int a[]) {
Random rand = new Random();
for(int i=8; i>0; i--) {
int r = rand.nextInt(i);
int t = a[i];
a[i] = a[r];
a[r] = t;
}
for(int k=0;k<9;k++)System.out.print(a[k]+" ");
System.out.println(" ");
}
 
 
}
</source>
<h6> 2.3 Create Grid.java</h6>
<source lang="java">
package ca.on.senecac.scs.sudoku.provider.internals;
 
/**
* The Sudoku Grid class contains the results of the Sudoku
* Generator.
*
* @author John Selmys
*
*/
public class Grid {
int[][] grid;
 
Grid(int side) {
grid = new int[side][side];
for (int i=0;i<9;i++)
for(int j=0;j<9;j++)
grid[i][j] = 0;
}
void display() {
for (int i=0;i<9;i++) {
for(int j=0;j<9;j++)
System.out.print(grid[i][j]+" ");
System.out.println();
}
}
void setGrid(int r, int c, int v) {
grid[r][c] = v;
}
int getGrid(int r, int c) {
return grid[r][c];
}
}
</source>
63
edits