Java中的数独游戏

时间:2015-03-11 18:00:24

标签: java java.util.scanner

以下是我对此代码的指示: 一个简单的数独“数组”的基本实现。基本实现将具有Sudoku难题的表示以及用户插入值,移除值,使用行,列和框内的当前数据检查有效值的方式,并打印“数组”列出的值。打印功能应指示哪些值对当前数据无效,但不必是网格显示(即它可以只是数组项的水平列表)。数组项应该编号,使得最左边的元素是arrayname [0,0],最右边的元素是arrayname [0,8],最左边的元素是arrayname [8,0],最右边的元素是arrayname [ 8,8]。

需要以下方法: 两个构造函数 - 一个初始化一个完全空的网格,另一个构造函数将网格初始化为上面显示的所需特征值。

  • public void printMySudoku() - 打印数独阵列的当前值,包括空白点
  • public boolean insertVal(int r, int c, int val) - 如果能够将val中指示的值插入到由r - row和c - column指示的位置的Sudoku数组中并返回true,则返回true。
  • public boolean removeVal(int r, int c, int val) - 如果能够将val中指示的值移除到由r - row和c - column指示的位置的Sudoku数组中,则返回true并将其删除。
  • private boolean checkRow(int r, int c, int val) - 如果可以在r-row和c-column指示的位置插入val,则返回true。
  • private boolean checkCol(int r, int c, int val) - 如果可以在沿着该列的r - row和c - column指示的位置插入val,则返回true。
  • private boolean checkBox(int r, int c, int val) - 如果可以在该BOX内的r - row和c - column指示的位置插入val,则返回true。

我唯一要做的就是创建一个检查,如果用户插入行,列或值9或更高,则打印出一个语句。我曾多次尝试这样做,但都失败了。我把它放在哪里都有点朦胧,需要一些关于支票编码的帮助。我自己完成了整个作业,这是我的个人代码。我只需要检查部分的帮助。

这是我现在的代码(没有签入):

public class Sudoku {
    public int[][] grid;

    Sudoku(){
        grid = new int[9][9]; //empty
    }

    Sudoku(String start){  
        grid = new int[9][9];
        for(int x = 0; x <9; x++){
            for(int y =0; y<9; y++){
                grid[x][y] = 0;
            }
        }

        grid[0][0] = 5; //top left most
        grid[0][1] = 3;
        grid[0][4] = 7;
        grid[1][0] = 6;
        grid[1][3] = 1;
        grid[1][4] = 9;
        grid[1][5] = 5;
        grid[2][1] = 9;
        grid[2][2] = 8;
        grid[2][7] = 6;
        grid[3][0] = 8;
        grid[3][4] = 6;
        grid[3][8] = 3;
        grid[4][0] = 4;
        grid[4][3] = 8;
        grid[4][5] = 3;
        grid[4][8] = 1;
        grid[5][0] = 7;
        grid[5][4] = 2;
        grid[5][8] = 6;
        grid[6][1] = 6;
        grid[6][6] = 2;
        grid[6][7] = 8;
        grid[7][3] = 4;
        grid[7][4] = 1;
        grid[7][5] = 9;
        grid[7][8] = 5;
        grid[8][4] = 8;
        grid[8][7] = 7;
        grid[8][8] = 9; //bottom right most

    }

    public void printMySudoku(){
        for (int i = 0; i < 9; ++i) {
            if (i % 3 == 0)
                System.out.println(" -----------------------");
            for (int j = 0; j < 9; ++j) {
                if (j % 3 == 0) System.out.print("| ");
                System.out.print(grid[i][j] == 0
                        ? " "
                                : Integer.toString(grid[i][j]));

                System.out.print(' ');
            }
            System.out.println("|");
        }
        System.out.println(" -----------------------");
    }

    public boolean insertVal(int row, int col, int myVal){
        System.out.println("Entered insertVal " + "row " + row + "       column " + col + " myVal " + myVal);
        if(checkRow(row, col, myVal) == false) 
            return false;

        if(checkCol(row, col, myVal) == false)
            return false;

        if(checkBox(row, col, myVal) == false) 
            return false;

        grid[row][col] = myVal;


        return true;

    }

    public boolean removeVal(int row, int col, int myVal) {
        grid[row][col] = 0;

        return true;
    }

    private boolean checkRow(int row, int col, int myVal) {
        for (int a = 0; a < 9; ++a)  // row
            if (myVal == grid[row][a]){
                System.out.println(myVal + " Already in Row: " + row);
                return false;
            }
        return true;

    }
    private boolean checkCol(int row, int col, int myVal) {
        for (int b = 0; b < 9; ++b)  // column
            if (myVal == grid[b][col]){
                System.out.println(myVal + " Already in Column: " + col);  
                return false;
            }
        return true;

    }

    private boolean checkBox(int row, int col, int myVal) {
        int boxRowOffset = (row / 3)*3;
        int boxColOffset = (col / 3)*3;
        for (int c = 0; c < 3; ++c) // box
            for (int d = 0; d < 3; ++d)
                if (myVal == grid[boxRowOffset+c][boxColOffset+d]){
                    System.out.println(myVal + " Already in Box " );
                     return false;
                }
        return true;

    }
}

以下是我的测试人员:

import java.util.Scanner;
   public class Play {


   public static void main(String args[]){

       Sudoku i = new Sudoku("start");
       i.printMySudoku();
       Scanner guess = new Scanner(System.in);

       int row, col, val;
       String action;

       while(true){
           System.out.println("Enter I for insert or R for remove: ");
           action = guess.next();

           System.out.println("Row: ");
           row = guess.nextInt();
           System.out.println("Column: ");
           col = guess.nextInt();
           System.out.println("Value: ");
           val = guess.nextInt();

           if(action.equals("I")){
               if(i.insertVal(row, col, val)){
                   System.out.println("Good Job  ");
               }
               else System.out.println("Try Again ");
               i.printMySudoku();
           }
           else{ i.removeVal(row, col, val);
           i.printMySudoku();}
       }
   }
}

2 个答案:

答案 0 :(得分:0)

快速解决方案:

int temp = 0;

// first, assign the guess to a temporary variable
if ((temp = guess.nextInt()) > 9) { // is this out of range?
    System.out.println("You can't do that, it's too big");
} else {
    // assign whichever one you're assigning, row/col/val to temp
}

我建议为每个作业使用其中一个块。

答案 1 :(得分:0)

我建议你提供一个特定的方法来进行验证:

private static int validate(int value)
{
    int validated;
    if (value<1 || value>9)
    {
        validated=-1;
        System.err.println("Error: Parameter must be between 1 and 9");
    }
    else
    {
        validated=value;
    }
    return validated;
}

...然后用它来验证从扫描仪读取的每个参数,以这种方式:

do
{
    System.out.println("Row: ");
    row = validate(guess.nextInt());
}
while (row<0);

...与参数col和value相同(你可以对它们使用相同的验证方法)。

相关问题