国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

基于Android實現數獨游戲

2019-10-22 18:19:58
字體:
來源:轉載
供稿:網友

本文實例為大家分享了Android實現數獨游戲的具體代碼,供大家參考,具體內容如下

1、在src中有4個Java類:

其中代碼分別是:
Game.java:

package com.example.test1;  import android.R.integer;  public class Game {   public final String str="360000000004230800000004200"           +"070460003820000014500013020"           +"001900000007048300000000045";   /*public final String str="124576893"+"967348521"+"835291674"+           "259784316"+"316952748"+"748613902"+"582439160"+"493167285"+"671825430";*/   static int sudoku[]=new int [9*9];      private int used[][][]=new int[9][9][];   int sum=0;   public int[] getSudoku(){     return sudoku;   }   public int sum(int a[]){     for(int i=0;i<a.length;i++)       sum+=a[i];     return sum;   }   public Game()   {       sudoku=fromPuzzleString(str);              calculateAllUesdTiles();   }     //  根據九宮格中的坐標,返回該坐標,所應該返回的數字   public int getTile(int x,int y){       return sudoku[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 [src.length()];       for(int i=0;i<sudo.length;i++)       {           sudo[i]=src.charAt(i)-'0';       }       return sudo;   }      //用于計算所有單元格對應的不可用數據   public void calculateAllUesdTiles()   {       for(int x=0;x<9;x++)       {           for(int y=0;y<9;y++)           {               used[x][y]=calculateUesdTiles(x,y);           }       }   }      //取出某一單元格當中已經不可用的數據   public int[] getUsedTileByCoor(int x, int y)   {       return used[x][y];   }    //計算某一單元格當中不可用的數據   private int[] calculateUesdTiles(int x,int y) {       // TODO Auto-generated method stub       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;           }       }       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;   }   protected boolean setTileIfValid(int x,int y,int value)   {       int tiles[]=getUsedTiles(x,y);       if(value !=0)       {           for(int tile:tiles)           {               if(tile==value)                   return false;           }       }       setTile(x,y,value);       calculateAllUesdTiles();       return true;          }    private int[] getUsedTiles(int x, int y) {       return used[x][y];   }   private void setTile(int x,int y,int value)   {       sudoku[y*9+x]=value;   }  } 

KeyDialog.java:

package com.example.test1;  import android.app.Dialog; import android.content.Context; import android.os.Bundle; import android.view.View;  //該類用于實現Dialog,實現自定義的對話框功能 public class KeyDialog extends Dialog{     //用來存放代表對話框中按鈕的對象     private final View keys[]=new View[9];     private final int used[];     private ShuduView myView;     //構造函數的第二個參數當中保存著當前單元格已經使用過的數字     public KeyDialog(Context context,int[] used,ShuduView myView) {         super(context);         this.used=used;         this.myView=myView;     }     //當一個Dialog第一次顯示的時候,會調用Oncreate方法     @Override     protected void onCreate(Bundle savedInstanceState) {         // TODO Auto-generated method stub         super.onCreate(savedInstanceState);         //設置標題         setTitle("KeyDialog");         //用于為該Dialog設置布局文件         setContentView(R.layout.keypad);         findViews();                  //遍歷整個used數組         for(int i=0;i<used.length;i++)         {             if(used[i]!=0)             {                 keys[used[i]-1].setVisibility(View.INVISIBLE);             }         }         setListeners();     }      private void findViews()     {         keys[0]=findViewById(R.id.keypad_1);         keys[1]=findViewById(R.id.keypad_2);         keys[2]=findViewById(R.id.keypad_3);         keys[3]=findViewById(R.id.keypad_4);         keys[4]=findViewById(R.id.keypad_5);         keys[5]=findViewById(R.id.keypad_6);         keys[6]=findViewById(R.id.keypad_7);         keys[7]=findViewById(R.id.keypad_8);         keys[8]=findViewById(R.id.keypad_9);              }     //通知MyView對象,刷新整個九宮格顯示的數據     private void returnResult(int tile)     {         myView.setSelectedTile(tile);         //取消對話框顯示         dismiss();     }          private void setListeners(){         //遍歷整個keys數組         for(int i=0;i<keys.length;i++)         {             final int t=i+1;             keys[i].setOnClickListener(new View.OnClickListener() {                                  @Override                 public void onClick(View v) {                     // TODO Auto-generated method stub                     returnResult(t);                 }             });         }     } } 

ShuduView.java:

package com.example.test1;  import android.R.integer; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.FontMetrics; import android.graphics.Paint.FontMetricsInt; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.widget.TextView;  public class ShuduView extends View{   private float width;   private float height;   int selectedX;   int selectedY;   private Game game=new Game();   public ShuduView(Context context) {       super(context);       // TODO Auto-generated constructor stub   }       @Override   protected void onSizeChanged(int w, int h, int oldw, int oldh) {       // TODO Auto-generated method stub       this.width=w/9f;       this.height=h/9f;       super.onSizeChanged(w, h, oldw, oldh);   }     @Override   protected void onDraw(Canvas canvas) {       // TODO Auto-generated method stub       //繪制背景顏色       Paint background_paint = new Paint();       background_paint.setColor(getResources().getColor(R.color.shudu_background));       canvas.drawRect(0, 0, getWidth(), getHeight(), background_paint);              Paint white=new Paint();       white.setColor(getResources().getColor(R.color.shudu_hilite));              Paint light=new Paint();       light.setColor(getResources().getColor(R.color.shudu_light));              Paint dark=new Paint();       dark.setColor(getResources().getColor(R.color.shudu_dark));              for(int i=0;i<9;i++)       {           //畫出橫向的線           canvas.drawLine(0, i*height, getHeight(), i*height, light);           canvas.drawLine(0, i*height+1, getHeight(), i*height+1, white);           //畫出縱向的線           canvas.drawLine( i*width,0, i*width,getHeight(), light);           canvas.drawLine( i*width+1,0, i*width+1, getHeight(), white);       }       for(int i=0;i<9;i++)       {           if(i%3!=0)           {               continue;           }           canvas.drawLine(0, i*height, getHeight(), i*height, dark);           canvas.drawLine(0, i*height+1, getHeight(), i*height+1, white);           //畫出縱向的線           canvas.drawLine( i*width,0, i*width,getHeight(), dark);           canvas.drawLine( i*width+1,0, i*width+1, getHeight(), white);       }              Paint number_paint=new Paint();       number_paint.setColor(Color.BLACK);       //number_paint.setStyle(Paint.Style.STROKE);       number_paint.setTextSize(height*0.75f);       number_paint.setTextAlign(Paint.Align.CENTER);              FontMetrics fm=number_paint.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(game.getTileString(i, j), width*i+x, height*j+y, number_paint);           }       }              super.onDraw(canvas);   }      @Override   public boolean onTouchEvent(MotionEvent event) {       // TODO Auto-generated method stub       if(event.getAction()!=MotionEvent.ACTION_DOWN)       {           return super.onTouchEvent(event);       }       selectedX=(int)(event.getX()/width);       selectedY=(int)(event.getY()/height);              int used[]=game.getUsedTileByCoor(selectedX, selectedY);       int sum=0;       int sumNumber=0;       sumNumber=game.sum(game.getSudoku());       if(sumNumber==45*9){         AlertDialog.Builder builder=new AlertDialog.Builder(getContext());         builder.setMessage("Success!")         .setPositiveButton("Exit", new DialogInterface.OnClickListener() {                      @Override           public void onClick(DialogInterface arg0, int arg1) {             arg0.cancel();                        }         });         AlertDialog alertDialog=builder.create();         alertDialog.show();        }       else {         for(int i=0;i<used.length;i++){           sum+=used[i];         }         if(sum==45){           AlertDialog.Builder builder=new AlertDialog.Builder(getContext());           builder.setMessage("No Number!")           .setNegativeButton("Exit", new DialogInterface.OnClickListener() {                          @Override             public void onClick(DialogInterface arg0, int arg1) {               arg0.cancel();                            }           });           AlertDialog alertDialog=builder.create();           alertDialog.show();              }         else {           StringBuffer sb=new StringBuffer();           for(int i=0;i<used.length;i++)           {               sb.append(used[i]); //              System.out.println(used[i]);           }            KeyDialog keyDialog= new KeyDialog(getContext(),used,this);            keyDialog.show();         }       }              /*StringBuffer sb=new StringBuffer();       for(int i=0;i<used.length;i++)       {           sb.append(used[i]); //          System.out.println(used[i]);       }*/        //      //生成一個LayoutInflater對象 //      LayoutInflater layoutInflater=LayoutInflater.from(this.getContext()); //      //使用LayoutInflater對象根據一個布局文件,生成一個View //      View layoutView=layoutInflater.inflate(R.layout.dialog, null); //      //生成好的TextView當中,取出響應的控件 //      TextView textView=(TextView)layoutView.findViewById(R.id.usedTextId); //      //設置Textview內容 //      textView.setText(sb.toString()); //      //生成一個對話框的Builder對象 //      AlertDialog.Builder builder=new AlertDialog.Builder(this.getContext()); //      //設置對話框的布局 //      builder.setView(layoutView); //      //創建一個對話框 //      AlertDialog dialog=builder.create(); //      //顯示對話框 //      dialog.show();       /* KeyDialog keyDialog= new KeyDialog(getContext(),used,this);       keyDialog.show();*/       return true;   }     public void setSelectedTile(int tile) {       // TODO Auto-generated method stub       if(game.setTileIfValid(selectedX,selectedY,tile)){           invalidate();       }   }  } 

MainActivity.java:

package com.example.test1;  import android.os.Bundle; import android.app.Activity; import android.view.Menu;  public class MainActivity extends Activity {    @Override   protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     ShuduView sView=new ShuduView(this);     setContentView(sView);               //setContentView(R.layout.activity_main);   }     @Override   public boolean onCreateOptionsMenu(Menu menu) {     // Inflate the menu; this adds items to the action bar if it is present.     getMenuInflater().inflate(R.menu.main, menu);     return true;   }    } 

2、布局Layout中的配置文件:

其中activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"   xmlns:tools="http://schemas.android.com/tools"   android:layout_width="match_parent"   android:layout_height="match_parent"   android:paddingBottom="@dimen/activity_vertical_margin"   android:paddingLeft="@dimen/activity_horizontal_margin"   android:paddingRight="@dimen/activity_horizontal_margin"   android:paddingTop="@dimen/activity_vertical_margin"   tools:context=".MainActivity" >    <TextView     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="@string/hello_world" />  </RelativeLayout> 

dialog.xml:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   android:layout_width="match_parent"   android:layout_height="match_parent"   android:orientation="vertical" >      <TextView      android:id="@+id/usedTextId"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="@string/hello_world"     />  </LinearLayout> 

keypad.xml:

<?xml version="1.0" encoding="utf-8"?> <!-- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   android:layout_width="match_parent"   android:layout_height="match_parent"   android:orientation="vertical" >        </LinearLayout> -->  <TableLayout   xmlns:android="http://schemas.android.com/apk/res/android"   android:id="@+id/keypad"   android:orientation="vertical"   android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:stretchColumns="*">   <TableRow>     <Button android:id="@+id/keypad_1"       android:text="1"/>     <Button android:id="@+id/keypad_2"       android:text="2"/>     <Button android:id="@+id/keypad_3"       android:text="3"/>   </TableRow>     <TableRow>     <Button android:id="@+id/keypad_4"       android:text="4"/>     <Button android:id="@+id/keypad_5"       android:text="5"/>     <Button android:id="@+id/keypad_6"       android:text="6"/>   </TableRow>     <TableRow>     <Button android:id="@+id/keypad_7"       android:text="7"/>     <Button android:id="@+id/keypad_8"       android:text="8"/>     <Button android:id="@+id/keypad_9"       android:text="9"/>   </TableRow> </TableLayout> 

3、values中的配置文件
就只有color.xml是自己寫的,另外三個都是自動生成
color.xml

<?xml version="1.0" encoding="utf-8"?> <resources>     <color name="shudu_background">#ffe6f0ff</color>   <color name="shudu_dark">#64ff0000</color>   <color name="shudu_light">#64ffffff</color>   <color name="shudu_hilite">#6400ff00</color> </resources> 

4、一切就緒直接運行就0k

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 泽普县| 土默特左旗| 台安县| 南康市| 务川| 同仁县| 阿合奇县| 沭阳县| SHOW| 江北区| 高安市| 金秀| 河间市| 繁昌县| 博罗县| 阿坝县| 庆阳市| 聂拉木县| 泽州县| 巫溪县| 高州市| 安徽省| 盘山县| 英德市| 旬邑县| 乐山市| 鞍山市| 泰顺县| 岚皋县| 广平县| 松阳县| 连云港市| 竹北市| 泽州县| 香港| 甘泉县| 宜城市| 延吉市| 张家口市| 宣恩县| 裕民县|