0

我正在开发一个检查数独解决方案有效性的程序。我接近完成它,但由于某种原因,当我尝试调用我创建的某些方法时,编译器返回它们未定义。

主要分为三个类:

  1. 一个数独类,它实现了可迭代的接口。它包含一个二维数组,即拼图。构造函数从扫描仪输入中获取一个文件并构建拼图。它有一个迭代器方法来满足接口要求。此方法返回一个 SudokuIterator 类型的交互器。

  2. 一个 SudokuIterator 类,它实现了迭代器接口。这是数独类的私有内部类。它还有一个二维数组和一个光标作为属性。它具有标准的 hasNext()、next() 和 remove() 存根来满足接口。我还添加了一个 nextColumn() 和 nextBox(),它们根据光标位置返回一个数组。next 方法已被覆盖以返回行。

  3. 最后是验证器。该方法是主要方法。它有一个方法 isSolution() 返回一个布尔值,具体取决于对从 SudokuIterator 类中定义的方法返回的每个数组的分析。

这就是我的问题出现的地方;当使用迭代器方法实例化并返回一个 SudokuIterator 并尝试使用我添加的 nextColumn() 和 nextBox() 方法时,编译器返回这些方法未为迭代器定义。

任何意见或建议将不胜感激。提前致谢。

import java.util.*;

/**
 * Sudoku class represents the matrix of cells in a Sudoku puzzle
 * @version 01/05/2012
 * @author Bob Wilson
 */

public class Sudoku implements Iterable<Cell []>
{
  private Cell [] [] puzzle;

  /**
   * Default constructor should not be called.  Make it private.
   */
  private Sudoku() {}

  /**
   * Puzzle constructor that uses a Scanner object to read a file.
   * File contains 81 numbers that are the values of the 81 cells.
   * @param file a Scanner object on a File object
   */
  public Sudoku(Scanner file)
  {
    int size = file.nextInt();
    System.out.println("Size: " + size);
    puzzle = new Cell[size][size];
    for (int i = 0; i < size; i++)
      for (int j = 0; j < size; j++)
        puzzle[i][j] = new Cell(file.nextInt());
  }

  public int getLength(){
        return this.puzzle.length;
    }


   class SudokuIterator implements Iterator<Cell []> {

        private Cell [][] puzzle;
        private int cursor;

        public SudokuIterator(Cell [][] puzzle){

            this.puzzle = puzzle;
            cursor = 1;

        }


        public boolean hasNext(){
            if(cursor <= this.puzzle.length){
                return true;
            }
            return false;   
    }
        public Cell[] next(){
            Cell[] row = puzzle[cursor-1];
            cursor++;
            return row;
        }

        public Cell[] nextColumn(){
            Cell[] column = new Cell[puzzle.length];
            for(int i = 0; i < puzzle.length; i++){
                column[i] = puzzle[i][cursor-1];
            }
            cursor++;
            return column;
        }

        public Cell[] nextBox(){
            Cell[] box = new Cell[puzzle.length];
            int boxIndex = 0;
            for(int i = ((cursor - 1)/((int)Math.sqrt(puzzle.length)))*(int)Math.sqrt(puzzle.length) ; i < ((cursor - 1)/((int)Math.sqrt(puzzle.length)))*(int)Math.sqrt(puzzle.length) + (int)Math.sqrt(puzzle.length); i++){
                for(int j = (((cursor - 1) + ((int)Math.sqrt(puzzle.length))) % ((int)Math.sqrt(puzzle.length))) * ((int)Math.sqrt(puzzle.length)); j < (((cursor - 1) + ((int)Math.sqrt(puzzle.length))) % ((int)Math.sqrt(puzzle.length))) * ((int)Math.sqrt(puzzle.length)) + ((int)Math.sqrt(puzzle.length)); j++){
                    box[boxIndex] = puzzle[i][j];
                }
            }
            cursor++;
            return box;
        }
        public void remove(){}
  }
  /**
   * Generates and returns a String representation of the puzzle cells
   * @return A String representing the contents of the puzzle array
   */
  public String toString()
  {
    // display the puzzle
    String value = "Puzzle is:\n";

    for (int i = 0; i < puzzle.length; i++) {
      for (int j = 0; j < puzzle[i].length; j++) 
        value += puzzle[i][j].toString();
      value += "\n";
    }
    return value;
  }

  /**
   * Instantiates and returns a new SudokuIterator object
   * @return A SudokuIterator object on the puzzle array
   */

  public SudokuIterator iterator(){

     SudokuIterator iterator = new SudokuIterator(this.puzzle);
     return iterator;

  }


  }
  /* 201340 */

import java.util.*;
import java.io.*;

/** 
 * This class instantiates a Sudoku object passing a Scanner on a
 * file to the Sudoku constructor.  It prints the puzzle using the
 * Sudoku toString method.  It determines if the digit matrix is a
 * valid solution for a Sudoku puzzle or not and prints the result.
 * 
 * @version 01/05/2012
 * @author Bob Wilson
 * 
 */

public class SudokuValidator
{
  private Sudoku puzzle;

  /**
   * @param args - not used
   */
  public static void main( String [] args)
  {
    Scanner scan = new Scanner(System.in);
    System.out.println("Please enter name of file containing puzzle to verify");   
    SudokuValidator myValidator = new SudokuValidator(scan.nextLine());
    System.out.println(myValidator.isSolution());
  }

  public SudokuValidator(String fileName)
  {
    Scanner file = null;
    try
    {
      file = new Scanner(new File(fileName));
    }
    catch (Exception e)
    {
      System.out.println("Bad file name");
      System.exit(0);
    }

    puzzle = new Sudoku(file);
    System.out.println(puzzle);
  }

  public boolean isSolution(){


      boolean flag = true;
      Iterator<Cell[]> game = puzzle.iterator();

      while(game.hasNext()){
          Cell[] row = game.next();
          Cell[] column = game.nextColumn();
          Cell[] box = game.nextBox();


          for(Cell i: row){
              for(Cell j: row){
                  if(j.equals(i.getValue())){
                      flag = false;
                      return flag;
                  }
              }
          }
      }  
      return flag;
  }
}  /* 201340 */
4

1 回答 1

3

game问题是,引用变量的声明类型是Iterator,它没有定义任何nextColumn()方法,所以编译器找不到它。您可以通过将声明的类型更改为Sudoku.SudokuIterator(因为这是一个内部类)来解决此问题。

改变:

Iterator<Cell[]> game = puzzle.iterator();

至:

Sudoku.SudokuIterator game = puzzle.iterator();
于 2013-09-29T19:05:37.927 回答