Advertisement
Vita_Harvey

SudokuModel_D

Mar 22nd, 2019
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.71 KB | None | 0 0
  1. package csc143.sudoku;
  2.  
  3. import javax.swing.*;
  4. import csc143.sudoku.*;
  5. import java.awt.*;
  6. import java.util.*;
  7.  
  8. /**
  9.  * @author Vita Wiebe
  10.  * @version PAx: Sudoku Serialization and Make-Up
  11.  * A class extending SudokuCore, which in turn extends
  12.  * SudokuBase.
  13.  */
  14. public class SudokuModel extends SudokuCore {
  15.    
  16.     /** Our class constructor.
  17.      * @param int r, int c
  18.      */
  19.     public SudokuModel(int r, int c) {
  20.         super(r, c);
  21.         int size = super.getSize();
  22.     }
  23.          
  24.     /**
  25.      * Need to go thru cellCoordinates[][] Row index # n and determine
  26.      * whether Row is complete, incomplete, or contains duplicates (error).
  27.      * @param int n, the Row on the Board we wish to check
  28.      * @return enum State, the state of the Row in question.
  29.      */
  30.     public State getRowState(int n) {
  31.        
  32.         // Columns are total number for this whole game board
  33.         // (which is why they're squared).
  34.         int columns = getColumns() * getColumns();  
  35.         int size = getSize() * getSize();    
  36.         int gridSorted[] = new int[columns];
  37.        
  38.         // Initialize all values in array to -1;
  39.         // this will flag the end of values in array.
  40.         // for(int item : gridSorted) {
  41. //             gridSorted[item] = -1;
  42. //         }
  43.  
  44.         // Put the germane range of values from super.grid[] into
  45.         // gridSorted[] (depending on row we're examining).
  46.         //for(int i = 0; i < columns; i++) {
  47.             int i = 0;
  48.             for(int col = 0; col < columns; col++) {
  49.            
  50.                 gridSorted[i] = super.getValue(n, col);                
  51.                 i++;
  52.             }
  53.         }
  54.        
  55.         // Sort the values from grid/gridSorted associated  
  56.         // with row # n of cellCoordinates[][].
  57.         Arrays.sort(gridSorted);
  58.        
  59.         // hasEmpty is a flag to store when an empty
  60.         // element has been found.
  61.         boolean hasEmpty = false;  
  62.         if(gridSorted[0] == 0) {
  63.             hasEmpty = true;            
  64.         }
  65.        
  66.         // Iterate thru gridSorted, looking for empties
  67.         // and duplicate values.
  68.         for(int j = 1; j < columns; j++) {
  69.             if((gridSorted[j] == gridSorted[j - 1]) &&
  70.                (gridSorted[j] != 0)) {
  71.                   return State.ERROR;
  72.             } else if(gridSorted[j] == 0) {
  73.                 hasEmpty = true;              
  74.             }                                    
  75.         }
  76.         if(hasEmpty) {
  77.             return State.INCOMPLETE;
  78.         }
  79.         return State.COMPLETE;        
  80.     }
  81.    
  82.    /**
  83.     * Need to go thru cellCoordinates[][] Column index # n and determine
  84.     * whether Column is complete, incomplete, or contains duplicates (error).
  85.     * @param int n, the Column on the Board we wish to check
  86.     * @return enum State, the state of the Column in question.
  87.     */
  88.    public State getColumnState(int n) {
  89.        
  90.         // Rows is the total number of rows on the board
  91.         int rows = getRows() * getRows();      
  92.         int size = getSize()* getSize();
  93.        
  94.         // Create a copy of grid[], our superclass's array for holdinig
  95.         // the values of each cell on the board.
  96.         int gridSorted[] = new int[size];
  97.        
  98.         // Put the germane range of values from super.grid[] into
  99.         // gridSorted[] (depending on column we're examining).
  100.         for(int i = 0; i < size; i++) {
  101.        
  102.             for(int row = 0; row < rows; row++) {
  103.            
  104.                 gridSorted[i] = super.getValue(row, n);
  105.             }
  106.         }
  107.        
  108.         // Sort the values from grid/gridSorted associated  
  109.         // with column # n of cellCoordinates[][].
  110.         Arrays.sort(gridSorted);
  111.        
  112.         // hasEmpty is a flag to store when an empty
  113.         // element has been found.
  114.         boolean hasEmpty = false;
  115.         if(gridSorted[0] == 0) {
  116.             hasEmpty = true;            
  117.         }
  118.        
  119.         // Iterate thru gridSorted, looking for empties
  120.         // and duplicate values.
  121.         for(int j = 1; j < gridSorted.length; j++) {
  122.             if(gridSorted[j] == gridSorted[j - 1]) {
  123.                 return State.ERROR;
  124.             } else if(gridSorted[j] == 0) {
  125.                 hasEmpty = true;              
  126.             }                                    
  127.         }
  128.         if(hasEmpty) {
  129.             return State.INCOMPLETE;
  130.         }
  131.         return State.COMPLETE;        
  132.     }        
  133.    
  134.     // Search the given Region and return INCOMPLETE if empties and no
  135.     // duplicate values, ERROR if duplicates, and COMPLETE otherwise.
  136.     public State getRegionState(int n) {
  137.    
  138.         return State.COMPLETE;        
  139.     }
  140.    
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement