Advertisement
Vita_Harvey

SudokuModel_B

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