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

changeset 10
369903afbb29
parent 9
576e7a2861ae
child 12
1c62c6009161
     1.1 --- a/src/main/java/de/uapcore/sudoku/Field.java	Sat Jul 25 14:01:28 2020 +0200
     1.2 +++ b/src/main/java/de/uapcore/sudoku/Field.java	Sat Jul 25 15:29:51 2020 +0200
     1.3 @@ -1,16 +1,16 @@
     1.4  /*
     1.5   * Copyright 2013 Mike Becker. All rights reserved.
     1.6 - * 
     1.7 + *
     1.8   * Redistribution and use in source and binary forms, with or without
     1.9   * modification, are permitted provided that the following conditions are met:
    1.10 - * 
    1.11 + *
    1.12   * 1. Redistributions of source code must retain the above copyright
    1.13   *    notice, this list of conditions and the following disclaimer.
    1.14 - * 
    1.15 + *
    1.16   * 2. Redistributions in binary form must reproduce the above copyright
    1.17   *    notice, this list of conditions and the following disclaimer in the
    1.18   *    documentation and/or other materials provided with the distribution.
    1.19 - * 
    1.20 + *
    1.21   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    1.22   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.23   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.24 @@ -31,22 +31,26 @@
    1.25  import java.awt.image.BufferedImage;
    1.26  
    1.27  /**
    1.28 - *
    1.29 - * @author mike
    1.30 + * A panel rendering the Sudoku field.
    1.31 + * <p>
    1.32 + * Cells are identified by zero-based indices from top-left to bottom-right.
    1.33   */
    1.34  public final class Field extends JPanel {
    1.35      private SudokuTextField[][] cells;
    1.36 -    
    1.37 +
    1.38 +    /**
    1.39 +     * Constructs a new 9x9 Sudoku grid.
    1.40 +     */
    1.41      public Field() {
    1.42          setBackground(Color.WHITE);
    1.43 -        
    1.44 +
    1.45          setLayout(new GridBagLayout());
    1.46          GridBagConstraints c = new GridBagConstraints();
    1.47          c.insets = new Insets(5, 5, 5, 5);
    1.48 -        
    1.49 +
    1.50          cells = new SudokuTextField[9][9];
    1.51 -        for (int x = 0 ; x < 9 ; x++) {
    1.52 -            for (int y = 0 ; y < 9 ; y++) {
    1.53 +        for (int x = 0; x < 9; x++) {
    1.54 +            for (int y = 0; y < 9; y++) {
    1.55                  cells[x][y] = new SudokuTextField();
    1.56                  c.gridx = x;
    1.57                  c.gridy = y;
    1.58 @@ -55,69 +59,118 @@
    1.59          }
    1.60      }
    1.61  
    1.62 +    /**
    1.63 +     * Paints the grid and all contained cells.
    1.64 +     *
    1.65 +     * @param graphics the graphics context
    1.66 +     */
    1.67      @Override
    1.68      public void paint(Graphics graphics) {
    1.69          final int w = getWidth();
    1.70          final int h = getHeight();
    1.71          final int cw = w / 9;
    1.72          final int ch = h / 9;
    1.73 -        
    1.74 +
    1.75          BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    1.76          Graphics2D g = img.createGraphics();
    1.77          g.setBackground(Color.WHITE);
    1.78          g.clearRect(0, 0, w, h);
    1.79 -        
    1.80 +
    1.81          g.setColor(Color.BLACK);
    1.82 -        g.drawRect(1, 1, w-2, h-2);
    1.83 -        g.drawRect(2, 2, w-4, h-4);
    1.84 -        for (int x = cw ; x < w ; x += cw) {
    1.85 -            for (int y = ch ; y < h ; y += ch) {
    1.86 -                g.drawLine(x, 2, x, h-2);
    1.87 -                g.drawLine(2, y, w-2, y);
    1.88 +        g.drawRect(1, 1, w - 2, h - 2);
    1.89 +        g.drawRect(2, 2, w - 4, h - 4);
    1.90 +        for (int x = cw; x < w; x += cw) {
    1.91 +            for (int y = ch; y < h; y += ch) {
    1.92 +                g.drawLine(x, 2, x, h - 2);
    1.93 +                g.drawLine(2, y, w - 2, y);
    1.94                  if ((x / cw) % 3 == 0) {
    1.95 -                    g.drawLine(x+1, 2, x+1, h-2);
    1.96 +                    g.drawLine(x + 1, 2, x + 1, h - 2);
    1.97                  }
    1.98                  if ((y / ch) % 3 == 0) {
    1.99 -                    g.drawLine(2, y+1, w-2, y+1);
   1.100 +                    g.drawLine(2, y + 1, w - 2, y + 1);
   1.101                  }
   1.102              }
   1.103          }
   1.104 -        
   1.105 +
   1.106          graphics.drawImage(img, 0, 0, this);
   1.107          super.paintChildren(graphics);
   1.108      }
   1.109 -    
   1.110 +
   1.111 +    /**
   1.112 +     * Checks whether a cell is empty
   1.113 +     *
   1.114 +     * @param x horizontal position
   1.115 +     * @param y vertical position
   1.116 +     * @return true if the cell is empty, false otherwise
   1.117 +     */
   1.118      public boolean isCellEmpty(int x, int y) {
   1.119          return getCellValue(x, y) == 0;
   1.120      }
   1.121 -    
   1.122 +
   1.123 +    /**
   1.124 +     * Returns value of a specific cell.
   1.125 +     *
   1.126 +     * @param x horizontal position
   1.127 +     * @param y vertical position
   1.128 +     * @return the cell's value
   1.129 +     */
   1.130      public int getCellValue(int x, int y) {
   1.131          return cells[x][y].getValue();
   1.132      }
   1.133 -    
   1.134 +
   1.135 +    /**
   1.136 +     * Sets the value of a specific cell.
   1.137 +     *
   1.138 +     * @param x horizontal position
   1.139 +     * @param y vertical position
   1.140 +     * @param v the cells value
   1.141 +     */
   1.142      public void setCellValue(int x, int y, int v) {
   1.143          cells[x][y].setValue(v);
   1.144      }
   1.145 -    
   1.146 +
   1.147 +    /**
   1.148 +     * Clears the value of a specific cell.
   1.149 +     *
   1.150 +     * @param x horizontal position
   1.151 +     * @param y vertical position
   1.152 +     */
   1.153      public void clearCellValue(int x, int y) {
   1.154          setCellValue(x, y, 0);
   1.155      }
   1.156 -    
   1.157 +
   1.158 +    /**
   1.159 +     * Sets the modified state of a specific cell.
   1.160 +     *
   1.161 +     * @param x        horizontal position
   1.162 +     * @param y        vertical position
   1.163 +     * @param modified the modified state
   1.164 +     */
   1.165      public void setCellModified(int x, int y, boolean modified) {
   1.166          cells[x][y].setModified(modified);
   1.167      }
   1.168 -    
   1.169 +
   1.170 +    /**
   1.171 +     * Sets the modified state of all cells.
   1.172 +     *
   1.173 +     * @param modified the modified state
   1.174 +     */
   1.175      public void setAllCellsModified(boolean modified) {
   1.176 -        for (int x = 0 ; x < 9 ; x++) {
   1.177 -            for (int y = 0 ; y < 9 ; y++) {
   1.178 +        for (int x = 0; x < 9; x++) {
   1.179 +            for (int y = 0; y < 9; y++) {
   1.180                  cells[x][y].setModified(modified);
   1.181              }
   1.182          }
   1.183      }
   1.184 -    
   1.185 +
   1.186 +    /**
   1.187 +     * Checks whether any cell is modified.
   1.188 +     *
   1.189 +     * @return true if any cell is modified, false otherwise
   1.190 +     */
   1.191      public boolean isAnyCellModified() {
   1.192 -        for (int x = 0 ; x < 9 ; x++) {
   1.193 -            for (int y = 0 ; y < 9 ; y++) {
   1.194 +        for (int x = 0; x < 9; x++) {
   1.195 +            for (int y = 0; y < 9; y++) {
   1.196                  if (cells[x][y].isModified()) {
   1.197                      return true;
   1.198                  }
   1.199 @@ -125,53 +178,77 @@
   1.200          }
   1.201          return false;
   1.202      }
   1.203 -    
   1.204 +
   1.205 +    /**
   1.206 +     * Clears all cells.
   1.207 +     */
   1.208      public void clear() {
   1.209 -        for (int x = 0 ; x < 9 ; x++) {
   1.210 -            for (int y = 0 ; y < 9 ; y++) {
   1.211 +        for (int x = 0; x < 9; x++) {
   1.212 +            for (int y = 0; y < 9; y++) {
   1.213                  cells[x][y].setValue(0);
   1.214              }
   1.215          }
   1.216      }
   1.217 -    
   1.218 +
   1.219 +    /**
   1.220 +     * Returns a square identified by square coordinates.
   1.221 +     * <p>
   1.222 +     * Cells within the square are identified by the same coordinate system.
   1.223 +     *
   1.224 +     * @param x horizontal position from 0 to 2
   1.225 +     * @param y vertical position from 0 to 2
   1.226 +     * @return a two-dimensional array containing the square cell values
   1.227 +     */
   1.228      public int[][] getSquare(int x, int y) {
   1.229          if (x < 0 || x > 2 || y < 0 || y > 2) {
   1.230              throw new IllegalArgumentException("Invalid square coordinates");
   1.231          }
   1.232          int[][] square = new int[3][3];
   1.233 -        
   1.234 -        for (int u = 0 ; u < 3 ; u++) {
   1.235 -            for (int v = 0 ; v < 3 ; v++) {
   1.236 -                square[u][v] = getCellValue(3*x+u, 3*y+v);
   1.237 +
   1.238 +        for (int u = 0; u < 3; u++) {
   1.239 +            for (int v = 0; v < 3; v++) {
   1.240 +                square[u][v] = getCellValue(3 * x + u, 3 * y + v);
   1.241              }
   1.242          }
   1.243 -        
   1.244 +
   1.245          return square;
   1.246      }
   1.247 -    
   1.248 +
   1.249 +    /**
   1.250 +     * Returns an entire row.
   1.251 +     *
   1.252 +     * @param y the row position
   1.253 +     * @return an array containing the row values
   1.254 +     */
   1.255      public int[] getRow(int y) {
   1.256          if (y < 0 || y > 8) {
   1.257              throw new IllegalArgumentException("Invalid row number");
   1.258          }
   1.259          int row[] = new int[9];
   1.260 -        
   1.261 -        for (int x = 0 ; x < 9 ; x++) {
   1.262 +
   1.263 +        for (int x = 0; x < 9; x++) {
   1.264              row[x] = getCellValue(x, y);
   1.265          }
   1.266 -        
   1.267 +
   1.268          return row;
   1.269      }
   1.270 -    
   1.271 +
   1.272 +    /**
   1.273 +     * Returns an entire column
   1.274 +     *
   1.275 +     * @param x the column position
   1.276 +     * @return an array containing the column values
   1.277 +     */
   1.278      public int[] getColumn(int x) {
   1.279          if (x < 0 || x > 8) {
   1.280              throw new IllegalArgumentException("Invalid column number");
   1.281          }
   1.282          int column[] = new int[9];
   1.283 -        
   1.284 -        for (int y = 0 ; y < 9 ; y++) {
   1.285 +
   1.286 +        for (int y = 0; y < 9; y++) {
   1.287              column[y] = getCellValue(x, y);
   1.288          }
   1.289 -        
   1.290 +
   1.291          return column;
   1.292      }
   1.293  }

mercurial