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