Commit 7ee63c06 authored by Daniel Le Berre's avatar Daniel Le Berre

Moved sudoku code to the right place.


git-svn-id: svn+ssh://svn.forge.ow2.org/svnroot/sat4j/maven/trunk@307 ba638df5-4473-46d1-82f8-c3ae2a17a6e1
parent 2afa9a50
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry excluding="org/sat4j/apps/sudoku/SDWebStart.java" kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="lib/jlfgr-1_0.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/Java Web Start"/>
<classpathentry combineaccessrules="false" kind="src" path="/org.sat4j.core"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>org.sat4j.sudoku</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
Manifest-Version: 1.0
Built-By: Daniel Le Berre
Main-Class: org.sat4j.apps.sudoku.SDApplication
Specification-Title: SAT4J
Specification-Version: NA
Specification-Vendor: Daniel Le Berre
Implementation-Title: SAT4J
Implementation-Version: 2.0
Implementation-Vendor: CRIL CNRS UMR 8188 - Universite d'Artois
Class-Path: org.sat4j.core.jar lib/jlfgr-1_0.jar
"Problem","Package","File","Priority","Line","Description","Rule set","Rule"
<html><head><title>PMD</title></head><body>
<center><h3>PMD report</h3></center><center><h3>Problems found</h3></center><table align="center" cellspacing="0" cellpadding="3"><tr>
<th>#</th><th>File</th><th>Line</th><th>Problem</th></tr>
</table></body></html>
\ No newline at end of file
No problems found!
\ No newline at end of file
<html><head><title>PMD</title></head><style type="text/css"><!--
body { background-color: white; font-family:verdana, arial, helvetica, geneva; font-size: 16px; font-style: italic; color: black; }
.title { font-family: verdana, arial, helvetica,geneva; font-size: 12px; font-weight:bold; color: white; }
.body { font-family: verdana, arial, helvetica, geneva; font-size: 12px; font-weight:plain; color: black; }
#TableHeader { background-color: #003366; }
#RowColor1 { background-color: #eeeeee; }
#RowColor2 { background-color: white; }
--></style><body><center><br></center></body></html>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<pmd version="4.2.4" timestamp="2009-02-13T14:42:35.080">
</pmd>
\ No newline at end of file
package org.sat4j.apps.sudoku;
import java.awt.print.Paper;
public class A4Paper extends Paper {
private static final int DOTS_PER_INCH = 72;
public A4Paper() {
super();
setSize(8.3 * DOTS_PER_INCH, 11.8 * DOTS_PER_INCH);
setImageableArea(0.5 * DOTS_PER_INCH, 0.5 * DOTS_PER_INCH,
7.3 * DOTS_PER_INCH, 10.8 * DOTS_PER_INCH);
}
}
package org.sat4j.apps.sudoku;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ButtonHandler<T extends Enum<T>> implements ActionListener {
ButtonHandler() {
}
ButtonHandler(CommandHandler<T> commandHandler, Enum<T> command) {
this.commandHandler = commandHandler;
this.command = command;
}
public void actionPerformed(ActionEvent actionEvent) {
try {
commandHandler.execute(command);
} catch (Exception e) {
e.printStackTrace();
}
}
private CommandHandler<T> commandHandler;
private Enum<T> command;
}
package org.sat4j.apps.sudoku;
class ButtonHandlerFactory<T extends Enum<T>> {
ButtonHandlerFactory() {
}
ButtonHandlerFactory(CommandHandler<T> commandHandler) {
this.commandHandler = commandHandler;
}
ButtonHandler<T> newButtonHandler(Enum<T> command) {
return new ButtonHandler<T>(commandHandler, command);
}
CommandHandler<T> commandHandler;
}
This diff is collapsed.
package org.sat4j.apps.sudoku;
/**
* Provides integer fields to describe the possible status of each cell.
*/
public enum CellStatus {
/**
* The cell is protected, so it is not possible to type a value into it.
*/
PROTECTED,
/**
* The cell's value was entered by the user.
*/
USER_ENTERED,
/**
* The cell's value was entered by the automatic solver.
*/
SOLVER_ENTERED
};
package org.sat4j.apps.sudoku;
public abstract class CommandHandler<T extends Enum<T>> {
public CommandHandler(SuDoku sudoku) {
this.sudoku = sudoku;
}
public abstract void execute(Enum<T> command);
SuDoku sudoku;
}
package org.sat4j.apps.sudoku;
public class Coordinate {
int r, c;
public Coordinate(int r, int c) {
this.r = r;
this.c = c;
}
public int getRow() {
return r;
}
public int getColumn() {
return c;
}
}
package org.sat4j.apps.sudoku;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class CoordinateSet {
CoordinateSet(int rows, int columns) {
coordinates = new ArrayList<Coordinate>();
random = new Random();
this.rows = rows;
this.columns = columns;
for (int r = 1; r <= rows; r++) {
for (int c = 1; c <= columns; c++) {
coordinates.add(new Coordinate(r, c));
}
}
}
public Coordinate[] getGroup() {
Coordinate centre = null;
if (rows % 2 == 1) {
centre = new Coordinate((rows + 1) / 2, (columns + 1) / 2);
remove(centre);
}
Coordinate[] result = new Coordinate[4];
result[0] = getCoordinate();
if (result[0].getRow() * 2 == rows + 1) {
result[1] = new Coordinate(result[0].getRow(), columns + 1
- result[0].getColumn());
result[2] = new Coordinate(result[0].getColumn(), result[0]
.getRow());
result[3] = new Coordinate(columns + 1 - result[0].getColumn(),
result[0].getRow());
} else if (result[0].getColumn() * 2 == columns + 1) {
result[1] = new Coordinate(result[0].getColumn(), result[0]
.getRow());
result[2] = new Coordinate(rows + 1 - result[0].getRow(), result[0]
.getColumn());
result[3] = new Coordinate(result[0].getColumn(), rows + 1
- result[0].getRow());
} else {
result[1] = new Coordinate(result[0].getRow(), columns + 1
- result[0].getColumn());
result[2] = new Coordinate(rows + 1 - result[0].getRow(), result[0]
.getColumn());
result[3] = new Coordinate(rows + 1 - result[0].getRow(), columns
+ 1 - result[0].getColumn());
}
for (int i = 1; i <= 3; i++) {
remove(result[i]);
}
if (rows % 2 == 1) {
coordinates.add(centre);
}
return result;
}
void remove(Coordinate coord) {
int row = coord.getRow();
int column = coord.getColumn();
boolean finished;
finished = false;
for (int index = 0; !finished && (index < coordinates.size()); index++) {
Coordinate c = coordinates.get(index);
if ((c.getRow() == row) && (c.getColumn() == column)) {
coordinates.remove(index);
finished = true;
}
}
}
public Coordinate[] getRandomArray() {
Coordinate[] result = new Coordinate[coordinates.size()];
Coordinate[] group;
int pos = 0;
while (coordinates.size() > 0) {
if (coordinates.size() >= 400) {
group = getGroup();
for (int i = 0; i < group.length; i++) {
result[pos++] = group[i];
}
} else {
result[pos++] = getCoordinate();
}
}
return result;
}
public Coordinate[] getSpreadArray() {
RandomPermutation rpr, rpc;
rpr = new RandomPermutation(rows);
rpc = new RandomPermutation(columns);
Coordinate[] result = new Coordinate[rows];
for (int i = 1; i <= rows; i++) {
result[i - 1] = new Coordinate(rpr.permute(i), rpc.permute(i));
}
return result;
}
public Coordinate getCoordinate() {
Coordinate result;
int index;
index = random.nextInt(coordinates.size());
result = coordinates.get(index);
coordinates.remove(index);
return result;
}
List<Coordinate> coordinates;
Random random;
int rows, columns;
}
package org.sat4j.apps.sudoku;
public enum EditCommand {
COPY_PUZZLE, PASTE_PUZZLE
}
package org.sat4j.apps.sudoku;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
public class EditCommandHandler extends CommandHandler<EditCommand> {
public EditCommandHandler(SuDoku sudoku) {
super(sudoku);
}
void writeToClipboard(String writeMe) {
// get the system clipboard
Clipboard systemClipboard = Toolkit.getDefaultToolkit()
.getSystemClipboard();
// set the textual content on the clipboard to our
// Transferable object
// we use the
Transferable transferableText = new StringSelection(writeMe);
systemClipboard.setContents(transferableText, null);
}
String readFromClipboard() {
String str = "";
Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
try {
str = (String) c.getContents(this).getTransferData(
DataFlavor.stringFlavor);
if (str == null) {
str = "";
}
} catch (Exception e) {
str = "";
}
return str;
}
@Override
public void execute(Enum<EditCommand> command) {
switch ((EditCommand) command) {
case COPY_PUZZLE:
String toCopy = sudoku.getGui().getCellGrid().exportString();
if (sudoku.getMainProgramWindow().isApplication()) {
writeToClipboard(toCopy);
} else {
sudoku.getGui().setPuzzlePaste(toCopy);
}
break;
case PASTE_PUZZLE:
String toPaste;
if (sudoku.getMainProgramWindow().isApplication()) {
toPaste = readFromClipboard();
} else {
toPaste = sudoku.getGui().getPuzzlePaste();
}
CellGrid cellGrid = sudoku.getGui().getCellGrid();
cellGrid.clearProtection();
cellGrid.clearAll();
sudoku.getGui().getCellGrid().importString(toPaste);
sudoku.getGui().setProtection(true);
break;
}
}
}
package org.sat4j.apps.sudoku;
public class EditMenu extends GeneralMenu<EditCommand> {
/**
*
*/
private static final long serialVersionUID = 1L;
EditMenu(CommandHandler<EditCommand> commandHandler, SuDoku suDoku) {
super("EDIT_MENU_LABEL", new ButtonHandlerFactory<EditCommand>(commandHandler),
suDoku);
addMenuItem("EDIT_COPY_PUZZLE", EditCommand.COPY_PUZZLE);
addMenuItem("EDIT_PASTE_PUZZLE", EditCommand.PASTE_PUZZLE);
}
}
package org.sat4j.apps.sudoku;
public enum FileCommand {
IMPORT_PUZZLE, EXPORT_PUZZLE, IMPORT_CNF_MODEL, EXPORT_CNF_INSTANCE, PAGE_SETUP, PRINT, EXIT
}
package org.sat4j.apps.sudoku;
import java.awt.print.PageFormat;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
public class FileCommandHandler extends CommandHandler<FileCommand> {
public FileCommandHandler(SuDoku sudoku) {
super(sudoku);
pageFormat = new PageFormat();
pageFormat.setPaper(new A4Paper());
}
void importPuzzle() {
MainProgramWindow mainProgramWindow;
mainProgramWindow = sudoku.getMainProgramWindow();
if (mainProgramWindow.isApplet()) {
sudoku.getGui().setResult(
"Can't read files from an applet - use Edit menu");
} else if (!mainProgramWindow.fileAccess()) {
sudoku.getGui().setResult("No access to files");
} else {
String toImport = mainProgramWindow.readFile();
CellGrid cellGrid = sudoku.getGui().getCellGrid();
cellGrid.clearProtection();
cellGrid.clearAll();
sudoku.getGui().getCellGrid().importString(toImport);
sudoku.getGui().setProtection(true);
}
}
void exportPuzzle() {
MainProgramWindow mainProgramWindow;
mainProgramWindow = sudoku.getMainProgramWindow();
if (mainProgramWindow.isApplet()) {
sudoku.getGui().setResult(
"Can't write to files from an applet - use Edit menu");
} else if (!mainProgramWindow.fileAccess()) {
sudoku.getGui().setResult("No access to files");
} else {
String toExport = sudoku.getGui().getCellGrid().exportString();
mainProgramWindow.writeFile(toExport);
}
}
void importModel() {
MainProgramWindow mainProgramWindow;
mainProgramWindow = sudoku.getMainProgramWindow();
if (mainProgramWindow.isApplet()) {
sudoku.getGui().setResult(
"Can't read files from an applet - use tab \"cnf\"");
} else if (!mainProgramWindow.fileAccess()) {
sudoku.getGui().setResult("No access to files");
} else {
String toImport = mainProgramWindow.readFile();
sudoku.getGui().setCNFModel(toImport);
CellGrid cellGrid = sudoku.getGui().getCellGrid();
cellGrid.clearComputers();
sudoku.interpretModel(cellGrid);
}
}
void exportInstance() {
MainProgramWindow mainProgramWindow;
mainProgramWindow = sudoku.getMainProgramWindow();
if (mainProgramWindow.isApplet()) {
sudoku.getGui().setResult(
"Can't write to files from an applet - use tab \"cnf\"");
} else if (!mainProgramWindow.fileAccess()) {
sudoku.getGui().setResult("No access to files");
} else {
sudoku.fullCNF(sudoku.getGui().getCellGrid());
String toExport = sudoku.getGui().getCNFFile();
mainProgramWindow.writeFile(toExport);
}
}
void processPrint() {
try {
PrinterJob printerJob = PrinterJob.getPrinterJob();
printerJob.setPrintable(sudoku.getGui().getCellGrid(), pageFormat);
if (printerJob.printDialog()) {
printerJob.print();
}
} catch (PrinterException e) {
e.printStackTrace();
}
}
void processPageSetup() {
PrinterJob printerJob = PrinterJob.getPrinterJob();
pageFormat = printerJob.pageDialog(pageFormat);
}
@Override
public void execute(Enum<FileCommand> command) {
switch ((FileCommand) command) {
case IMPORT_PUZZLE:
importPuzzle();
break;
case EXPORT_PUZZLE:
exportPuzzle();
break;
case IMPORT_CNF_MODEL:
importModel();
break;
case EXPORT_CNF_INSTANCE:
exportInstance();
break;
case PAGE_SETUP:
processPageSetup();
break;
case PRINT:
processPrint();
break;
case EXIT:
if (sudoku.getMainProgramWindow().isApplet()) {
sudoku.getGui().setResult("Can't exit from an applet");
} else {
System.exit(0);
}
break;
}
}
PageFormat pageFormat;
}
package org.sat4j.apps.sudoku;
public class FileMenu extends GeneralMenu<FileCommand> {
/**
*
*/
private static final long serialVersionUID = 1L;
FileMenu(CommandHandler<FileCommand> commandHandler, SuDoku suDoku) {
super("FILE_MENU_LABEL", new ButtonHandlerFactory<FileCommand>(commandHandler),
suDoku);
addMenuItem("FILE_IMPORT_PUZZLE", FileCommand.IMPORT_PUZZLE);
addMenuItem("FILE_EXPORT_PUZZLE", FileCommand.EXPORT_PUZZLE);
addSeparator();
addMenuItem("FILE_IMPORT_MODEL", FileCommand.IMPORT_CNF_MODEL);
addMenuItem("FILE_EXPORT_CNF", FileCommand.EXPORT_CNF_INSTANCE);
addSeparator();
addMenuItem("FILE_PAGE_SETUP", FileCommand.PAGE_SETUP);
addMenuItem("FILE_PRINT", FileCommand.PRINT);
addSeparator();
addMenuItem("FILE_EXIT", FileCommand.EXIT);
}
}
This diff is collapsed.
package org.sat4j.apps.sudoku;
import javax.swing.ImageIcon;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
class GeneralMenu<T extends Enum<T>> extends JMenu {
/**
*
*/
private static final long serialVersionUID = 1L;
GeneralMenu(String title, ButtonHandlerFactory<T> buttonHandlerFactory,
SuDoku suDoku) {
super(suDoku.getSuDokuResources().getStringFromKey(title));
this.suDoku = suDoku;
this.buttonHandlerFactory = buttonHandlerFactory;
}
JMenuItem addMenuItem(String key, Enum<T> command) {
String name = suDoku.getSuDokuResources().getStringFromKey(key);
ImageIcon icon = suDoku.getSuDokuResources().getIconFromKey(key);
JMenuItem menuItem = new JMenuItem(name, icon);
menuItem.addActionListener(buttonHandlerFactory
.newButtonHandler(command));
add(menuItem);
return menuItem;
}
private SuDoku suDoku;
private ButtonHandlerFactory<T> buttonHandlerFactory;
}
package org.sat4j.apps.sudoku;
public enum HelpCommand {
SUDOKU, COMPLETE_SUDOKU, SAT4J, ABOUT
}