Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package csc143.sudoku;
- import javax.swing.*;
- import csc143.sudoku.*;
- import java.awt.*;
- import java.util.*;
- /**
- * @author Vita Wiebe
- * @version PAx: Sudoku Serialization and Make-Up
- * A class extending SudokuCore, which in turn extends
- * SudokuBase.
- */
- public class SudokuModel extends SudokuCore {
- /** Our class constructor.
- * @param int r, int c
- */
- public SudokuModel(int r, int c) {
- super(r, c);
- int size = super.getSize();
- }
- // Goes thru cellCoordinates[][] row index # n and determines
- // whether row is complete, incomplete, or contains duplicates (error)
- public State getRowState(int n) {
- // columns are total number for this whole game board
- // (which is why they're squared).
- int columns = getColumns() * getColumns();
- int size = getSize() * 2;
- int gridSorted[] = new int [size];
- // Initialize all values in array to -1;
- // this will flag the end of values in array.
- for(int item : gridSorted) {
- gridSorted[item] = -1;
- }
- // Put the germane range of values from super.grid[] into
- // gridSorted[] (depending on row we're examining).
- for(int i = 0; i < size; i++) {
- for(int col = 0; col < columns; col++) {
- gridSorted[i] = super.getValue(n, col);
- }
- }
- // Sort the values from grid/gridSorted associated
- // with row # n of cellCoordinates[][].
- Arrays.sort(gridSorted);
- // hasEmpty is a flag to store when an empty
- // element has been found.
- boolean hasEmpty = false;
- if(gridSorted[0] == 0) {
- hasEmpty = true;
- }
- // Iterate thru gridSorted, looking for empties
- // and duplicate values.
- for(int j = 1; gridSorted[j] != -1; j++) {
- if(gridSorted[j] == gridSorted[j - 1]) {
- return State.ERROR;
- } else if(gridSorted[j] == 0) {
- hasEmpty = true;
- }
- }
- if(hasEmpty) {
- return State.INCOMPLETE;
- }
- return State.COMPLETE;
- }
- // Need to go thru cellCoordinates[][] column index # n and determine
- // whether column is complete, incomplete, or contains duplicates (error).
- public State getColumnState(int n) {
- // Rows is the total number of rows on the board
- int rows = this.getRows();
- int size = this.getSize();
- // Create a copy of grid[], our superclass's array for holdinig
- // the values of each cell on the board.
- int gridSorted[] = new int[size];
- // Put the germane range of values from super.grid[] into
- // gridSorted[] (depending on column we're examining).
- for(int i = 0; i < size; i++) {
- for(int row = 0; row < rows; row++) {
- gridSorted[i] = super.getValue(row, n);
- }
- }
- // Sort the values from grid/gridSorted associated
- // with column # n of cellCoordinates[][].
- Arrays.sort(gridSorted);
- // hasEmpty is a flag to store when an empty
- // element has been found.
- boolean hasEmpty = false;
- if(gridSorted[0] == 0) {
- hasEmpty = true;
- }
- // Iterate thru gridSorted, looking for empties
- // and duplicate values.
- for(int j = 1; j < gridSorted.length; j++) {
- if(gridSorted[j] == gridSorted[j - 1]) {
- return State.ERROR;
- } else if(gridSorted[j] == 0) {
- hasEmpty = true;
- }
- }
- if(hasEmpty) {
- return State.INCOMPLETE;
- }
- return State.COMPLETE;
- }
- // Search the given Region and return INCOMPLETE if empties and no
- // duplicate values, ERROR if duplicates, and COMPLETE otherwise.
- public State getRegionState(int n) {
- return State.COMPLETE;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement