数独游戏(2)之单点触摸事件,碰撞检测,计算出不可用数据
1)单点触摸事件
就是手触摸屏幕时,根据操作的行为来激发某事件
需要重写view父类的 public boolean onTouchEvent(MotionEvent event) 方法
package com.soduku;
import com.soduku.R;
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);
}
//鼠标(手) 触动 手机屏幕 事件,当 手 触动 该 view 时 该函数会被调用
@Override
public boolean onTouchEvent(MotionEvent event) {
//判断 手 触摸屏幕的 动作(点击,滑动等等),来进行流程控制,作出相应的操作
if( event.getAction() != event.ACTION_DOWN )
{
return super.onTouchEvent(event);
}
//获取 点击 哪个单元格的坐标
int selectedX = (int)(event.getX()/width) ;
int selectedY = (int)(event.getY()/height) ;
int used[] = game.getUsedTilesByCoor(selectedX, selectedY);
//把获取某单元格 不可用的数据 打印出来
for(int i = 0 ; i < used.length ; i ++)
{
System.out.println(used[i]);
}
return true;
}
}
2)碰撞检测
这里通过
获取单元格的坐标
3)计算出不可用数据
这里定义了一个三维数组,来存放已经不可用的数据
具体计算某个单元格中,不可用的数据(包括整行,整列,一个九宫格中,都不可出现重复的数字),如下
1)计算 该单元格中 《列》上不可用的数据
2)计算 该单元格中 《行》上不可用的数据
3)用于计算一个《九宫格 里面》不可用的数据
4)对于一些没有赋上值数组元素进行压缩删除
具体代码:
package com.soduku;
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;
}
}
MainActivity类
package com.soduku;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new ShuduView(this));
}
}
其他的,比如colors.xml这些可参考 http://blog.youkuaiyun.com/hzc543806053/article/details/7675126