📄 sudokupuzzle.java
字号:
/*
* Copyright (C) 2005-2006 Leopardo.f
*
* This file is part of M-SuDoKu, a J2ME version of SuDoKu.
*
* M-SuDoKu is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* M-SuDoKu is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with M-SuDoKu; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA. Or, visit http://www.gnu.org/copyleft/gpl.html
*/
package MSuDoKu;
/**
* Class handling all low-level operations with the puzzle
* @author Leopardo.f
*/
public class SuDoKuPuzzle {
protected byte[][] yaaBoard, yaaSolution;
protected boolean[][] baaCarvedInStone;
protected byte yWritten = 0;
protected String sSolution;
/** Creates a new instance of SuDoKuPuzzle
* @param s 81-byte long string encoding the puzzle
*/
protected SuDoKuPuzzle (String s) {
int i, j;
sSolution = s;
yaaBoard = new byte[9][9];
yaaSolution = new byte[9][9];
baaCarvedInStone = new boolean[9][9];
for (i = 0; i < 9; i++)
for (j = 0; j < 9; j++)
{
yaaBoard[i][j] = (byte) (s.charAt (i * 9 + j) - '0');
if (yaaBoard[i][j] > 10)
{
baaCarvedInStone[i][j] = true;
yaaBoard[i][j] -= 10;
yaaSolution[i][j] = yaaBoard[i][j];
yWritten++;
}
else
{
yaaSolution[i][j] = yaaBoard[i][j];
yaaBoard[i][j] = 0;
baaCarvedInStone[i][j] = false;
}
}
}
/** Creates a new instance of SuDoKuPuzzle
* @param sOrig 81-byte long string encoding the original puzzle (used to check which cells are carved in stone)
* @param sSaved 81-byte long string encoding the puzzle saved on the phone
*/
protected SuDoKuPuzzle (String sOrig, String sSaved) {
int i, j;
byte b;
sSolution = sOrig;
yaaBoard = new byte[9][9];
yaaSolution = new byte[9][9];
baaCarvedInStone = new boolean[9][9];
for (i = 0; i < 9; i++)
for (j = 0; j < 9; j++)
{
yaaBoard[i][j] = (byte) (sSaved.charAt (i * 9 + j) - '0');
b = (byte) (sOrig.charAt (i * 9 + j) - '0');
if (yaaBoard[i][j] > 0)
yWritten++;
if (b > 10)
{
baaCarvedInStone[i][j] = true;
yaaSolution[i][j] = (byte) (b - 10);
}
else
{
baaCarvedInStone[i][j] = false;
yaaSolution[i][j] = b;
}
// System.out.println ("(" + i + "," + j + "): " + yaaBoard[i][j] + "," + b + " - " + baaCarvedInStone[i][j]);
}
}
/** Returns a new instance of SuDoKuPuzzle
* @param s 81-byte long string encoding the puzzle
*/
public static SuDoKuPuzzle getPuzzle (String s)
{
if (s.length() != 81)
return null;
return new SuDoKuPuzzle (s);
}
/** Returns a new instance of SuDoKuPuzzle
* @param sOrig 81-byte long string encoding the original puzzle (used to check which cells are carved in stone)
* @param sSaved 81-byte long string encoding the puzzle saved on the phone
*/
public static SuDoKuPuzzle getPuzzle (String sOrig, String sSaved)
{
if ((sOrig.length() != 81) || (sSaved.length() != 81))
return null;
return new SuDoKuPuzzle (sOrig, sSaved);
}
/** Checks if the given cell is carved in stone (i.e. contains a "given")
* @param i row of the cell (0-8)
* @param j column of the cell (0-8)
*/
public boolean isCarvedInStone (int i, int j)
{
return baaCarvedInStone[i][j];
}
/** Returns the value written in the given cell
* @param i row of the cell (0-8)
* @param j column of the cell (0-8)
*/
public byte getValue (byte i, byte j)
{
return yaaBoard[i][j];
}
/** Returns the correct value to be written in the given cell
* @param i row of the cell (0-8)
* @param j column of the cell (0-8)
*/
public byte getSolutionValue (byte i, byte j)
{
return yaaSolution[i][j];
}
/** Writes the given value into the given cell
* @param i row of the cell (0-8)
* @param j column of the cell (0-8)
* @param n value to be written (0-9)
*/
public void setValueWithoutChecks (byte i, byte j, byte n)
{
byte old = yaaBoard[i][j];
yaaBoard[i][j] = n;
if (!baaCarvedInStone[i][j])
if ((n > 0) && (old == 0))
yWritten++;
else if ((n == 0) && (old > 0))
yWritten--;
// System.out.println ("Written numbers: " + yWritten);
}
/** Checks if the puzzle is solved
*/
public boolean isSolved()
{
// System.out.println ("Filled cells:" + yWritten);
if (yWritten < 81)
return false;
for (byte i = 0; i < 9; i++)
{
// System.out.println ("Checking column: " + i);
if (!isValidColumn (i))
return false;
// System.out.println ("Checking row: " + i);
if (!isValidRow (i))
return false;
// System.out.println ("Checking square: " + i);
if (!isValidSquare ((byte) (i / 3), (byte) (i % 3)))
return false;
}
// System.out.println ("Puzzle solved!");
return true;
}
/** Checks if the given column for a candidate solved puzzle is valid (i.e. no number appearing more than once)
* @param j column to be checked (0-8)
*/
protected boolean isValidColumn (byte j)
{
byte b, k;
for (byte i = 0; i < 8; i++)
{
b = yaaBoard[i][j];
for (k = (byte) (i + 1); k < 9; k++)
if (b == yaaBoard[k][j])
return false;
}
return true;
}
/** Checks if the given row for a candidate solved puzzle is valid (i.e. no number appearing more than once)
* @param i row to be checked (0-8)
*/
protected boolean isValidRow (byte i)
{
byte b, k;
for (byte j = 0; j < 8; j++)
{
b = yaaBoard[i][j];
for (k = (byte) (j + 1); k < 9; k++)
if (b == yaaBoard[i][k])
return false;
}
return true;
}
/** Checks if the given square for a candidate solved puzzle is valid (i.e. no number appearing more than once)
* @param iRow row of the 3x3 square to be checked (0-2)
* @param jCol column of the 3x3 square to be checked (0-2)
*/
protected boolean isValidSquare (byte iRow, byte jCol)
{
byte b, k;
for (byte n = 0; n < 8; n++)
{
b = yaaBoard[iRow * 3 + (n / 3)][jCol * 3 + (n % 3)];
for (k = (byte) (n + 1); n < 9; n++)
if (b == yaaBoard[iRow * 3 + (k / 3)][jCol * 3 + (k % 3)])
return false;
}
return true;
}
/** Returns a string representation of the current puzzle
*/
public String getPuzzleAsString()
{
char[] caReturn = new char[81];
byte i, j;
for (i = 0; i < 9; i++)
for (j = 0; j < 9; j++)
caReturn[i * 9 + j] = (char) ('0' + yaaBoard[i][j]);
// System.out.println ("Puzzle string: " + new String (caReturn));
return new String (caReturn);
}
/** Returns a string representation of the solution to the current puzzle
*/
public String getSolutionAsString()
{
return sSolution;
}
/** Cleans up the board
*/
public void clean()
{
byte i, j;
for (i = 0; i < 9; i++)
for (j = 0; j < 9; j++)
if (!baaCarvedInStone[i][j])
yaaBoard[i][j] = 0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -