數獨遊戲-安卓版源代碼和分析。

1.在創建的項目中,主要有以下類,其中,

Game.java 是數獨遊戲的算法:

keydialog:主要控制數據九宮格的顯示和操作

MainActivity:主要加載主頁面,代碼只需改動一個setcontentview。

shuduView:主要是調用各個功能函數,和實現具體操作。



(1)shuduView.Java源代碼如下:

package com.lien.lien_shudu;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.view.MotionEvent;
import android.view.View;

public class ShuduView extends View {
	
	//單元格的寬度和高度
	private float width ;
	private float height ;
	
	private Game game = new Game();
	
	public ShuduView(Context context)
	{
		super(context);
	}
	
	//w:整個 veiw 的寬度;  h:整個 veiw 的高度

	@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
	public void onDraw(Canvas canvas) {
		
		
		
		Paint backgroundPaint = new Paint();
		//從配置文件中 獲取顏色值
		backgroundPaint.setColor(this.getResources().getColor(R.color.shudu_background));
		//畫出整個手機屏幕的 的背景色
		canvas.drawRect(0, 0, this.getWidth(), this.getHeight(), backgroundPaint);
		
		//----------------------------------------
		
		Paint darkPaint = new Paint();
		darkPaint.setColor(this.getResources().getColor(R.color.shudu_dark));
		
		Paint hilitePaint = new Paint();
		hilitePaint.setColor(this.getResources().getColor(R.color.shudu_hilite));
		
		Paint lightPaint = new Paint();
		lightPaint.setColor(this.getResources().getColor(R.color.shudu_light));
		
		//畫九宮格里面的 橫線,縱線,每次畫出的線要想達到某種效果,需畫兩條 之間格1像素的位置,且顏色也要搭配好
		for(int i = 0 ; i < 9 ; i++ )
		{
			canvas.drawLine(0, i*height, this.getWidth(), i*height, lightPaint);
			canvas.drawLine(0, i*height+1, this.getWidth(), i*height+1, hilitePaint);
		    
			canvas.drawLine(i*width,0, i*width, this.getHeight(), lightPaint);
			canvas.drawLine(i*width+1,0, i*width+1, this.getHeight(), hilitePaint);
		}
		
		//把整個 屏幕的格子 分成9個大的 9 宮格,每個大的9宮格 裏面又有9個小格,   實際上就是 用顏色比較深的線隔開
		for(int i = 0 ; i < 9 ; i ++)
		{
			if(i%3 != 0)
			{
				continue ;
			}
			
			canvas.drawLine(0, i*height, this.getWidth(), i*height, darkPaint);
			canvas.drawLine(0, i*height+1, this.getWidth(), i*height+1, hilitePaint);
		    
			canvas.drawLine(i*width,0, i*width, this.getHeight(), darkPaint);
			canvas.drawLine(i*width+1,0, i*width+1, this.getHeight(), hilitePaint);
			
		}
		
		//設置在表格上顯示的數字
		Paint numberPaint = new Paint();
		
		numberPaint.setColor(Color.BLACK);
		numberPaint.setStyle(Paint.Style.STROKE); //讓其畫出來的東西是 空的
		numberPaint.setTextSize(height*0.75f); //設置字體大小
		numberPaint.setTextAlign(Paint.Align.CENTER); //讓字體居中
		
		float x = width/2f ;
		//調整字體的位置 ( 度量)  比如居中,調整垂直方向上的居中
		FontMetrics fm = numberPaint.getFontMetrics();
		// 這些  fm.ascent 都是基於 基準線 而言
		float y = height/2f - (fm.ascent+fm.descent)/2 ;
		//System.out.println("y:"+y+"fm.ascent:"+fm.ascent+"fm.descent:"+fm.descent);
		
		//初始化數據
		for(int i = 0 ; i < 9 ; i ++)
		{
			for(int j = 0 ; j < 9 ; j ++)
			{
				
				canvas.drawText(game.getTileString(i, j), i*width+x,j*height+y , numberPaint);
			}
		}
		
		super.onDraw(canvas);
	}
	
	int selectedX ;
	int selectedY ;
	//鼠標(手) 觸動 手機屏幕 事件,當 手 觸動 該 view 時 該函數會被調用
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		
		if( event.getAction() != event.ACTION_DOWN )
		{
			return super.onTouchEvent(event);
		}
		
		//獲取 點擊 哪個單元格的座標
		selectedX = (int)(event.getX()/width) ;
		selectedY = (int)(event.getY()/height) ;
		
		//把獲取某單元格 不可用的數據
		int used[] = game.getUsedTilesByCoor(selectedX, selectedY);
		
		//把獲取某單元格 不可用的數據 打印出來
//		for(int i = 0 ; i < used.length ; i ++)
//		{
//			System.out.println(used[i]);
//		}
		
		KeyDialog keyDialog = new KeyDialog(this.getContext(),used,this) ;
		
		keyDialog.show() ;
		
		
		
		return true;
	}
	
	//刷新整個九宮格里的數據
	public void setSelectedTile(int tile)
	{
		if(game.setTileIfValid(selectedX, selectedY, tile))
		{
			invalidate();//重新繪製整個視圖,也就相當於 重新生成一次該對象
		}
	}

}<span style="color:#000099;background-color: rgb(255, 0, 0);">
</span>
</pre><h1><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 0, 0);"><span style="color:#000099;">(2)Game.Java源代碼如下</span></span></h1><pre name="code" class="java">
</pre><pre name="code" class="java">package com.lien.lien_shudu;


public class Game {
	
	//初始化 九宮格的數據
	private final String initStr =
			"360000000004230800000004200"+
	        "070460003820000014500013020"+
			"001900000007048300000000045";
	//定義一個 數組 存放 初始化數據,首先 要將 initStr裏面的數據分離開,存放在數組裏
	private int[] shuduku = new int[9*9] ;
	
	//用於存儲 已經使用過的 數據
	private int[][][] used = new int[9][9][]  ;
	
	public Game()
	{
		shuduku = fromPuzzleString(initStr) ;	
		calculateAllUsedTiles() ; //一創建對象的時候,就把每個單元格中 不可用的數據存到 三維數組裏
	}
	
	//通過傳來的座標值,獲取 該座標 的 具體值(整數)
	private int getTile(int x , int y)
	{
		return shuduku[y*9+x] ;
	}
	
	public String getTileString(int x , int y)
	{
		int v = getTile(x , y);
		if(0 == v)
			return "" ;
		else

		    return String.valueOf(v); //把獲取的 整數 轉成 字符串
	}
	
	//把字符串 一個個分離出來,存放至 shudu數組中,通過返回值,賦值給 shuduku數組中
	public int[] fromPuzzleString(String str)
	{
		int[] shudu = new int[str.length()] ;
		
		for(int i = 0 ; i < str.length() ; i++)
		{
			shudu[i] = str.charAt(i) - '0' ; //把獲取的單個字符減去 '0' 轉成整數,賦給 整形 shudu數組中
		}
		
		return shudu ;
	}
	
	//用於計算 所有單元格中 不可用的 數據,返回一個 一維數組賦值給 三維數組 
	public void calculateAllUsedTiles()
	{
		for(int i = 0 ; i < 9 ; i++)
		{
			for(int j = 0 ; j < 9 ; j ++)
			{
				// 這裏的 數組 賦值,只要 它們 加起來 是 三維 即可,假如是 四維 也一樣
				used[i][j] = calculateUsedTiles(i , j) ;
			}
		}
	}
	
	// 取出某個單元格中 不可用的 數
	public int[] getUsedTilesByCoor(int x , int y )
	{
		return used[x][y] ;
	}
	
	
	//用於計算  某一單元格 已經不可用的 數據
	public int[] calculateUsedTiles(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 ;
			}
		}
		
		// 經過 上面的程序 檢測出不可用的數據,存在c數組中,由於還有一些沒有 賦值,默認爲 0,需要進行壓縮,把默認設置爲0的數組值,進行刪除
		
		// 把數組裏面爲0 的數 過濾 掉,用 nused 來標識有幾個不爲0的數
		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;
	}
	//把用戶選的字,設置到 soduku數組中
	public boolean setTileIfValid(int x , int y , int value)
	{
		// 取出某個單元格中 不可用的 數
		int tiles[] = getUsedTilesByCoor(x, y);
		
		if(value != 0)
		{
			for(int tile : tiles)
			{
				//如果用戶選的數字和 “ 不可用數組” 裏面的數字相同,則返回false,
				if(tile == value)
				{
					return false ;
				}
			}
		}
		
		setTile(x,y,value) ;
		
		//每次修改之後都得 進行重新計算 不可用的值
		calculateAllUsedTiles() ;
		
		return true;
	}
	//進行修改 數獨數組 裏的值
	private void setTile(int x , int y , int value)
	{
		shuduku[9*y+x] = value ;
	}

}
</pre><pre name="code" class="java">
</pre><pre name="code" class="java">

(3)keyDialog.Java源代碼如下:

</pre><pre name="code" class="java">
package com.lien.lien_test;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;

public class KeyDialog extends Dialog{

	private final View keys[]=new View[9];//用來存放代表對話框當中按鈕的對象
	private final int used[];
	private shuduTest shuduTest=null;
	//used【】保存着當前單元格已經使用過的數字。
	public KeyDialog(Context context,int[]used,shuduTest shuduTest) {
		super(context);
		this.used=used;
		this.shuduTest=shuduTest;
		
		
	}

	
	//當一個dialog第一次顯示的時候,會調用其oncreate方法
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		//設置對話框的標題
		setTitle("lien warnning ! ! !");
		//用於爲該dialog設置佈局文件。
		setContentView(R.layout.keypad);
		findViews();//把按鈕對象添加到keys數組中  
		
		
		//遍歷整個used數組,已經使用過的數字就讓它隱藏。
		for(int i=0;i<used.length;i++){
			if(used[i]!=0){
				keys[used[i]].setVisibility(View.INVISIBLE);
			}
		}
		//爲對話框當中所有的按鈕設置監聽器
		setListeners();
	}

	


	//爲每一個按鈕設置一個ID,然後方便找到
	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);
		
		
	}
	
	
	
	
	
private void setListeners() {
		
		//遍歷整個數組。
		for(int i=0;i<keys.length;i++){
			
			final int t=i+1;//用T表示用戶點擊的數字。
			//爲每一個按鈕設置監聽器。
			keys[i].setOnClickListener(new View.OnClickListener() {
				
			
				public void onClick(View v) {
					returnResult(t);
					
				}
				
				
				
			});
		}
	}




//通知shuduTest對象,刷新整個九宮格顯示的數據。
	public void returnResult(int tile) {
		shuduTest.setSelectedTile(tile);//把用戶點擊的數字傳到setSelectedTile函數裏面。
		dismiss();//取消對話框的顯示。
		
	}
}


(4)MainActivity.Java只需修改setcontentView代碼就可以了。

(5)佈局文件keypad.xml源代碼如下。

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/keypad"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:orientation="vertical" >  
      
    <TableRow>  
          
        <Button android:id="@+id/keypad_1"  
            android:text="1">  
        </Button>  
          
        <Button android:id="@+id/keypad_2"   
            android:text="2">  
        </Button>  
          
        <Button android:id="@+id/keypad_3"   
            android:text="3">  
        </Button>  
          
    </TableRow>  
       
    <TableRow>  
        <Button android:id="@+id/keypad_4"   
            android:text="4">  
        </Button>  
          
        <Button android:id="@+id/keypad_5"   
            android:text="5">  
        </Button>  
          
        <Button android:id="@+id/keypad_6"   
            android:text="6">  
        </Button>  
    </TableRow>  
                
    <TableRow>  
        <Button android:id="@+id/keypad_7"   
            android:text="7">  
        </Button>  
          
        <Button android:id="@+id/keypad_8"   
            android:text="8">  
        </Button>  
          
        <Button android:id="@+id/keypad_9"   
            android:text="9">  
        </Button>  
    </TableRow>  
  
    <TableRow>  
          
        <Button  
            android:text="確定"   
        >            
        </Button>  
          
        <Button  
            android:text="LMN"   
        >            
        </Button>  
          
        <Button android:id="@+id/back_1"  
            android:text="返回"   
        >            
        </Button>  
    </TableRow>  
  
</TableLayout>  
<span style="font-family: Arial, Helvetica, sans-serif;">
</span>
<span style="font-family: Arial, Helvetica, sans-serif;">
</span>

(6)佈局文件dialog.xml源代碼如下。

<span style="font-family: Arial, Helvetica, sans-serif;">
</span>
<span style="font-family: Arial, Helvetica, sans-serif;">
</span>
<?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"
        />

</LinearLayout>

(7)剩餘部分</span>



剩下的都是一些定義顏色和string之類的,有了以上代碼,自己稍加修改就可以成功了,

<span style="font-size:24px;"><strong>
</strong></span>
<span style="font-size:24px;"><strong>運行結果如下圖所示:</strong></span>





介紹結束!!!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章