Advertisement
Vita_Harvey

SudokuModel_C

Mar 22nd, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.29 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.     // Goes thru cellCoordinates[][] row index # n and determines
  25.     // whether row is complete, incomplete, or contains duplicates (error)
  26.     public State getRowState(int n) {
  27.        
  28.         // columns are total number for this whole game board
  29.         // (which is why they're squared).
  30.         int columns = getColumns() * getColumns();  
  31.         int size = getSize() * 2;    
  32.         int gridSorted[] = new int [size];
  33.         // Initialize all values in array to -1;
  34.         // this will flag the end of values in array.
  35.         for(int item : gridSorted) {
  36.             gridSorted[item] = -1;
  37.         }
  38.         // Put the germane range of values from super.grid[] into
  39.         // gridSorted[] (depending on row we're examining).
  40.         for(int i = 0; i < size; i++) {
  41.        
  42.             for(int col = 0; col < columns; col++) {
  43.            
  44.                 gridSorted[i] = super.getValue(n, col);
  45.             }
  46.         }
  47.        
  48.         // Sort the values from grid/gridSorted associated  
  49.         // with row # n of cellCoordinates[][].
  50.         Arrays.sort(gridSorted);
  51.        
  52.         // hasEmpty is a flag to store when an empty
  53.         // element has been found.
  54.         boolean hasEmpty = false;  
  55.         if(gridSorted[0] == 0) {
  56.             hasEmpty = true;            
  57.         }
  58.        
  59.         // Iterate thru gridSorted, looking for empties
  60.         // and duplicate values.
  61.         for(int j = 1; gridSorted[j] != -1; j++) {
  62.             if(gridSorted[j] == gridSorted[j - 1]) {
  63.                 return State.ERROR;
  64.             } else if(gridSorted[j] == 0) {
  65.                 hasEmpty = true;              
  66.             }                                    
  67.         }
  68.         if(hasEmpty) {
  69.             return State.INCOMPLETE;
  70.         }
  71.         return State.COMPLETE;        
  72.     }
  73.    
  74.    // Need to go thru cellCoordinates[][] column index # n and determine
  75.    // whether column is complete, incomplete, or contains duplicates (error).
  76.    public State getColumnState(int n) {
  77.        
  78.         // Rows is the total number of rows on the board
  79.         int rows = this.getRows();      
  80.         int size = this.getSize();
  81.        
  82.         // Create a copy of grid[], our superclass's array for holdinig
  83.         // the values of each cell on the board.
  84.         int gridSorted[] = new int[size];
  85.        
  86.         // Put the germane range of values from super.grid[] into
  87.         // gridSorted[] (depending on column we're examining).
  88.         for(int i = 0; i < size; i++) {
  89.        
  90.             for(int row = 0; row < rows; row++) {
  91.            
  92.                 gridSorted[i] = super.getValue(row, n);
  93.             }
  94.         }
  95.        
  96.         // Sort the values from grid/gridSorted associated  
  97.         // with column # n of cellCoordinates[][].
  98.         Arrays.sort(gridSorted);
  99.        
  100.         // hasEmpty is a flag to store when an empty
  101.         // element has been found.
  102.         boolean hasEmpty = false;
  103.         if(gridSorted[0] == 0) {
  104.             hasEmpty = true;            
  105.         }
  106.        
  107.         // Iterate thru gridSorted, looking for empties
  108.         // and duplicate values.
  109.         for(int j = 1; j < gridSorted.length; j++) {
  110.             if(gridSorted[j] == gridSorted[j - 1]) {
  111.                 return State.ERROR;
  112.             } else if(gridSorted[j] == 0) {
  113.                 hasEmpty = true;              
  114.             }                                    
  115.         }
  116.         if(hasEmpty) {
  117.             return State.INCOMPLETE;
  118.         }
  119.         return State.COMPLETE;        
  120.     }        
  121.    
  122.     // Search the given Region and return INCOMPLETE if empties and no
  123.     // duplicate values, ERROR if duplicates, and COMPLETE otherwise.
  124.     public State getRegionState(int n) {
  125.    
  126.         return State.COMPLETE;        
  127.     }
  128.    
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement