public class Game {
private final String str = "360000000004230800000004200"+
"070460003820000014500013020"+
"001900000007048300000000045";
private int [] soduku = new int [9*9];
public Game(){
soduku = fromPuzzleString(str);
}
// 根据9宫格的坐标,返回坐标的格子中的数据;
private int getTile(int x, int y){
return soduku[y* 9 + x];
}
public String getTileString(int x, int y){
int v = getTile(x,y);
if(v == 0){
return "";
}else{
return String.valueOf(v);
}
}
protected int[] fromPuzzleString(String src){
int[] sudo = new int[str.length()];
for(int i = 0;i < sudo.length;i++){
sudo[i] = str.charAt(i) - '0';
}
return sudo;
}
}
//画字体
Paint textpaint = new Paint();
textpaint.setColor(Color.BLACK);
textpaint.setStyle(Paint.Style.STROKE);
textpaint.setTextSize(height * 0.75f);
textpaint.setTextAlign(Paint.Align.CENTER);
textpaint.setAntiAlias(true);
//计算字体大小
FontMetrics fm = textpaint.getFontMetrics();
float x = width / 2;
float y = height / 2 - (fm.ascent + fm.descent)/2;
for(int i = 0 ;i < 9 ;i++){
for(int j = 0 ; j < 9 ; j++){
canvas.drawText(new Game().getTileString(i, j), i * width + x,j * height + y, textpaint);
}
}