[Picross-commit] SF.net SVN: picross: [32] trunk/src/picross
Status: Pre-Alpha
Brought to you by:
yvan_norsa
From: <yva...@us...> - 2007-06-12 14:21:44
|
Revision: 32 http://picross.svn.sourceforge.net/picross/?rev=32&view=rev Author: yvan_norsa Date: 2007-06-12 07:21:43 -0700 (Tue, 12 Jun 2007) Log Message: ----------- visually marks completed hints Modified Paths: -------------- trunk/src/picross/PicrossModel.java trunk/src/picross/grid/GridController.java trunk/src/picross/grid/GridMediator.java trunk/src/picross/grid/GridModel.java trunk/src/picross/grid/GridUI.java Added Paths: ----------- trunk/src/picross/grid/ClearCommand.java trunk/src/picross/grid/CompletedCommand.java Modified: trunk/src/picross/PicrossModel.java =================================================================== --- trunk/src/picross/PicrossModel.java 2007-06-11 10:43:20 UTC (rev 31) +++ trunk/src/picross/PicrossModel.java 2007-06-12 14:21:43 UTC (rev 32) @@ -58,13 +58,7 @@ this.height = 14; this.data = new boolean[this.width][this.height]; - /* - for (int i = 0; i < this.width; i++) { - for (int j = 0; j < this.height; j++) { - this.data[i][j] = true; - } - } - */ + this.data[0][7] = true; this.data[0][8] = true; this.data[0][13] = true; Added: trunk/src/picross/grid/ClearCommand.java =================================================================== --- trunk/src/picross/grid/ClearCommand.java (rev 0) +++ trunk/src/picross/grid/ClearCommand.java 2007-06-12 14:21:43 UTC (rev 32) @@ -0,0 +1,93 @@ +/* + * $Id$ + * + * Copyright (c) 2007 + * + * This software is governed by the CeCILL license under French law and + * abiding by the rules of distribution of free software. You can use, + * modify and/ or redistribute the software under the terms of the CeCILL + * license as circulated by CEA, CNRS and INRIA at the following URL + * "http://www.cecill.info". + * + * As a counterpart to the access to the source code and rights to copy, + * modify and redistribute granted by the license, users are provided only + * with a limited warranty and the software's author, the holder of the + * economic rights, and the successive licensors have only limited + * liability. + * + * In this respect, the user's attention is drawn to the risks associated + * with loading, using, modifying and/or developing or reproducing the + * software by the user in light of its specific status of free software, + * that may mean that it is complicated to manipulate, and that also + * therefore means that it is reserved for developers and experienced + * professionals having in-depth computer knowledge. Users are therefore + * encouraged to load and test the software's suitability as regards their + * requirements in conditions enabling the security of their systems and/or + * data to be ensured and, more generally, to use and operate it in the + * same conditions as regards security. + * + * The fact that you are presently reading this means that you have had + * knowledge of the CeCILL license and that you accept its terms. + */ + + +package picross.grid; + +import fr.cle.mmvcs.Command; + +/** + * Command asking to clear hints. + * + * @author Y. Norsa + */ +final class ClearCommand extends Command { + /*** Constants ***/ + + /** Clear a column. */ + static final int COLUMN = 0; + + /** Clear a row. */ + static final int ROW = 1; + + /*** Fields ***/ + + /** Type of hints to clear. */ + private int type; + + /** Number of the column or row. */ + private int pos; + + /*** Constructor ***/ + + /** + * Constructor. + * + * @param type type of hints to clear + * @param pos number of the column or row + */ + ClearCommand(int type, int pos) { + this.type = type; + this.pos = pos; + } + + /*** Accessors ***/ + + /** + * Returns the type of hints to clear. + * + * @return type of hints to clear + */ + int getType() { + return this.type; + } + + /** + * Returns the position of the column or row. + * + * @return position of the number or row + */ + int getPos() { + return this.pos; + } +} + Property changes on: trunk/src/picross/grid/ClearCommand.java ___________________________________________________________________ Name: svn:keywords + Id Added: trunk/src/picross/grid/CompletedCommand.java =================================================================== --- trunk/src/picross/grid/CompletedCommand.java (rev 0) +++ trunk/src/picross/grid/CompletedCommand.java 2007-06-12 14:21:43 UTC (rev 32) @@ -0,0 +1,107 @@ +/* + * $Id$ + * + * Copyright (c) 2007 + * + * This software is governed by the CeCILL license under French law and + * abiding by the rules of distribution of free software. You can use, + * modify and/ or redistribute the software under the terms of the CeCILL + * license as circulated by CEA, CNRS and INRIA at the following URL + * "http://www.cecill.info". + * + * As a counterpart to the access to the source code and rights to copy, + * modify and redistribute granted by the license, users are provided only + * with a limited warranty and the software's author, the holder of the + * economic rights, and the successive licensors have only limited + * liability. + * + * In this respect, the user's attention is drawn to the risks associated + * with loading, using, modifying and/or developing or reproducing the + * software by the user in light of its specific status of free software, + * that may mean that it is complicated to manipulate, and that also + * therefore means that it is reserved for developers and experienced + * professionals having in-depth computer knowledge. Users are therefore + * encouraged to load and test the software's suitability as regards their + * requirements in conditions enabling the security of their systems and/or + * data to be ensured and, more generally, to use and operate it in the + * same conditions as regards security. + * + * The fact that you are presently reading this means that you have had + * knowledge of the CeCILL license and that you accept its terms. + */ + + +package picross.grid; + +import fr.cle.mmvcs.Command; + +/** + * Command indicating that a hint has been completed. + * + * @author Y. Norsa + */ +final class CompletedCommand extends Command { + /*** Constants ***/ + + /** A column hint has been completed. */ + static final int COLUMN = 0; + + /** A row hint has been completed. */ + static final int ROW = 1; + + /*** Fields ***/ + + /** Type of hint that has been completed. */ + private int type; + + /** Number of the column or row. */ + private int position; + + /** Position of the hint in the column or row. */ + private int hintPos; + + /*** Constructor ***/ + + /** + * Constructor. + * + * @param type type of hint that has been completed + * @param position number of the column or row + * @param hintPos position of the hint in the column or row + */ + CompletedCommand(int type, int position, int hintPos) { + this.type = type; + this.position = position; + this.hintPos = hintPos; + } + + /*** Accessors ***/ + + /** + * Returns the type of hint that has been completed. + * + * @return type of hint that has been completed + */ + int getType() { + return this.type; + } + + /** + * Returns the number of the column or row. + * + * @return number of the column or row + */ + int getPosition() { + return this.position; + } + + /** + * Returns the position of the hint in the column or row. + * + * @return position of the hint in the column or row + */ + int getHintPos() { + return this.hintPos; + } +} + Property changes on: trunk/src/picross/grid/CompletedCommand.java ___________________________________________________________________ Name: svn:keywords + Id Modified: trunk/src/picross/grid/GridController.java =================================================================== --- trunk/src/picross/grid/GridController.java 2007-06-11 10:43:20 UTC (rev 31) +++ trunk/src/picross/grid/GridController.java 2007-06-12 14:21:43 UTC (rev 32) @@ -63,15 +63,15 @@ /** 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"; - /** Command asking to repaint part of the grid. */ static final String PAINT_CMD = "PAINT_CMD"; + /** Command indicating that a hint has been completed. */ + static final String COMPLETED_CMD = "COMPLETED_CMD"; + + /** Command asking to clear the completed hints. */ + static final String CLEAR_HINTS_CMD = "CLEAR_HINTS_CMD"; + /** Checking a box. */ static final int CHECK_ACTION = 0; @@ -104,8 +104,35 @@ if (cmd.equals(GridController.PAINT_CMD)) { PaintCommand command = (PaintCommand) e.getCommand(); this.view.repaint(command.getRow(), command.getColumn()); + return; } + + if (cmd.equals(GridController.COMPLETED_CMD)) { + CompletedCommand command = (CompletedCommand) e.getCommand(); + + if (command.getType() == CompletedCommand.COLUMN) { + this.view.completedColHint(command.getPosition(), + command.getHintPos()); + } else { //if (command.getType() == CompletedCommand.ROW) { + this.view.completedRowHint(command.getPosition(), + command.getHintPos()); + } + + return; + } + + if (cmd.equals(GridController.CLEAR_HINTS_CMD)) { + ClearCommand command = (ClearCommand) e.getCommand(); + + if (command.getType() == ClearCommand.COLUMN) { + this.view.clearColHints(command.getPos()); + } else { //if (command.getType() == ClearCommand.ROW) { + this.view.clearRowHints(command.getPos()); + } + + return; + } } /*** Methods implanted from the interface MouseListener ***/ Modified: trunk/src/picross/grid/GridMediator.java =================================================================== --- trunk/src/picross/grid/GridMediator.java 2007-06-11 10:43:20 UTC (rev 31) +++ trunk/src/picross/grid/GridMediator.java 2007-06-12 14:21:43 UTC (rev 32) @@ -124,6 +124,52 @@ new PaintCommand(row, column)); } + /** + * Asks to clear a column's hints. + * + * @param column column number + */ + void clearColHints(int column) { + this.fireEventPerformed(GridController.CLEAR_HINTS_CMD, + new ClearCommand(ClearCommand.COLUMN, + column)); + } + + /** + * Ask to mark a column hint as completed. + * + * @param column column number + * @param hintPos position of the hint + */ + void completedColHint(int column, int hintPos) { + this.fireEventPerformed(GridController.COMPLETED_CMD, + new CompletedCommand(CompletedCommand.COLUMN, + column, hintPos)); + } + + /** + * Asks to clear a row's hints. + * + * @param row row number + */ + void clearRowHints(int row) { + this.fireEventPerformed(GridController.CLEAR_HINTS_CMD, + new ClearCommand(ClearCommand.ROW, + row)); + } + + /** + * Asks to mark a row hint as completed. + * + * @param row row number + * @param hintPos position of the hint + */ + void completedRowHint(int row, int hintPos) { + this.fireEventPerformed(GridController.COMPLETED_CMD, + new CompletedCommand(CompletedCommand.ROW, + row, hintPos)); + } + /*** Accessor ***/ /** Modified: trunk/src/picross/grid/GridModel.java =================================================================== --- trunk/src/picross/grid/GridModel.java 2007-06-11 10:43:20 UTC (rev 31) +++ trunk/src/picross/grid/GridModel.java 2007-06-12 14:21:43 UTC (rev 32) @@ -225,10 +225,67 @@ } } + /*** Static methods ***/ + + /** + * Returns the index of the first non-empty hint. + * + * @param hints hints array + * @return index of the first non-empty hint + */ + private static int getFirstHintIndex(int[] hints) { + return GridModel.getNextHintIndex(0, hints); + } + + /** + * Returns the index of the next non-empty hint. + * + * @param currentIndex start index + * @param hints hints array + * @return index of the next non-empty hint + */ + private static int getNextHintIndex(int currentIndex, int[] hints) { + for (int i = currentIndex; i < hints.length; i++) { + if (hints[i] != GridModel.EMPTY_HINT) { + return i; + } + } + + return GridModel.EMPTY_HINT; + } + + /** + * Returns the index of the last non-empty hint. + * + * @param hints hints array + * @return index of the last non-empty hint + */ + private static int getLastHintIndex(int[] hints) { + return GridModel.getPreviousHintIndex(hints.length - 1, hints); + } + + /** + * Returns the index of the previous non-empty hint. + * + * @param currentIndex start index + * @param hints hints array + * @return index of the previous non-empty hint + */ + private static int getPreviousHintIndex(int currentIndex, int[] hints) { + for (int i = currentIndex; i >= 0; i--) { + if (hints[i] != GridModel.EMPTY_HINT) { + return i; + } + } + + return GridModel.EMPTY_HINT; + } + /*** Methods ***/ /** * Method called during an action. + * TODO rename this method * * @param row row of the box * @param column column of the box @@ -297,9 +354,282 @@ } this.lastModified = this.boxes[column][row]; + this.checkColumn(column); + this.checkRow(row); this.checkCompleted(); } + /** + * Checks if a hint has been completed in a row. + * + * @param row row number to check + */ + private void checkRow(int row) { + // Contains the completed hints + List<Integer> completedHints = new ArrayList<Integer>(); + + // Current hint we're looking to complete + int currentHintIndex = + GridModel.getFirstHintIndex(this.rowData[row]); + + // First from left to right + int currentHint = 0; + + // If the first box is empty, do nothing this time + if (!this.boxes[currentHint][row].isEmpty()) { + // Current chain of checked boxes + int currentChain = 0; + + while (currentHint < this.boxes.length) { + if (this.boxes[currentHint][row].isChecked()) { + currentChain++; + } else { + // We reach the end of a chain + + // And it matches the current hint + if (this.rowData[row][currentHintIndex] + == currentChain) { + + completedHints.add(currentHintIndex); + currentChain = 0; + + currentHintIndex = + GridModel.getNextHintIndex(currentHintIndex + 1, + this.rowData[row]); + + if (currentHintIndex == GridModel.EMPTY_HINT) { + /* + * If this is the last hint, we verify that + * there aren't any extra checked boxes + */ + for (int i = currentHint; i < this.boxes.length; + i++) { + + if (this.boxes[i][row].isChecked()) { + /* + * If this is the case, + * we cancel everything + */ + completedHints.clear(); + break; + } + } + + break; + } + } + + // If there is a blank after a filled hint, we stop there + if (this.boxes[currentHint][row].isEmpty()) { + break; + } + } + + currentHint++; + } + + /* + * If we've reached the end of the row + * and there is a current chain, we mark it as completed + */ + if (currentHintIndex != -1 + && this.rowData[row][currentHintIndex] == currentChain) { + + completedHints.add(currentHintIndex); + currentChain = 0; + } + } + + // Same thing from right to left + currentHintIndex = GridModel.getLastHintIndex(this.rowData[row]); + + currentHint = this.boxes.length - 1; + + if (!this.boxes[currentHint][row].isEmpty()) { + int currentChain = 0; + + while (currentHint >= 0) { + if (this.boxes[currentHint][row].isChecked()) { + currentChain++; + } else { + if (this.rowData[row][currentHintIndex] + == currentChain) { + + completedHints.add(currentHintIndex); + currentChain = 0; + + currentHintIndex = + GridModel.getPreviousHintIndex(currentHintIndex + - 1, + this.rowData[row]); + + if (currentHintIndex == -1) { + for (int i = currentHint; i >= 0; i--) { + if (this.boxes[i][row].isChecked()) { + completedHints.clear(); + } + } + + break; + } + } + + if (this.boxes[currentHint][row].isEmpty()) { + break; + } + } + + currentHint--; + } + } + + this.mediator.clearRowHints(row); + + for (int index : completedHints) { + this.mediator.completedRowHint(row, index); + } + } + + /** + * Checks if a hint has been completed in a column. + * + * @param column column number to check + */ + private void checkColumn(int column) { + //GridModel.log.debug("checkColumn(" + column + ")"); + + List<Integer> completedHints = new ArrayList<Integer>(); + /* + for (int i = 0; i < this.colData[column].length; i++) { + GridModel.log.debug("this.colData[" + column + "][" + i + "] = " + + this.colData[column][i]); + } + */ + //GridModel.log.debug("hint courant :" + //+ this.colData[column][currentHintIndex]); + + int currentHintIndex = + GridModel.getLastHintIndex(this.colData[column]); + + int currentHint = 0; + + if (!this.boxes[column][currentHint].isEmpty()) { + int currentChain = 0; + + while (currentHint < this.boxes[0].length) { + //GridModel.log.debug("dans la boucle, currentHint = " + //+ currentHint + ", currentChain = " + currentChain); + + if (this.boxes[column][currentHint].isChecked()) { + currentChain++; + } else { + if (this.colData[column][currentHintIndex] + == currentChain) { + + //GridModel.log.debug("found " + currentChain); + + completedHints.add(currentHintIndex); + currentChain = 0; + + //GridModel.log.debug("currentHintIndex = " + //+ currentHintIndex); + + currentHintIndex = GridModel + .getPreviousHintIndex(currentHintIndex - 1, + this.colData[column]); + + if (currentHintIndex == GridModel.EMPTY_HINT) { + for (int i = currentHint; i < this.boxes[0].length; + i++) { + + if (this.boxes[column][i].isChecked()) { + completedHints.clear(); + break; + } + } + + break; + } + } + + if (this.boxes[column][currentHint].isEmpty()) { + break; + } + } + + currentHint++; + } + + //GridModel.log.debug("fin de la boucle, currentHint = " + //+ currentHint); + + if (currentHintIndex != GridModel.EMPTY_HINT + && this.colData[column][currentHintIndex] == currentChain) { + + //GridModel.log.debug("found " + currentChain); + + completedHints.add(currentHintIndex); + currentChain = 0; + } + + } + + currentHintIndex = GridModel.getFirstHintIndex(this.colData[column]); + + //GridModel.log.debug("hintCourant2 : " + //+ this.colData[column][currentHintIndex]); + + currentHint = this.boxes[0].length - 1; + + if (!this.boxes[column][currentHint].isEmpty()) { + int currentChain = 0; + + while (currentHint >= 0) { + //GridModel.log.debug("dans la boucle2, currentHint = " + //+ currentHint + ", currentChain = " + currentChain); + + if (this.boxes[column][currentHint].isChecked()) { + currentChain++; + } else { + if (this.colData[column][currentHintIndex] + == currentChain) { + + //GridModel.log.debug("found2 " + currentChain); + + completedHints.add(currentHintIndex); + currentChain = 0; + + currentHintIndex = + GridModel.getNextHintIndex(currentHintIndex + 1, + this.colData[column]); + + if (currentHintIndex == GridModel.EMPTY_HINT) { + for (int i = currentHint; i >= 0; i--) { + if (this.boxes[column][i].isChecked()) { + completedHints.clear(); + } + } + + break; + } + } + + if (this.boxes[column][currentHint].isEmpty()) { + break; + } + } + + currentHint--; + } + } + + this.mediator.clearColHints(column); + + for (int index : completedHints) { + this.mediator.completedColHint(column, index); + } + } + /** Checks wether the grid is finished. */ private void checkCompleted() { boolean completed = true; Modified: trunk/src/picross/grid/GridUI.java =================================================================== --- trunk/src/picross/grid/GridUI.java 2007-06-11 10:43:20 UTC (rev 31) +++ trunk/src/picross/grid/GridUI.java 2007-06-12 14:21:43 UTC (rev 32) @@ -76,6 +76,9 @@ /** Color of the hints text. */ private static final Color HINT_TEXT_COLOR = new Color(233, 246, 255); + /** Color of a completed hint text. */ + private static final Color COMPLETED_HINT_COLOR = new Color(208, 215, 217); + /** Font used for the hints. */ private static final Font HINT_FONT = new Font("Sans Serif", Font.BOLD, 10); @@ -186,6 +189,12 @@ /** Boxes blocks. */ private List<Line2D> blocksLines; + /** Completed column hints. */ + private boolean[][] completedCols; + + /** Completed row hints. */ + private boolean[][] completedRows; + /*** Constructor ***/ /** @@ -303,6 +312,9 @@ this.initBlocks(); + this.completedCols = new boolean[this.width][this.colData[0].length]; + this.completedRows = new boolean[this.height][this.rowData[0].length]; + this.setPreferredSize(new Dimension(this.rightBoundary + GridUI.RIGHT_SPACE, this.bottomBoundary @@ -424,7 +436,8 @@ for (int j = 0; j < this.colData.length; j++) { if (this.colData[j][i] != GridModel.EMPTY_HINT) { - this.drawHint(g, this.colData[j][i], x, y); + this.drawHint(g, this.colData[j][i], x, y, + this.completedCols[j][i]); } x += GridUI.BOX_WIDTH; @@ -447,7 +460,8 @@ for (int j = 0; j < this.rowData[i].length; j++) { if (this.rowData[i][j] != GridModel.EMPTY_HINT) { - this.drawHint(g, this.rowData[i][j], x, y); + this.drawHint(g, this.rowData[i][j], x, y, + this.completedRows[i][j]); } x += this.leftHintsDecal; @@ -465,10 +479,16 @@ * @param x X coordinate * @param y Y coordinate */ - private void drawHint(Graphics g, int value, int x, int y) { + private void drawHint(Graphics g, int value, int x, int y, + boolean complete) { this.hintBoxIcon.paintIcon(this, g, x, y); - g.setColor(GridUI.HINT_TEXT_COLOR); + if (complete) { + g.setColor(GridUI.COMPLETED_HINT_COLOR); + } else { + g.setColor(GridUI.HINT_TEXT_COLOR); + } + g.setFont(GridUI.HINT_FONT); y += this.centerHintHeight; @@ -566,4 +586,53 @@ this.repaint(rect); } } + + /** + * Clears a column completed hints. + * + * @param column column number + */ + void clearColHints(int column) { + for (int i = 0; i < this.completedCols[column].length; i++) { + this.completedCols[column][i] = false; + } + + this.repaint(this.topHintsRect); + } + + /** + * Marks a column hint as completed. + * + * @param column the column number + * @param hintPos position of the hint in the column + */ + void completedColHint(int column, int hintPos) { + this.completedCols[column][hintPos] = true; + this.repaint(this.topHintsRect); + } + + /** + * Clears a row completed hints. + * + * @param row row number + */ + void clearRowHints(int row) { + for (int i = 0; i < this.completedRows[row].length; i++) { + this.completedRows[row][i] = false; + } + + this.repaint(this.leftHintsRect); + } + + /** + * Marks a row hint as completed. + * + * @param row the row number + * @param hintPos position of the hint in the row + */ + void completedRowHint(int row, int hintPos) { + //GridUI.log.debug("completedRowHint(" + row + ", " + hintPos + ")"); + this.completedRows[row][hintPos] = true; + this.repaint(this.leftHintsRect); + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |