数独游戏开发(二)

在数独数独游戏开发(一)当中 我们已经把数据填充到了相应的单元格,在数独游戏开发(二)中,我们继续为单元格设置点击事件,先获取不能使用的数字,然后通过自定义对话框把不可用的数字显示出来。(可理解使用过的数字)

  • 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
  • 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
  • 演示:

数独游戏开发(二)_第1张图片
shudu.gif
  • 源码地址:

链接:http://pan.baidu.com/s/1nvcUEbZ 密码:4bno

你可能感兴趣的:(数独游戏开发(二))