您好,登錄后才能下訂單哦!
小編給大家分享一下如何基于Android實(shí)現(xiàn)數(shù)獨(dú)游戲,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
具體內(nèi)容如下
1、在src中有4個(gè)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(); } // 根據(jù)九宮格中的坐標(biāo),返回該坐標(biāo),所應(yīng)該返回的數(shù)字 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); } } //根據(jù)一個(gè)字符串?dāng)?shù)據(jù),生成一個(gè)整型數(shù)組,作為數(shù)獨(dú)游戲的初始化數(shù)據(jù) 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; } //用于計(jì)算所有單元格對應(yīng)的不可用數(shù)據(jù) public void calculateAllUesdTiles() { for(int x=0;x<9;x++) { for(int y=0;y<9;y++) { used[x][y]=calculateUesdTiles(x,y); } } } //取出某一單元格當(dāng)中已經(jīng)不可用的數(shù)據(jù) public int[] getUsedTileByCoor(int x, int y) { return used[x][y]; } //計(jì)算某一單元格當(dāng)中不可用的數(shù)據(jù) 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; //該類用于實(shí)現(xiàn)Dialog,實(shí)現(xiàn)自定義的對話框功能 public class KeyDialog extends Dialog{ //用來存放代表對話框中按鈕的對象 private final View keys[]=new View[9]; private final int used[]; private ShuduView myView; //構(gòu)造函數(shù)的第二個(gè)參數(shù)當(dāng)中保存著當(dāng)前單元格已經(jīng)使用過的數(shù)字 public KeyDialog(Context context,int[] used,ShuduView myView) { super(context); this.used=used; this.myView=myView; } //當(dāng)一個(gè)Dialog第一次顯示的時(shí)候,會調(diào)用Oncreate方法 @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); //設(shè)置標(biāo)題 setTitle("KeyDialog"); //用于為該Dialog設(shè)置布局文件 setContentView(R.layout.keypad); findViews(); //遍歷整個(gè)used數(shù)組 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對象,刷新整個(gè)九宮格顯示的數(shù)據(jù) private void returnResult(int tile) { myView.setSelectedTile(tile); //取消對話框顯示 dismiss(); } private void setListeners(){ //遍歷整個(gè)keys數(shù)組 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]); }*/ // //生成一個(gè)LayoutInflater對象 // LayoutInflater layoutInflater=LayoutInflater.from(this.getContext()); // //使用LayoutInflater對象根據(jù)一個(gè)布局文件,生成一個(gè)View // View layoutView=layoutInflater.inflate(R.layout.dialog, null); // //生成好的TextView當(dāng)中,取出響應(yīng)的控件 // TextView textView=(TextView)layoutView.findViewById(R.id.usedTextId); // //設(shè)置Textview內(nèi)容 // textView.setText(sb.toString()); // //生成一個(gè)對話框的Builder對象 // AlertDialog.Builder builder=new AlertDialog.Builder(this.getContext()); // //設(shè)置對話框的布局 // builder.setView(layoutView); // //創(chuàng)建一個(gè)對話框 // 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是自己寫的,另外三個(gè)都是自動(dòng)生成
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、一切就緒直接運(yùn)行就0k
看完了這篇文章,相信你對“如何基于Android實(shí)現(xiàn)數(shù)獨(dú)游戲”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。