Advertisement
Vita_Harvey

SudokuModel_A

Mar 17th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.33 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, implementing code by Dan Jinguji
  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.     }
  22.    
  23.     // Need to go thru cellCoordinates[][] row index # n and determine
  24.     // whether row is complete, incomplete, or contains duplicates (error)
  25.     public State getRowState(int n) {
  26.        
  27.         // Create a copy of grid[], our superclass's array for holdinig
  28.         // the values of each cell on the board.
  29.         int gridSorted[] = Arrays.copyOf(super.grid);
  30.         // Sort the copy of grid[] in ascending order so elements
  31.         // can be easily compared.
  32.         Arrays.sort(gridSorted);
  33.        
  34.         // columns are total number for this game board
  35.         int columns = c;
  36.         for(int col = 1; col < columns; col++) {
  37.             if(super.getValue(n, col) == 0) {
  38.                 return State.INCOMPLETE;
  39.             } else if(super.getValue(n, col) == (super.getValue(n, col - 1))) {
  40.                 return State.ERROR;
  41.             } else {
  42.                 return State.COMPLETE;
  43.             }
  44.         }        
  45.     }
  46.    
  47.     public State getColumnState(int n) {
  48.         // Create a copy of grid[], our superclass's array for holdinig
  49.         // the values of each cell on the board.
  50.         int gridSorted[] = Arrays.copyOf(super.grid);
  51.         // Sort the copy of grid[] in ascending order so elements
  52.         // can be easily compared.
  53.         Arrays.sort(gridSorted);
  54.        
  55.         // Rows is the total number of rows on the board
  56.         int rows = r;
  57.         for(int row = 1; row < rows; row++) {
  58.             if(super.getValue(n, col) == 0) {
  59.                 return State.INCOMPLETE;
  60.             } else if(super.getValue(n, col) == (super.getValue(n, col - 1))) {
  61.                 return State.ERROR;
  62.             } else {
  63.                 return State.COMPLETE;
  64.             }
  65.         }        
  66.     }
  67.        
  68.    
  69.     public State getRegionState(int n) {
  70.         return State.ERROR;
  71.     }
  72.    
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement