| 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.model; |
| 22 | |
| 23 | import java.util.HashMap; |
| 24 | import java.util.Map; |
| 25 | |
| 26 | /** |
| 27 | * SMALL_X type puzzle. 8 by 8 cells, divided into half vertically and into |
| 28 | * quarters horizontally. Each row and each column must contain the digits 1 |
| 29 | * to 8, as must each box. In addition, 2 by 4 boxes in the other direction |
| 30 | * and both diagonals must also contain all the digits. |
| 31 | * @author <a href="http://sourceforge.net/users/stevensa">Andrew Stevens</a> |
| 32 | */ |
| 33 | public class SmallXPuzzle extends Puzzle { |
| 34 | private static final int SIZE = 8; |
| 35 | |
| 36 | public static Puzzle create() { |
| 37 | Puzzle puzzle = new SmallXPuzzle(); |
| 38 | |
| 39 | for (int row = 0; row < SIZE; row++) { |
| 40 | for (int column = 0 ; column < SIZE; column++) { |
| 41 | puzzle.grid.put(new Coordinate(row, column), 0); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | return puzzle; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Creates a new instance of SmallXPuzzle |
| 50 | */ |
| 51 | public SmallXPuzzle() { |
| 52 | this.grid = new Grid(SIZE); |
| 53 | } |
| 54 | |
| 55 | public PuzzleTypeEnum getType() { |
| 56 | return PuzzleTypeEnum.SMALL_X; |
| 57 | } |
| 58 | |
| 59 | public int getMaxInt() { |
| 60 | return SIZE; |
| 61 | } |
| 62 | |
| 63 | public Map<String, Region> getRegions() { |
| 64 | Map<String, Region> regions = new HashMap<String, Region>(); |
| 65 | |
| 66 | // rows |
| 67 | for (int row = 0; row < SIZE; row++) { |
| 68 | Region region = new Region("Row " + row); |
| 69 | for (int column = 0 ; column < SIZE; column++) { |
| 70 | region.add(new Coordinate(row, column)); |
| 71 | } |
| 72 | regions.put("row-" + row, region); |
| 73 | } |
| 74 | // columns |
| 75 | for (int column = 0 ; column < SIZE; column++) { |
| 76 | Region region = new Region("Column " + column); |
| 77 | for (int row = 0; row < SIZE; row++) { |
| 78 | region.add(new Coordinate(row, column)); |
| 79 | } |
| 80 | regions.put("column-" + column, region); |
| 81 | } |
| 82 | // blocks - 2 x 4 rectangular blocks, each one 4 x 2 cells |
| 83 | for (int x = 0; x < 4; x++) { |
| 84 | for (int y = 0; y < 2; y++) { |
| 85 | Region region = new Region("Box " + ((x * 2) + y)); |
| 86 | for (int row = x * 2; row < (x + 1) * 2; row++) { |
| 87 | for (int column = y * 4; column < (y + 1) * 4; column++) { |
| 88 | region.add(new Coordinate(row, column)); |
| 89 | } |
| 90 | } |
| 91 | regions.put("box-" + ((x * 2) + y), region); |
| 92 | } |
| 93 | } |
| 94 | // more blocks - 4 x 2 rectangular blocks, each one 2 x 4 cells |
| 95 | for (int x = 0; x < 2; x++) { |
| 96 | for (int y = 0; y < 4; y++) { |
| 97 | Region region = new Region("Box " + ((x * 4) + y + 10)); |
| 98 | for (int row = x * 4; row < (x + 1) * 4; row++) { |
| 99 | for (int column = y * 2; column < (y + 1) * 2; column++) { |
| 100 | region.add(new Coordinate(row, column)); |
| 101 | } |
| 102 | } |
| 103 | regions.put("box-" + ((x * 4) + y + 10), region); |
| 104 | } |
| 105 | } |
| 106 | // diagonals |
| 107 | Region diagonal0 = new Region("Diagonal 0"); |
| 108 | for (int x = 0 ; x < SIZE; x++) { |
| 109 | diagonal0.add(new Coordinate(x, x)); |
| 110 | } |
| 111 | regions.put("diagonal-0", diagonal0); |
| 112 | Region diagonal1 = new Region("Diagonal 1"); |
| 113 | for (int x = 0 ; x < SIZE; x++) { |
| 114 | diagonal1.add(new Coordinate(x, SIZE - 1 - x)); |
| 115 | } |
| 116 | regions.put("diagonal-1", diagonal1); |
| 117 | // super-rows |
| 118 | for (int x = 0; x < 4; x++) { |
| 119 | Region region = new Region("superRow-" + x); |
| 120 | for (int row = x * 2; row < (x + 1) * 2; row++) { |
| 121 | region.addAll(regions.get("row-" + row)); |
| 122 | } |
| 123 | regions.put("superRow-" + x, region); |
| 124 | } |
| 125 | // super-columns |
| 126 | for (int x = 0; x < 4; x++) { |
| 127 | Region region = new Region("superColumn-" + x); |
| 128 | for (int column = x * 2; column < (x + 1) * 2; column++) { |
| 129 | region.addAll(regions.get("column-" + column)); |
| 130 | } |
| 131 | regions.put("superColumn-" + x, region); |
| 132 | } |
| 133 | |
| 134 | return regions; |
| 135 | } |
| 136 | |
| 137 | public String toString() { |
| 138 | return this.grid.toString(); |
| 139 | } |
| 140 | |
| 141 | public int getSize() { |
| 142 | return SIZE; |
| 143 | } |
| 144 | |
| 145 | } |