src/main/java/de/uapcore/sudoku/Field.java

Sat, 25 Jul 2020 15:56:48 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 25 Jul 2020 15:56:48 +0200
changeset 11
f7433671fec5
parent 10
369903afbb29
child 12
1c62c6009161
permissions
-rw-r--r--

missing class documentation

3
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
1 /*
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
2 * Copyright 2013 Mike Becker. All rights reserved.
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
3 *
3
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
4 * Redistribution and use in source and binary forms, with or without
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
5 * modification, are permitted provided that the following conditions are met:
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
6 *
3
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
7 * 1. Redistributions of source code must retain the above copyright
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
8 * notice, this list of conditions and the following disclaimer.
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
9 *
3
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
10 * 2. Redistributions in binary form must reproduce the above copyright
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
11 * notice, this list of conditions and the following disclaimer in the
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
12 * documentation and/or other materials provided with the distribution.
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
13 *
3
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
24 * POSSIBILITY OF SUCH DAMAGE.
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
25 */
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
26
1
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 package de.uapcore.sudoku;
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28
9
576e7a2861ae converts to maven project
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
29 import javax.swing.*;
576e7a2861ae converts to maven project
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
30 import java.awt.*;
1
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31 import java.awt.image.BufferedImage;
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33 /**
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
34 * A panel rendering the Sudoku field.
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
35 * <p>
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
36 * Cells are identified by zero-based indices from top-left to bottom-right.
1
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37 */
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
38 public final class Field extends JPanel {
1
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39 private SudokuTextField[][] cells;
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
40
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
41 /**
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
42 * Constructs a new 9x9 Sudoku grid.
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
43 */
1
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
44 public Field() {
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45 setBackground(Color.WHITE);
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
46
1
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
47 setLayout(new GridBagLayout());
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48 GridBagConstraints c = new GridBagConstraints();
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49 c.insets = new Insets(5, 5, 5, 5);
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
50
1
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51 cells = new SudokuTextField[9][9];
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
52 for (int x = 0; x < 9; x++) {
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
53 for (int y = 0; y < 9; y++) {
1
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 cells[x][y] = new SudokuTextField();
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55 c.gridx = x;
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56 c.gridy = y;
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 add(cells[x][y], c);
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58 }
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 }
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60 }
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
62 /**
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
63 * Paints the grid and all contained cells.
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
64 *
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
65 * @param graphics the graphics context
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
66 */
1
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67 @Override
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68 public void paint(Graphics graphics) {
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69 final int w = getWidth();
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 final int h = getHeight();
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71 final int cw = w / 9;
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72 final int ch = h / 9;
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
73
1
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74 BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
75 Graphics2D g = img.createGraphics();
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
76 g.setBackground(Color.WHITE);
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
77 g.clearRect(0, 0, w, h);
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
78
1
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
79 g.setColor(Color.BLACK);
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
80 g.drawRect(1, 1, w - 2, h - 2);
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
81 g.drawRect(2, 2, w - 4, h - 4);
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
82 for (int x = cw; x < w; x += cw) {
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
83 for (int y = ch; y < h; y += ch) {
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
84 g.drawLine(x, 2, x, h - 2);
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
85 g.drawLine(2, y, w - 2, y);
1
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86 if ((x / cw) % 3 == 0) {
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
87 g.drawLine(x + 1, 2, x + 1, h - 2);
1
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88 }
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
89 if ((y / ch) % 3 == 0) {
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
90 g.drawLine(2, y + 1, w - 2, y + 1);
1
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
91 }
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
92 }
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
93 }
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
94
1
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
95 graphics.drawImage(img, 0, 0, this);
5
8ddf4af937d7 moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
96 super.paintChildren(graphics);
1
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
97 }
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
98
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
99 /**
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
100 * Checks whether a cell is empty
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
101 *
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
102 * @param x horizontal position
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
103 * @param y vertical position
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
104 * @return true if the cell is empty, false otherwise
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
105 */
7
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
106 public boolean isCellEmpty(int x, int y) {
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
107 return getCellValue(x, y) == 0;
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
108 }
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
109
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
110 /**
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
111 * Returns value of a specific cell.
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
112 *
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
113 * @param x horizontal position
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
114 * @param y vertical position
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
115 * @return the cell's value
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
116 */
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
117 public int getCellValue(int x, int y) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
118 return cells[x][y].getValue();
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
119 }
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
120
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
121 /**
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
122 * Sets the value of a specific cell.
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
123 *
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
124 * @param x horizontal position
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
125 * @param y vertical position
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
126 * @param v the cells value
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
127 */
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
128 public void setCellValue(int x, int y, int v) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
129 cells[x][y].setValue(v);
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
130 }
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
131
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
132 /**
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
133 * Clears the value of a specific cell.
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
134 *
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
135 * @param x horizontal position
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
136 * @param y vertical position
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
137 */
7
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
138 public void clearCellValue(int x, int y) {
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
139 setCellValue(x, y, 0);
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
140 }
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
141
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
142 /**
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
143 * Sets the modified state of a specific cell.
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
144 *
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
145 * @param x horizontal position
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
146 * @param y vertical position
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
147 * @param modified the modified state
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
148 */
7
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
149 public void setCellModified(int x, int y, boolean modified) {
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
150 cells[x][y].setModified(modified);
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
151 }
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
152
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
153 /**
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
154 * Sets the modified state of all cells.
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
155 *
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
156 * @param modified the modified state
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
157 */
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
158 public void setAllCellsModified(boolean modified) {
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
159 for (int x = 0; x < 9; x++) {
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
160 for (int y = 0; y < 9; y++) {
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
161 cells[x][y].setModified(modified);
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
162 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
163 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
164 }
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
165
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
166 /**
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
167 * Checks whether any cell is modified.
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
168 *
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
169 * @return true if any cell is modified, false otherwise
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
170 */
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
171 public boolean isAnyCellModified() {
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
172 for (int x = 0; x < 9; x++) {
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
173 for (int y = 0; y < 9; y++) {
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
174 if (cells[x][y].isModified()) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
175 return true;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
176 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
177 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
178 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
179 return false;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents: 1
diff changeset
180 }
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
181
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
182 /**
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
183 * Clears all cells.
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
184 */
4
b8588e318001 added NEW function
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
185 public void clear() {
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
186 for (int x = 0; x < 9; x++) {
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
187 for (int y = 0; y < 9; y++) {
4
b8588e318001 added NEW function
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
188 cells[x][y].setValue(0);
b8588e318001 added NEW function
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
189 }
b8588e318001 added NEW function
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
190 }
b8588e318001 added NEW function
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
191 }
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
192
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
193 /**
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
194 * Returns a square identified by square coordinates.
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
195 * <p>
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
196 * Cells within the square are identified by the same coordinate system.
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
197 *
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
198 * @param x horizontal position from 0 to 2
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
199 * @param y vertical position from 0 to 2
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
200 * @return a two-dimensional array containing the square cell values
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
201 */
5
8ddf4af937d7 moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
202 public int[][] getSquare(int x, int y) {
8ddf4af937d7 moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
203 if (x < 0 || x > 2 || y < 0 || y > 2) {
8ddf4af937d7 moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
204 throw new IllegalArgumentException("Invalid square coordinates");
8ddf4af937d7 moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
205 }
8ddf4af937d7 moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
206 int[][] square = new int[3][3];
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
207
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
208 for (int u = 0; u < 3; u++) {
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
209 for (int v = 0; v < 3; v++) {
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
210 square[u][v] = getCellValue(3 * x + u, 3 * y + v);
5
8ddf4af937d7 moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
211 }
8ddf4af937d7 moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
212 }
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
213
5
8ddf4af937d7 moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
214 return square;
8ddf4af937d7 moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
215 }
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
216
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
217 /**
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
218 * Returns an entire row.
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
219 *
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
220 * @param y the row position
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
221 * @return an array containing the row values
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
222 */
5
8ddf4af937d7 moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
223 public int[] getRow(int y) {
8ddf4af937d7 moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
224 if (y < 0 || y > 8) {
8ddf4af937d7 moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
225 throw new IllegalArgumentException("Invalid row number");
8ddf4af937d7 moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
226 }
8ddf4af937d7 moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
227 int row[] = new int[9];
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
228
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
229 for (int x = 0; x < 9; x++) {
5
8ddf4af937d7 moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
230 row[x] = getCellValue(x, y);
8ddf4af937d7 moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
231 }
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
232
5
8ddf4af937d7 moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
233 return row;
8ddf4af937d7 moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
234 }
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
235
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
236 /**
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
237 * Returns an entire column
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
238 *
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
239 * @param x the column position
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
240 * @return an array containing the column values
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
241 */
5
8ddf4af937d7 moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
242 public int[] getColumn(int x) {
8ddf4af937d7 moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
243 if (x < 0 || x > 8) {
8ddf4af937d7 moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
244 throw new IllegalArgumentException("Invalid column number");
8ddf4af937d7 moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
245 }
8ddf4af937d7 moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
246 int column[] = new int[9];
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
247
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
248 for (int y = 0; y < 9; y++) {
5
8ddf4af937d7 moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
249 column[y] = getCellValue(x, y);
8ddf4af937d7 moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
250 }
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
251
5
8ddf4af937d7 moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
252 return column;
8ddf4af937d7 moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
253 }
1
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
254 }

mercurial