📄 sudoku.java
字号:
// Name: Tsang Kai Ming, Patrick (25)
// Couse Code 41300/1 Group V
// Subject: ICT-1414 Object-oriented Programming
// Sudoku Assignment 2 File (Java Programming)
class Sudoku {
//------------------------------------------------------------------------------------------------
public final static int BC= 3;
Grid[][] grids=new Grid[BC][BC];
// TODO:
// Declare a 3x3 2-dimension Grid array
// Initialize your Grid array by creating Grid objects in each of your element
// You need a nested for loop to help you do the work
public Sudoku() {
//------------------------------------------------------------------------------------------------
for (int i = 0; i < BC; i++)
for (int j = 0; j < BC; j++)
grids[i][j] =new Grid();
}
// The x,y here represent the coordinate in the 9x9
// We need to resolve:
// which Grid the 9x9 coordinate represents by using integral division (/)
// which array element in the Grid we are getting by using modulus (%)
// The return statement is given to you in the method.
// Do the error checking and fill in the ? marks.
public int get(int x, int y) {
//------------------------------------------------------------------------------------------------
if (x<0 || x>9 || y<0 || y>9){
return 0;}
else{
return grids[x/3][y/3].get(x%3, y%3);
}
// If x,y are not between 0-8, returns 0
// Otherwise, return using the following statement
// return grids[?][?].get(?, ?);
}
// Resolve the x,y as in the get method
// The statement is given to you in the method.
// Do the error checking and fill in the ? marks.
public void put(int num, int x, int y) {
//------------------------------------------------------------------------------------------------
if (num<0 || num>9 || x<0 || x>9 || y<0 || y>9){
System.out.println ("Please Insert the number between 0 - 9 ");
}else{
grids[x/3][y/3].put(num , x%3, y%3);
}
// If num not between 0 and 9,
// Print an error message and return (exit) the method
// otherwise, use the following statement to set the value
// (fill in the ? marks)
// grids[?][?].put(num, ?, ?);
}
// This method creates and returns a copy of Sudoku
// YOU DO NOT NEED TO MODIFY THIS METHOD
public Object clone() {
Sudoku sdk = new Sudoku();
for(int i=0; i<9; i++)
for(int j=0; j<9; j++)
sdk.put(this.get(i,j), i, j);
return sdk;
}
// The method checks if num is valid in the Sudoku.
public boolean checkValid(int num, int x, int y) {
// Check if the num is valid in its own Grid
// (i) first find out the grid coordinate,
// (ii) use the grid array element to call checkInside method
// return false if the checkInside method return false
//------------------------------------------------------------------------------------------------
if (grids[x/3][y/3].checkInside(num) == false)
return false;
// Then use a for loop,
for(int i=0; i<9; i++) {
//------------------------------------------------------------------------------------------------
if (y != i)
if (num == this.get(x,i))
return false;
if (x != i)
if (num == this.get(i,y))
return false;
// (i) Check if num appears in the same row:
// Use get method, fix the row to x, check different column
// (ii) Check if num appears in the same column:
// Use get method, fix the column to y, check different row
// Both (i) & (ii), return false immediately if:
// the get method returns a same num and
// the row,col coordinate is not the x,y in the input argument
}
// In any other cases, it is valid:
return true;
}
// The method returns a String representing possible moves in x,y
public String hintsMessage(int x, int y) {
// The following string is used to store the result
String str = "";
// Temp stores the current value in x,y coordinate of the Sudoku
int temp = get(x,y);
// We try to put in the numbers 1 to 9 one by one
for(int i=1; i<=9; i++) {
//------------------------------------------------------------------------------------------------
put(i,x,y);
if(checkValid(i,x,y))
str += i+" ";
// call put method to put i into the place
// check if the number is valid in the place
// if valid, append the number to the current result string
}
// Put back the original value of the x,y coordinate
put(temp,x,y);
// and return the result String
return str;
}
// Try to return the Sudoku as a String
// e.g.
// 5 3 | 7 |
// 6 | 1 9 5 |
// 9 8 | | 6
// -------+-------+------
// 8 | 6 | 3
// 4 | 8 5 3 | 1
// 7 | 2 | 6
// -------+-------+------
// 6 | | 2 8
// | 4 1 9 | 5
// | 8 | 7 9
public String toString() {
// create a string to store your result
String str="";
// use a nested for loop
for(int i=0; i<9; i++) { // outter for loop (i) controls a row
// print a horizontal line if i is 3 or 6
//------------------------------------------------------------------------------------------------
if (i == 3 || i == 6)
str += " -------+-------+------";
for (int j=0; j<9; j++) {
// inner for loop (j) controls the elements in a row
// print a vertial split (|) when j = 3 or 6
//------------------------------------------------------------------------------------------------
if (i == 3 || i == 6)
str += "|"+" ";
if (get(i,j) !=0)
str+=get(i,j)+" ";
else
str+=" ";
// get the value of the coordinate i,j and print it out
// if it is a zero, print a space " " instead
}
// add a line break when a line is finished
//------------------------------------------------------------------------------------------------
str += "\n";
}
return str;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -