Android开发视频教程《游戏数独》

Android开发视频教程《游戏数独》

5 (10人评价)

千锋3G学院-Android游戏开发教程-数独-第4讲的笔记

相关课时: 笔记详情:

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);
}
}

0 0

你感兴趣的课程

8万+浏览/ 916学员/ 4.5评分
免费
6万+浏览/ 177学员/ 5评分
免费
6万+浏览/ 972学员/ 4.8评分
免费