1 | /* |
2 | * Copyright (c) 2005 The PseudoQ Project. |
3 | * |
4 | * This file is part of PseudoQ. |
5 | * |
6 | * PseudoQ is free software; you can redistribute it and/or modify |
7 | * it under the terms of the GNU Lesser General Public License as published by |
8 | * the Free Software Foundation; either version 2.1 of the License, or |
9 | * (at your option) any later version. |
10 | * |
11 | * PseudoQ is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU Lesser General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU Lesser General Public License |
17 | * along with PseudoQ; if not, write to the Free Software |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
19 | */ |
20 | |
21 | package net.sourceforge.pseudoq.generation; |
22 | |
23 | import java.util.Map; |
24 | import net.sourceforge.pseudoq.model.Coordinate; |
25 | |
26 | import net.sourceforge.pseudoq.model.Grid; |
27 | import net.sourceforge.pseudoq.model.Puzzle; |
28 | import net.sourceforge.pseudoq.model.PuzzleFactory; |
29 | import net.sourceforge.pseudoq.model.PuzzleTypeEnum; |
30 | import net.sourceforge.pseudoq.model.Region; |
31 | |
32 | /** |
33 | * Fills a samurai style grid, by filling in each of the intersecting grids in |
34 | * turn using an appropriate filler. |
35 | * @author <a href="http://sourceforge.net/users/stevensa">Andrew Stevens</a> |
36 | */ |
37 | public class SamuraiGridFiller implements GridFiller { |
38 | /** Log4J logger */ |
39 | private static final org.apache.log4j.Logger log = |
40 | org.apache.log4j.LogManager.getLogger(SamuraiGridFiller.class); |
41 | |
42 | GridFiller filler = null; |
43 | |
44 | /** Creates a new instance of SamuraiGridFiller */ |
45 | public SamuraiGridFiller(GridFiller filler) { |
46 | this.filler = filler; |
47 | } |
48 | |
49 | public void fill(Grid grid, Map<String, Region> regions, int digits) |
50 | throws GenerationException { |
51 | if (grid == null) { |
52 | throw new IllegalArgumentException("Can't fill a grid that doesn't exist"); |
53 | } |
54 | if (regions == null) { |
55 | throw new IllegalArgumentException("Can't fill grid with no regions"); |
56 | } |
57 | if (digits <= 0) { |
58 | throw new IllegalArgumentException("Can't fill grid with no digits"); |
59 | } |
60 | |
61 | Puzzle subgrid = PuzzleFactory.newInstance(PuzzleTypeEnum.STANDARD); |
62 | // first fill centre ([6, 6] to [14, 14]) |
63 | copyArea(grid, subgrid.getGrid(), 6, 6, 0, 0, 9, 9); |
64 | filler.fill(subgrid.getGrid(), subgrid.getRegions(), digits); |
65 | copyArea(subgrid.getGrid(), grid, 0, 0, 6, 6, 9, 9); |
66 | subgrid.reset(); |
67 | // next fill bottom right ([12, 12] to [20, 20]) |
68 | copyArea(grid, subgrid.getGrid(), 12, 12, 0, 0, 9, 9); |
69 | filler.fill(subgrid.getGrid(), subgrid.getRegions(), digits); |
70 | copyArea(subgrid.getGrid(), grid, 0, 0, 12, 12, 9, 9); |
71 | subgrid.reset(); |
72 | // next fill top right ([0, 12] to [8, 20]) |
73 | copyArea(grid, subgrid.getGrid(), 0, 12, 0, 0, 9, 9); |
74 | flipVertically(subgrid.getGrid()); |
75 | filler.fill(subgrid.getGrid(), subgrid.getRegions(), digits); |
76 | flipVertically(subgrid.getGrid()); |
77 | copyArea(subgrid.getGrid(), grid, 0, 0, 0, 12, 9, 9); |
78 | subgrid.reset(); |
79 | // next comes bottom left ([12, 0] to [20, 8]) |
80 | copyArea(grid, subgrid.getGrid(), 12, 0, 0, 0, 9, 9); |
81 | flipHorizontally(subgrid.getGrid()); |
82 | filler.fill(subgrid.getGrid(), subgrid.getRegions(), digits); |
83 | flipHorizontally(subgrid.getGrid()); |
84 | copyArea(subgrid.getGrid(), grid, 0, 0, 12, 0, 9, 9); |
85 | subgrid.reset(); |
86 | // lastly, top left ([0, 0] to [8, 8]) |
87 | copyArea(grid, subgrid.getGrid(), 0, 0, 0, 0, 9, 9); |
88 | flipVertically(subgrid.getGrid()); |
89 | flipHorizontally(subgrid.getGrid()); |
90 | filler.fill(subgrid.getGrid(), subgrid.getRegions(), digits); |
91 | flipHorizontally(subgrid.getGrid()); |
92 | flipVertically(subgrid.getGrid()); |
93 | copyArea(subgrid.getGrid(), grid, 0, 0, 0, 0, 9, 9); |
94 | } |
95 | |
96 | private void copyArea(Grid from, Grid to, |
97 | int fromRow, int fromColumn, int toRow, int toColumn, |
98 | int rows, int columns) { |
99 | for (int row = 0; row < rows; row++) { |
100 | for (int column = 0; column < columns; column++) { |
101 | to.put(new Coordinate(toRow + row, toColumn + column), |
102 | from.get(new Coordinate(fromRow + row, fromColumn + column))); |
103 | } |
104 | } |
105 | } |
106 | |
107 | private void flipVertically(Grid grid) { |
108 | int size = grid.getSize(); |
109 | for (int row = 0; row < (size / 2); row++) { |
110 | for (int column = 0; column < size; column++) { |
111 | Coordinate cell1 = new Coordinate(row, column); |
112 | Coordinate cell2 = new Coordinate(size - row - 1, column); |
113 | Integer value = grid.get(cell1); |
114 | |
115 | grid.put(cell1, grid.get(cell2)); |
116 | grid.put(cell2, value); |
117 | } |
118 | } |
119 | } |
120 | |
121 | private void flipHorizontally(Grid grid) { |
122 | int size = grid.getSize(); |
123 | for (int row = 0; row < size; row++) { |
124 | for (int column = 0; column < (size / 2); column ++) { |
125 | Coordinate cell1 = new Coordinate(row, column); |
126 | Coordinate cell2 = new Coordinate(row, size - column - 1); |
127 | Integer value = grid.get(cell1); |
128 | |
129 | grid.put(cell1, grid.get(cell2)); |
130 | grid.put(cell2, value); |
131 | } |
132 | } |
133 | } |
134 | |
135 | } |