在數獨數獨游戲開發(一)當中 我們已經把數據填充到了相應的單元格,在數獨游戲開發(二)中,我們繼續為單元格設置點擊事件,先獲取不能使用的數字,然后通過自定義對話框把不可用的數字顯示出來。(可理解使用過的數字)
-
Game.java (增加不可用數字篩選)
/**
* 數獨游戲開發(二)
* @author dandan
*
*/
public class Game {
//數獨初始化數據
private final String data = "360000000004230800000004200"+
"070460003820000014500013020"+
"001900000007048300000000045";
//存儲整形數組
private int[] shuduku = new int[9*9];
public Game(){
shuduku = fromString(data);
calculateAllTiles();
}
//根據九宮格的坐標,返回坐標所對應的數字
private int getTile(int x,int y){
return shuduku[y*9+x];
}
//得到字符串
public String getStringTile(int x,int y){
int v = getTile(x,y);
if(v==0){
return "";
}
return String.valueOf(v);
}
//用來存儲每個單元格不可用的數字
private static int user[][][] = new int[9][9][];
//計算每個單元格中不可用的數字
public int[] calculateUserTiles(int x,int y){
int[] c = new int[9];
for(int i=0;i<9;i++){
if(i==y){
continue;
}
int t = getTile(x,i);
if(t!=0){
c[t-1]=t;
}
}
for(int i=0;i<9;i++){
if(i==x){
continue;
}
int t = getTile(i,y);
if(t!=0){
c[t-1]=t;
}
}
int startx = (x/3)*3;
int starty = (y/3)*3;
for(int i=startx;i<startx+3;i++){
for(int j=starty;j<starty+3;j++){
if(i==x&&j==y){
continue;
}
int t = getTile(i,j);
if(t!=0){
c[t-1]=t;
}
}
}
//compress
int nused = 0;
for(int t:c){
if(t!=0){
nused++;
}
}
int[] c1 = new int[nused];
nused = 0;
for(int t:c){
if(t!=0){
c1[nused++]=t;
}
}
return c1;
}
//計算所有單元格不可用的數據
public void calculateAllTiles(){
for(int x=0;x<9;x++){
for(int y=0;y<9;y++){
user[x][y] = calculateUserTiles(x,y);
}
}
}
//去除某單元格不可用的數據
public static int[] getUserTilesCoor(int x,int y){
return user[x][y];
}
//根據一個字符串數據,生成一個整型數組
protected int[] fromString(String src){
int[] shudu = new int[src.length()];
for(int i=0;i<src.length();i++){
shudu[i] = src.charAt(i)-'0';
}
return shudu;
}
}
-
MyView.java (增加onTouchEvent && AlertDialog自定義)
/**
* 數獨游戲開發(二)
* @author dandan
*
*/
public class MyView extends View{
private float widht,height;
private Game game = new Game();
//構造方法
public MyView(Context context) {
super(context);
}
//得到改變的視圖寬和高
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
this.widht=w/9f;
this.height=h/9f;
super.onSizeChanged(w, h, oldw, oldh);
}
//畫視圖,⑨宮格
@Override
protected void onDraw(Canvas canvas) {
//用于生成背景色的畫筆(共4種顏色的畫筆)
Paint backgroundpaint = new Paint();
//設置畫筆顏色
backgroundpaint.setColor(getResources().getColor(R.color.background));
//畫出背景
canvas.drawRect(0, 0, getWidth(), getHeight(), backgroundpaint);
Paint darkpaint = new Paint();
darkpaint.setColor(getResources().getColor(R.color.dark));
Paint lightpaint = new Paint();
lightpaint.setColor(getResources().getColor(R.color.light));
Paint hilitepaint = new Paint();
hilitepaint.setColor(getResources().getColor(R.color.hilite));
//下面開始畫線
for(int i=0;i<9;i++){
//畫橫線
canvas.drawLine(0, i*height, getWidth(), i*height, lightpaint);
canvas.drawLine(0, i*height+1, getWidth(), i*height+1, hilitepaint);
//畫豎線
canvas.drawLine(i*widht,0,i*widht, getHeight(), lightpaint);
canvas.drawLine(i*widht+1,0,i*widht+1, getHeight(), hilitepaint);
}
//畫外面的最清楚的4個深色的線
for(int i=0;i<9;i++){
if(i%3!=0){continue;}
//畫橫線
canvas.drawLine(0, i*height, getWidth(), i*height, darkpaint);
canvas.drawLine(0, i*height+1, getWidth(), i*height+1, hilitepaint);
//畫豎線
canvas.drawLine(i*widht,0,i*widht, getHeight(), darkpaint);
canvas.drawLine(i*widht+1,0,i*widht+1, getHeight(), hilitepaint);
}
//框框里添加文本
Paint numpaint = new Paint();
numpaint.setColor(Color.BLACK);
numpaint.setStyle(Paint.Style.STROKE);
numpaint.setTextAlign(Paint.Align.CENTER);
numpaint.setTextSize(height*0.75f);
//數字的位置
FontMetrics fm = numpaint.getFontMetrics();
float x = widht/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(game.getStringTile(i,j), i*widht+x,j*height+y, numpaint);
}
}
super.onDraw(canvas);
}
//觸摸每個單元格 則觸發此方法
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction()!=MotionEvent.ACTION_DOWN){
return super.onTouchEvent(event);
}
int selectx = (int) (event.getX()/widht);
int selecty = (int) (event.getY()/height);
//調用GAME類的方法(得到使用過的數字)
int used[] = Game.getUserTilesCoor(selectx,selecty);
//吧使用過的數字加載到 StringBuffer 里面去
StringBuffer sb = new StringBuffer();
for(int i=0;i<used.length;i++){
sb.append(used[i]);
}
//用來顯示sb中的不可用的數據
//生成一個LayoutInflater對象(生成一個自定義View)
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
//根據layoutInflater生成布局文件 ,view對象
View view = layoutInflater.inflate(R.layout.text, null);
//從生成好的Textview中取出相應的控件
TextView textview = (TextView) view.findViewById(R.id.usedid);
//設置一些屬相 參數
textview.setText(sb.toString());
//生成一個對話框對象
AlertDialog.Builder Builder = new AlertDialog.Builder(this.getContext());
//吧view對象塞進來,在對話框中進行顯示
Builder.setView(view);
//創建 顯示對話框
AlertDialog dialog = Builder.create();
dialog.show();
return true;
}
}
-
演示:
shudu.gif
-
源碼地址:
鏈接:http://pan.baidu.com/s/1nvcUEbZ 密碼:4bno