美文网首页
数独游戏开发(二)

数独游戏开发(二)

作者: 蛋蛋不哭 | 来源:发表于2016-05-07 10:59 被阅读93次

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

    • 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

    相关文章

      网友评论

          本文标题:数独游戏开发(二)

          本文链接:https://www.haomeiwen.com/subject/kpojrttx.html