Android数独游戏源码


Android数独游戏源码 ,照着mars的视频教程学的。

  1. package com.example.android;  
  2.   
  3. import android.annotation.SuppressLint;  
  4. import android.content.Context;  
  5. import android.graphics.Canvas;  
  6. import android.graphics.Color;  
  7. import android.graphics.Paint;  
  8. import android.graphics.Paint.Align;  
  9. import android.graphics.Paint.FontMetrics;  
  10. import android.util.AttributeSet;  
  11. import android.view.MotionEvent;  
  12. import android.view.View;  
  13.   
  14. @SuppressLint("DrawAllocation")  
  15. public class myView extends View{  
  16.   
  17.     public myView(Context context, AttributeSet attrs) {  
  18.         super(context, attrs);  
  19.     }  
  20.     //方格长宽   
  21.     float width;  
  22.     float height;  
  23.     //选定的坐标   
  24.     int selectX;  
  25.     int selectY;  
  26.     myGame mGame=new myGame();  
  27.       
  28.       
  29.     //绘图函数   
  30.     @Override  
  31.     protected void onDraw(Canvas canvas) {  
  32.         super.onDraw(canvas);  
  33.         //画背景   
  34.         Paint bgPaint=new Paint();  
  35.         bgPaint.setColor(getResources().getColor(R.color.bg));  
  36.         canvas.drawRect(0,0,getWidth(),getHeight()*2/3f, bgPaint);  
  37.         //线条画笔   
  38.         Paint darkPaint =new Paint();  
  39.         darkPaint.setColor(getResources().getColor(R.color.dark));  
  40.         Paint hilitePaint =new Paint();  
  41.         hilitePaint.setColor(getResources().getColor(R.color.hilite));  
  42.         Paint lightPaint =new Paint();  
  43.         lightPaint.setColor(getResources().getColor(R.color.light));  
  44.         //绘制线条   
  45.         for(int i=0;i<9;i++){  
  46.             canvas.drawLine(0, i*height,getWidth(),i*height,lightPaint);  
  47.             canvas.drawLine(0, i*height+1,getWidth(),i*height+1,hilitePaint);  
  48.             canvas.drawLine(i*width, 0,i*width,getHeight(),lightPaint);  
  49.             canvas.drawLine(i*width+1,0,i*width+1,getHeight(),hilitePaint);  
  50.             if(i%3==0){  
  51.                 canvas.drawLine(0, i*height,getWidth(),i*height,darkPaint);  
  52.                 canvas.drawLine(0, i*height+1,getWidth(),i*height+1,hilitePaint);  
  53.                 canvas.drawLine(i*width, 0,i*width,getHeight(),darkPaint);  
  54.                 canvas.drawLine(i*width+10,i*width+1,getHeight(),hilitePaint);  
  55.             }  
  56.         }  
  57.         //绘制数字   
  58.         Paint numberPaint =new Paint();  
  59.         numberPaint.setColor(Color.BLACK);  
  60.         numberPaint.setStyle(Paint.Style.STROKE);  
  61.         numberPaint.setTextSize(height*0.75f);  
  62.         numberPaint.setTextAlign(Align.CENTER);  
  63.           
  64.         //调节文字居中   
  65.         FontMetrics fMetrics=numberPaint.getFontMetrics();  
  66.         float x=width/2;  
  67.         float y=height/2-(fMetrics.ascent+fMetrics.descent)/2;  
  68.         for(int i=0;i<9;i++)  
  69.         {  
  70.             for(int j=0;j<9;j++)  
  71.             {  
  72.                 canvas.drawText(mGame.getNumber(i+1, j+1), i*width+x, y+j*height, numberPaint);  
  73.             }  
  74.         }  
  75.   
  76.     }  
  77.     //触摸事件   
  78.     @Override  
  79.     public boolean onTouchEvent(MotionEvent event) {  
  80.         int x=(int)(event.getX()/width);  
  81.         int y=(int)(event.getY()/height);  
  82.         int []t=mGame.getUsed(x, y);  
  83.         selectX=x;  
  84.         selectY=y;  
  85.         myDialog mDialog=new myDialog(getContext(), t, this);  
  86.         mDialog.show();  
  87.         return super.onTouchEvent(event);  
  88.     }  
  89.     //获得屏幕尺寸   
  90.     @Override  
  91.     protected void onSizeChanged(int w, int h, int oldw, int oldh) {  
  92.         super.onSizeChanged(w, h, oldw, oldh);  
  93.         //每一个小格的长宽   
  94.         this.width=w/9f;  
  95.         this.height=h/9f*2/3;  
  96.     }  
  97.     public void setTitle(int i){  
  98.         mGame.setTitle(i,selectX,selectY);  
  99.         invalidate();  
  100.     }  
  101.       
  102. }  

 

  1. package com.example.android;  
  2.   
  3.   
  4.   
  5. public class myGame {  
  6.     String data="008309100" +  
  7.             "900060004" +  
  8.             "007504800" +  
  9.             "036000540" +  
  10.             "001000600" +  
  11.             "042000970" +  
  12.             "005907300" +  
  13.             "600010008" +  
  14.             "004608200";  
  15.     int numbers[][] =new int[9][9];  
  16.     public myGame(){  
  17.         //初始化data   
  18.         for(int i=0;i<9;i++)  
  19.             for(int j=0;j<9;j++)  
  20.             {  
  21.                 numbers[i][j]=data.charAt(i+j*9)-'0';  
  22.             }  
  23.     }  
  24.     //得到值   
  25.     public String getNumber(int x,int y){  
  26.         if(numbers[x-1][y-1]==0)  
  27.             return "";  
  28.         else  
  29.             return ""+numbers[x-1][y-1];  
  30.     }  
  31.     //算出已经被用的数字   
  32.     public int[] getUsed(int x,int y){  
  33.         int c[]=new int[9];  
  34.         //x列   
  35.         for(int i=0;i<9;i++)  
  36.         {  
  37.             if(numbers[x][i]!=0)  
  38.             {  
  39.                 c[numbers[x][i]-1]=numbers[x][i];  
  40.                 //System.out.println("x:"+numbers[x][i]);   
  41.             }  
  42.         }  
  43.         //y排   
  44.         for(int i=0;i<9;i++)  
  45.         {  
  46.             if(numbers[i][y]!=0)  
  47.             {  
  48.                 c[numbers[i][y]-1]=numbers[i][y];  
  49.                 //System.out.println("y:"+numbers[i][y]);   
  50.             }  
  51.         }  
  52.         //小九宫格   
  53.         x=(x/3)*3;  
  54.         y=(y/3)*3;  
  55.         for(int i=0;i<9;i++)  
  56.         {  
  57.             if(numbers[x+i%3][y+i/3]!=0)  
  58.             {  
  59.                 c[numbers[x+i%3][y+i/3]-1]=numbers[x+i%3][y+i/3];  
  60.             }  
  61.         }  
  62.         return c;  
  63.     }  
  64.     //设置选定的数字   
  65.     public void setTitle(int i,int x,int y){  
  66.         numbers[x][y]=i;  
  67.     }  
  68. }  

 

  1. package com.example.android;  
  2.   
  3. import android.app.Dialog;  
  4. import android.content.Context;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7.   
  8. public class myDialog extends Dialog{  
  9.   
  10.     myView mView;  
  11.     View keys[]=new View[9];  
  12.     int []used=new int[9];  
  13.     //传入已经使用过的数字   
  14.     public myDialog(Context context,int[] useed,myView m) {  
  15.         super(context);  
  16.         this.mView=m;  
  17.         this.used=useed;  
  18.           
  19.     }  
  20.     @Override  
  21.     protected void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         //设置标题   
  24.         setTitle("可选数字");  
  25.         //设置布局文件   
  26.         setContentView(R.layout.table);  
  27.         int id[]=new int[]{R.id.bt1,R.id.bt2,R.id.bt3,R.id.bt4,  
  28.                 R.id.bt5,R.id.bt6,R.id.bt7,R.id.bt8,R.id.bt9};  
  29.         for(int i=0;i<9;i++)  
  30.         {  
  31.             final int t=i+1;  
  32.             keys[i]=findViewById(id[i]);  
  33.             keys[i].setOnClickListener(new View.OnClickListener() {  
  34.                   
  35.                 public void onClick(View v) {  
  36.                     mView.setTitle(t);  
  37.                     dismiss();  
  38.                 }  
  39.             });  
  40.         }  
  41.         for(int i=0;i<9;i++)  
  42.         {  
  43.             if(used[i]!=0)  
  44.                 keys[used[i]-1].setVisibility(View.INVISIBLE);  
  45.         }  
  46.     }  
  47.   
  48. }  

 

  1. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="wrap_content"  
  3.     android:layout_height="wrap_content"  
  4.     android:orientation="vertical"  
  5.     android:stretchColumns="*" >  
  6.   
  7.   
  8.     <TableRow  
  9.         android:id="@+id/row1"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content" >  
  12.   
  13.   
  14.   
  15.   
  16.         <Button  
  17.             android:id="@+id/bt1"  
  18.             android:layout_width="wrap_content"  
  19.             android:layout_height="wrap_content"  
  20.             android:text="1" />  
  21.   
  22.   
  23.         <Button  
  24.             android:id="@+id/bt2"  
  25.             android:layout_width="wrap_content"  
  26.             android:layout_height="wrap_content"  
  27.             android:text="2" />  
  28.   
  29.   
  30.         <Button  
  31.             android:id="@+id/bt3"  
  32.             android:layout_width="wrap_content"  
  33.             android:layout_height="wrap_content"  
  34.             android:text="3" />  
  35.   
  36.     </TableRow>  
  37.   
  38.   
  39.     <TableRow  
  40.         android:id="@+id/row2"  
  41.         android:layout_width="wrap_content"  
  42.         android:layout_height="wrap_content" >  
  43.   
  44.   
  45.   
  46.         <Button  
  47.             android:id="@+id/bt4"  
  48.             android:layout_width="wrap_content"  
  49.             android:layout_height="wrap_content"  
  50.             android:text="4" />  
  51.   
  52.   
  53.   
  54.         <Button  
  55.             android:id="@+id/bt5"  
  56.             android:layout_width="wrap_content"  
  57.             android:layout_height="wrap_content"  
  58.             android:text="5" />  
  59.   
  60.   
  61.   
  62.         <Button  
  63.             android:id="@+id/bt6"  
  64.             android:layout_width="wrap_content"  
  65.             android:layout_height="wrap_content"  
  66.             android:text="6" />  
  67.   
  68.     </TableRow>  
  69.   
  70.   
  71.     <TableRow  
  72.         android:id="@+id/row3"  
  73.         android:layout_width="wrap_content"  
  74.         android:layout_height="wrap_content" >  
  75.   
  76.   
  77.   
  78.         <Button  
  79.             android:id="@+id/bt7"  
  80.             android:layout_width="wrap_content"  
  81.             android:layout_height="wrap_content"  
  82.             android:text="7" />  
  83.   
  84.   
  85.   
  86.         <Button  
  87.             android:id="@+id/bt8"  
  88.             android:layout_width="wrap_content"  
  89.             android:layout_height="wrap_content"  
  90.             android:text="8" />  
  91.   
  92.   
  93.   
  94.         <Button  
  95.             android:id="@+id/bt9"  
  96.             android:layout_width="wrap_content"  
  97.             android:layout_height="wrap_content"  
  98.             android:text="9" />  
  99.   
  100.     </TableRow>  
  101.   
  102. </TableLayout>  

相关内容