📄 freesudoku.java
字号:
/**
* Copyright 2005 Victor Ferrer
*
* This file is part of FreeSudoku.
*
* FreeSudoku 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.
*
* FreeSudoku 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 FreeSudoku; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA*
*/
package org.gos.freesudoku;
import java.util.*;
import org.gos.freesudoku.view.Board;
import org.gos.freesudoku.view.Trans;
public class FreeSudoku
{
private Board board;
private Game game;
private FreeSudoku()
{
// Initialitations
new Trans( CONSTS.defLang); // init translation with current language
board = new Board();
board.setParent( this);
board.setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args)
{
new FreeSudoku();
}
public void posKeyTyped( int i, int j, int val)
{
if( game.isLegal( i, j, val))
{
board.setPosValue( i, j, val);
game.setPosVal( i, j, val);
if( game.resolved()) solved();
if( val != 0) board.checkConsistence( i, j);
}
else
{
board.setMessage( Trans.get().tag("incorrect_number"));
}
}
public boolean posKeyTypedSmall( int i, int j, int val)
{
// TODO: check this and prev method
if( game.isLegal( i, j, val))
{
board.setPosValue( i, j, val);
return true;
}
else
{
board.setMessage(Trans.get().tag("incorrect_number"));
return false;
}
}
public String getPosibleVals(int i, int j, boolean blanks)
{
StringBuffer aux = new StringBuffer();
ArrayList al = game.getAllLegalVals( i, j);
for (int k = 0; k < al.size(); k++)
{
if(blanks)
aux.append(al.get(k) + " ");
else aux.append(al.get(k));
}
return aux.toString().trim();
}
private void solved()
{
board.disableAll();
board.setMessage(Trans.get().tag("congratulations"));
}
public void restart()
{
game = new Game();
game.initPartida( board.getNivel(), board.isSymmetric());
board.initPartida( game.getPos(), game.getInitPos());
board.setParent( this);
board.setVisible(true);
}
public String partidaToString()
{
return game.toString();
}
/**
* Get current status and turn it to arraylist
*/
public ArrayList statusToFile()
{
ArrayList current = new ArrayList();
String cNivel = Integer.toString(game.getCNivel());
current.add(0,cNivel);
String cPosFilled = Integer.toString(game.getCPosFilled());
current.add(1,cPosFilled);
String sym = Integer.toString(booleanToInt(game.getIsSymmetric()));
current.add(2,sym);
boolean initPos[][] = game.getInitPos();
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
stringBuffer.append( booleanToInt(initPos[i][j]));
}
}
current.add(3,stringBuffer.toString());
current.add(4,game.cPosToString());
current.add(5,game.cResToString());
return current;
}
public void loadFromFile(ArrayList key){
game = new Game();
///
String cNivel =(String) key.get(0);
game.setCNivel(Integer.parseInt(cNivel));
String cPosFilled = (String)key.get(1);
game.setCPosFilled(Integer.parseInt(cPosFilled));
String sym = (String)key.get(2);
game.setIsSymmetric(intToBoolean(Integer.parseInt(sym)));
game.setInitPos((String)key.get(3));
game.cPosFromString((String)key.get(4));
game.cResFromString((String)key.get(5));
board.initPartida( game.getPos(), game.getInitPos());
board.setParent( this);
board.setVisible(true);
}
/**
* auxiliary function for statusToFile
* @param a
* @return
*/
public static int booleanToInt(boolean a){
if(a)
return 1;
return 0;
}
/**
* auxiliary function for loadFromFile
* @param a
* @return
*/
public static boolean intToBoolean(int a){
if(a==1){
return true;
}
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -