[Picross-commit] SF.net SVN: picross: [10] trunk/src/picross
Status: Pre-Alpha
Brought to you by:
yvan_norsa
From: <yva...@us...> - 2007-06-06 08:27:55
|
Revision: 10 http://picross.svn.sourceforge.net/picross/?rev=10&view=rev Author: yvan_norsa Date: 2007-06-06 01:27:54 -0700 (Wed, 06 Jun 2007) Log Message: ----------- code cleanup, comments Modified Paths: -------------- trunk/src/picross/PicrossController.java trunk/src/picross/PicrossMediator.java trunk/src/picross/PicrossModel.java trunk/src/picross/PicrossUI.java trunk/src/picross/grid/FillCommand.java trunk/src/picross/grid/PicrossGridController.java trunk/src/picross/grid/PicrossGridMediator.java trunk/src/picross/grid/PicrossGridModel.java trunk/src/picross/grid/PicrossGridUI.java Modified: trunk/src/picross/PicrossController.java =================================================================== --- trunk/src/picross/PicrossController.java 2007-06-05 14:44:56 UTC (rev 9) +++ trunk/src/picross/PicrossController.java 2007-06-06 08:27:54 UTC (rev 10) @@ -43,7 +43,7 @@ * * @author Y. Norsa */ -class PicrossController extends Controller { +final class PicrossController extends Controller { /*** Static field ***/ /** The class's logger. */ Modified: trunk/src/picross/PicrossMediator.java =================================================================== --- trunk/src/picross/PicrossMediator.java 2007-06-05 14:44:56 UTC (rev 9) +++ trunk/src/picross/PicrossMediator.java 2007-06-06 08:27:54 UTC (rev 10) @@ -36,7 +36,7 @@ import fr.cle.mmvcs.Mediateur; import fr.cle.mmvcs.SimpleEvent; -import org.apache.log4j.Logger; +//import org.apache.log4j.Logger; import picross.grid.PicrossGridController; import picross.grid.PicrossGridMediator; @@ -46,11 +46,11 @@ * * @author Y. Norsa */ -class PicrossMediator extends Mediateur { +final class PicrossMediator extends Mediateur { /*** Static field ***/ /** Class' logger. */ - private static Logger log = Logger.getLogger(PicrossMediator.class); + //private static Logger log = Logger.getLogger(PicrossMediator.class); /*** Constructor ***/ @@ -75,7 +75,7 @@ /** {@inheritDoc} */ public void eventPerformed(SimpleEvent e) { - PicrossMediator.log.debug("eventPerformed(" + e + ")"); + //PicrossMediator.log.debug("eventPerformed(" + e + ")"); String cmd = e.getCommandName(); Modified: trunk/src/picross/PicrossModel.java =================================================================== --- trunk/src/picross/PicrossModel.java 2007-06-05 14:44:56 UTC (rev 9) +++ trunk/src/picross/PicrossModel.java 2007-06-06 08:27:54 UTC (rev 10) @@ -38,7 +38,7 @@ * * @author Y. Norsa */ -class PicrossModel { +final class PicrossModel { /*** Fields ***/ /** Grid width. */ Modified: trunk/src/picross/PicrossUI.java =================================================================== --- trunk/src/picross/PicrossUI.java 2007-06-05 14:44:56 UTC (rev 9) +++ trunk/src/picross/PicrossUI.java 2007-06-06 08:27:54 UTC (rev 10) @@ -43,7 +43,7 @@ * * @author Y. Norsa */ -class PicrossUI extends JFrame { +final class PicrossUI extends JFrame { /*** Constant ***/ /** Serialisation ID. */ Modified: trunk/src/picross/grid/FillCommand.java =================================================================== --- trunk/src/picross/grid/FillCommand.java 2007-06-05 14:44:56 UTC (rev 9) +++ trunk/src/picross/grid/FillCommand.java 2007-06-06 08:27:54 UTC (rev 10) @@ -40,7 +40,7 @@ * * @author Y. Norsa */ -class FillCommand extends Command { +final class FillCommand extends Command { /*** Fields ***/ /** Row of the box to fill. */ Modified: trunk/src/picross/grid/PicrossGridController.java =================================================================== --- trunk/src/picross/grid/PicrossGridController.java 2007-06-05 14:44:56 UTC (rev 9) +++ trunk/src/picross/grid/PicrossGridController.java 2007-06-06 08:27:54 UTC (rev 10) @@ -42,14 +42,14 @@ import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; -import org.apache.log4j.Logger; +//import org.apache.log4j.Logger; /** * Grid controller. * * @author Y. Norsa */ -public class PicrossGridController extends Controller +public final class PicrossGridController extends Controller implements MouseListener, MouseMotionListener { /*** Constants ***/ @@ -60,15 +60,19 @@ /** Command indicating the grid is filled. */ public static final String GRID_FILLED_CMD = "GRID_FILLED_CMD"; + /** Command indicating the action has reached its end. */ static final String END_ACTION_CMD = "END_ACTION_CMD"; + /** Command to check a box. */ static final String CHECK_CMD = "CHECK_CMD"; + + /** Command to uncheck a box. */ static final String UNCHECK_CMD = "UNCHECK_CMD"; /*** Static field ***/ /** The class' logger. */ - private static Logger log = Logger.getLogger(PicrossGridController.class); + //private static Logger log = Logger.getLogger(PicrossGridController.class); /*** Field ***/ @@ -79,7 +83,7 @@ /** {@inheritDoc} */ public void eventPerformed(SimpleEvent e) { - PicrossGridController.log.debug("eventPerformed(" + e + ")"); + //PicrossGridController.log.debug("eventPerformed(" + e + ")"); String cmd = e.getCommandName(); @@ -106,9 +110,7 @@ /*** Methods implanted from the interface MouseListener ***/ /** {@inheritDoc} */ - public void mouseClicked(MouseEvent e) { - this.checkAndFill(e); - } + public void mouseClicked(MouseEvent e) { } /** {@inheritDoc} */ public void mouseEntered(MouseEvent e) { } @@ -121,7 +123,8 @@ /** {@inheritDoc} */ public void mouseReleased(MouseEvent e) { - PicrossGridController.log.debug("mouseReleased()"); + //PicrossGridController.log.debug("mouseReleased()"); + this.checkAndFill(e); this.fireEventPerformed(PicrossGridController.END_ACTION_CMD); } @@ -155,7 +158,7 @@ if (this.view.isInGrid(point)) { int row = this.view.getRow(point); int column = this.view.getColumn(point); - + this.fireEventPerformed(PicrossGridController.FILL_CMD, new FillCommand(row, column)); } Modified: trunk/src/picross/grid/PicrossGridMediator.java =================================================================== --- trunk/src/picross/grid/PicrossGridMediator.java 2007-06-05 14:44:56 UTC (rev 9) +++ trunk/src/picross/grid/PicrossGridMediator.java 2007-06-06 08:27:54 UTC (rev 10) @@ -45,7 +45,7 @@ * * @author Y. Norsa */ -public class PicrossGridMediator extends Mediateur { +public final class PicrossGridMediator extends Mediateur { /*** Static field ***/ /** Class' logger. */ @@ -104,18 +104,30 @@ } } - /*** Method ***/ + /*** Methods ***/ /** Tells the application mediator the grid has been filled. */ void congratulations() { this.fireEventPerformed(PicrossGridController.GRID_FILLED_CMD); } + /** + * Checks a box. + * + * @param row row of the box + * @param column column of the box + */ void check(int row, int column) { this.fireEventPerformed(PicrossGridController.CHECK_CMD, new FillCommand(row, column)); } + /** + * Unchecks a box. + * + * @param row row of the box + * @param column column of the box + */ void uncheck(int row, int column) { this.fireEventPerformed(PicrossGridController.UNCHECK_CMD, new FillCommand(row, column)); Modified: trunk/src/picross/grid/PicrossGridModel.java =================================================================== --- trunk/src/picross/grid/PicrossGridModel.java 2007-06-05 14:44:56 UTC (rev 9) +++ trunk/src/picross/grid/PicrossGridModel.java 2007-06-06 08:27:54 UTC (rev 10) @@ -43,7 +43,12 @@ * * @author Y. Norsa */ -class PicrossGridModel { +final class PicrossGridModel { + /*** Constant ***/ + + /** Empty hint value. */ + static final int EMPTY_HINT = -1; + /*** Static field ***/ /** The class' logger. */ @@ -58,16 +63,20 @@ private boolean[][] data; /** The grid as filled by the user. */ - private Boolean[][] checked; + private DummyBoolean[][] checked; + /** + * The last modified box. Permis to know if we are in a serie or + * a single action. + */ + private DummyBoolean lastChecked = null; + /** Columns hints. */ private int[][] colData; /** Rows hints. */ private int[][] rowData; - private Boolean lastChecked = null; - /*** Constructor ***/ /** @@ -80,26 +89,31 @@ this.mediator = mediator; this.data = data; - this.checked = new Boolean[this.data.length][this.data[0].length]; + this.checked = new DummyBoolean[this.data.length][this.data[0].length]; for (int i = 0; i < this.checked.length; i++) { for (int j = 0; j < this.checked[i].length; j++) { - this.checked[i][j] = Boolean.valueOf(false); + this.checked[i][j] = new DummyBoolean(false); } } - List<List<Integer>> colRawData = new ArrayList<List<Integer>>(); + // Grid of columns hints + List<List<Integer>> colHints = new ArrayList<List<Integer>>(); + + // Largest number of hints for a column int max = 0; for (boolean[] col : data) { List<Integer> current = new ArrayList<Integer>(); + // Current hint int chain = 0; for (boolean cell : col) { if (cell) { chain++; } else if (chain > 0) { + // We've reached the end of a series of checked boxes current.add(chain); chain = 0; } @@ -108,6 +122,7 @@ if (chain > 0) { current.add(chain); } else if (current.size() == 0) { + // If this column is empty, we add a "0" hint current.add(0); } @@ -117,24 +132,48 @@ max = currentSize; } - colRawData.add(current); + colHints.add(current); } + /* + * Final array containing the hints, in the following form : + * + * 1 + * 0 2 1 + * + * Which corresponds to the following grid : + * + * |-----| + * |_|X|_| + * |_|_|X| + * |_|X|_| + * |_|X|_| + * |-----| + */ this.colData = new int[data.length][max]; for (int i = 0; i < max; i++) { - for (int j = 0; j < colRawData.size(); j++) { - if (colRawData.get(j).size() >= (max - i)) { - this.colData[j][max - 1 - i] - = colRawData.get(j).get(colRawData.get(j).size() - - max + i); + // Minimal number of hints for the current column to be considered + int ref = max - i; + + // Current hint row + int currentRow = ref - 1; + + for (int j = 0; j < colHints.size(); j++) { + List<Integer> currentCol = colHints.get(j); + int size = currentCol.size(); + + if (size >= ref) { + this.colData[j][currentRow] = currentCol.get(size - ref); } else { - this.colData[j][max - 1 - i] = -1; + this.colData[j][currentRow] = PicrossGridModel.EMPTY_HINT; } } } - List<List<Integer>> rowRawData = new ArrayList<List<Integer>>(); + // Same operations as for the columns, basically + + List<List<Integer>> rowHints = new ArrayList<List<Integer>>(); max = 0; for (int i = 0; i < data[0].length; i++) { @@ -163,66 +202,104 @@ max = currentSize; } - rowRawData.add(current); + rowHints.add(current); } this.rowData = new int[data[0].length][max]; + int nbRows = rowHints.size(); for (int i = 0; i < max; i++) { - for (int j = 0; j < rowRawData.size(); j++) { - if (rowRawData.get(j).size() >= (max - i)) { - int index = i - Math.abs(rowRawData.get(j).size() - max); - this.rowData[j][i] = rowRawData.get(j).get(index); + int ref = max - i; + + for (int j = 0; j < nbRows; j++) { + List<Integer> currentRow = rowHints.get(j); + int size = currentRow.size(); + + if (size >= ref) { + this.rowData[j][i] = + currentRow.get(i - Math.abs(size - max)); } else { - this.rowData[j][i] = -1; + this.rowData[j][i] = PicrossGridModel.EMPTY_HINT; } } } } - /*** Method ***/ + /*** Methods ***/ /** - * Indicates a box has been checked. + * Method called during an action. * * @param row row of the box * @param column column of the box */ void checkBox(int row, int column) { - if (this.lastChecked == null || - !this.lastChecked.equals(this.checked[row][column])) { + //PicrossGridModel.log.debug("checkBox(" + row + ", " + column + ")"); + //PicrossGridModel.log.debug("lastChecked == null : " + // + (lastChecked == null)); - if (!this.checked[row][column]) { - //PicrossGridModel.log.debug("checking " + row + "," + column); + /* + * If we are trying to check the last box we just checked + * (while dragging), do nothing + */ + if (this.lastChecked != null + && this.lastChecked == this.checked[row][column]) { - this.checked[row][column] = true; - this.lastChecked = this.checked[row][column]; + return; + } - this.mediator.check(row, column); + boolean currentValue = this.checked[row][column].getValue(); - boolean completed = true; + /* + * The value of the last checked box tells us if we are in + * a checks serie or a uncheck serie + */ + boolean lastValue = false; - for (int i = 0; i < this.data.length; i++) { - for (int j = 0; j < this.data[i].length; j++) { - if (this.data[i][j] != this.checked[i][j]) { - completed = false; - break; - } - } - } + if (this.lastChecked != null) { + lastValue = this.lastChecked.getValue(); - if (completed) { - this.mediator.congratulations(); + /* + * If we are in a box which is in the same state as our aim + * (if we are in a checks serie and we are on a box which + * is already checked), do nothing + */ + if (currentValue == lastValue) { + return; + } + } + + if (!currentValue && (this.lastChecked == null || lastValue)) { + this.checked[row][column].setValue(true); + this.mediator.check(row, column); + } else if (currentValue && (this.lastChecked == null || !lastValue)) { + this.checked[row][column].setValue(false); + this.mediator.uncheck(row, column); + } + + this.lastChecked = this.checked[row][column]; + this.checkCompleted(); + } + + /** Checks wether the grid is finished. */ + private void checkCompleted() { + boolean completed = true; + + for (int i = 0; i < this.data.length; i++) { + for (int j = 0; j < this.data[i].length; j++) { + if (this.data[i][j] != this.checked[i][j].getValue()) { + completed = false; + break; } - } else { - this.checked[row][column] = false; - this.lastChecked = this.checked[row][column]; - - this.mediator.uncheck(row, column); } } + + if (completed) { + this.mediator.congratulations(); + } } + /** Indicates the current action has come to an end. */ void endAction() { this.lastChecked = null; } @@ -246,5 +323,49 @@ int[][] getRowData() { return this.rowData; } + + /** + * This class is a wrapper for boolean, allowing us to do reference + * comparisons. + * + * @author Y. Norsa + */ + private static final class DummyBoolean { + /*** Field ***/ + + /** The value being held. */ + private boolean value; + + /*** Constructor ***/ + + /** + * Constructor. + * + * @param value the value to hold + */ + private DummyBoolean(boolean value) { + this.value = value; + } + + /*** Accessors ***/ + + /** + * Returns the value. + * + * @return held value + */ + private boolean getValue() { + return this.value; + } + + /** + * Allows to set the value. + * + * @param value new value to hold + */ + private void setValue(boolean value) { + this.value = value; + } + } } Modified: trunk/src/picross/grid/PicrossGridUI.java =================================================================== --- trunk/src/picross/grid/PicrossGridUI.java 2007-06-05 14:44:56 UTC (rev 9) +++ trunk/src/picross/grid/PicrossGridUI.java 2007-06-06 08:27:54 UTC (rev 10) @@ -40,14 +40,14 @@ import javax.swing.JPanel; -import org.apache.log4j.Logger; +//import org.apache.log4j.Logger; /** * Grid UI. * * @author Y. Norsa */ -class PicrossGridUI extends JPanel { +final class PicrossGridUI extends JPanel { /*** Constants ***/ /** Serialisation ID. */ @@ -83,7 +83,7 @@ /*** Static field ***/ /** Class' logger. */ - private static Logger log = Logger.getLogger(PicrossGridUI.class); + //private static Logger log = Logger.getLogger(PicrossGridUI.class); /*** Fields ***/ @@ -114,6 +114,7 @@ /** Filled boxes. */ private boolean[][] filled; + /** Controller attached to this view. */ private transient PicrossGridController controller; /*** Constructor ***/ @@ -161,8 +162,8 @@ this.bottomBoundary = this.topBoundary + (this.height * PicrossGridUI.BOX_HEIGHT); - PicrossGridUI.log.debug("rightBoundary : " + this.rightBoundary); - PicrossGridUI.log.debug("bottomBoundary : " + this.bottomBoundary); + //PicrossGridUI.log.debug("rightBoundary : " + this.rightBoundary); + //PicrossGridUI.log.debug("bottomBoundary : " + this.bottomBoundary); this.setPreferredSize(new Dimension(this.rightBoundary + PicrossGridUI.RIGHT_SPACE, @@ -183,7 +184,7 @@ x = this.leftBoundary + PicrossGridUI.COL_HINT_WIDTH; for (int j = 0; j < this.colData.length; j++) { - if (this.colData[j][i] != -1) { + if (this.colData[j][i] != PicrossGridModel.EMPTY_HINT) { g.drawString(String.valueOf(this.colData[j][i]), x, y); } @@ -200,7 +201,7 @@ x = 0; for (int j = 0; j < this.rowData[i].length; j++) { - if (this.rowData[i][j] != -1) { + if (this.rowData[i][j] != PicrossGridModel.EMPTY_HINT) { g.drawString(String.valueOf(this.rowData[i][j]), x, y + PicrossGridUI.ROW_HINT_HEIGHT); } @@ -283,19 +284,42 @@ return (int) (x / PicrossGridUI.BOX_WIDTH); } - + + /** Removes the listeners to disable the grid. */ void disableGrid() { this.removeMouseListener(this.controller); this.removeMouseMotionListener(this.controller); } + /** + * Checks a row. + * + * @param row row of the box + * @param column column of the box + */ void check(int row, int column) { - this.filled[row][column] = true; - this.repaint(); + this.setBoxState(row, column, true); } + /** + * Unchecks a row. + * + * @param row row of the box + * @param column column of the box + */ void uncheck(int row, int column) { - this.filled[row][column] = false; + this.setBoxState(row, column, false); + } + + /** + * Modifies a box and repaints the grid. + * + * @param row row of the box + * @param column column of the box + * @param state new state of the box + */ + private void setBoxState(int row, int column, boolean state) { + this.filled[row][column] = state; this.repaint(); } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |