第五课时:
1、单点触摸事件
publie boolean onTouchEvent(MotionEven event){
//获取事件的种类
event.getAction();
//获取点击的坐标
event.getX();
event.getY();
}
2、碰撞检测
判断点击的方位。
3、可用数据计算
4、总结
第一步:Game
package com.example.shudu_1;
public class Game {
private final String str="360000000"+
"004230800"+
"000004200"+
"070460003"+
"820000014"+
"500013020"+
"001900000"+
"007048300"+
"000000045";
private int sudoku[]=new int[9*9];
//用于存储每个单元格已经不可用的数据
private int used[][][]=new int[9][9][];
public Game(){
sudoku=fromPuzzleString(str);
calculateAllUsedTiles();
}
//用于计算所有单元格对应的不可用数据
private void calculateAllUsedTiles() {
for(int x=0;x<9;x++){
for(int y=0;y<9;++y){
used[x][y]=calculateUsedTiles(x, y);
}
}
}
//取出某一个单元格中已经不可用的数据
public int[]getUsedTilesByCoor(int x,int y){
return used[x][y];
}
private 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);
}
private 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 int[] calculateUsedTiles(int x,int y){
int c[]=new int[9];
//遍历y轴上不能出现的数字
for(int i=0;i<9;++i){
if(i==y){
continue;
}
int t=getTile(x, i);
if(t!=0){
c[t-1]=t;
}
}
//遍历x轴上不能出现的数字
for(int i=0;i<9;++i){
if(i==x){
continue;
}
int t=getTile(i, y);
if(t!=0)
c[t-1]=t;
}
//小的9单元格也是不能重复的数字
int startx=(x/3)*3;
int starty=(y/3)*3;
for(int i=0;i<startx+3;++i){
for(int j=0;j<starty+3;++j){
if(i==x&&j==y){
continue;
}
int t=getTile(i, j);
if(t!=0)
c[t-1]=t;
}
}
//compress
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;
}
}
第二部:MyView
package com.example.shudu_1;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.support.v4.view.PagerAdapter;
import android.text.style.LineHeightSpan.WithDensity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
public class MyView extends View {
//设置单元格的宽度和高度
private float width;
private float height;
private Game game=new Game();
public MyView(Context context) {
super(context);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
//计算当前单元格的高度和宽度
this.width=w/9f;
this.height=h/9f;
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas) {
//生成用于绘制背景色的画笔
Paint backgroundPaint=new Paint();
//设置背景的颜色
backgroundPaint.setColor(getResources().getColor(R.color.shudu_background));
//绘制背景色
canvas.drawRect(0, 0, getWidth(), getHeight(), backgroundPaint);
Paint darkPain=new Paint();
darkPain.setColor(getResources().getColor(R.color.shudu_dark));
Paint hiliPaint=new Paint();
hiliPaint.setColor(getResources().getColor(R.color.shudu_hilite));
Paint lightPaint=new Paint();
lightPaint.setColor(getResources().getColor(R.color.shudu_light));
for(int i=0;i<9;++i){
//以下两行代码用户绘制横向的单元格
//明线和黑线才能合成一条雕刻的线
canvas.drawLine(0, i*height, getWidth(), i*height,lightPaint);
canvas.drawLine(0, i*height+1, getWidth(), i*height+1,hiliPaint);
//纵向
canvas.drawLine(i*width, 0, i*width, getHeight(), lightPaint);
canvas.drawLine(i*width+1, 0, i*width+1, getHeight(), hiliPaint);
}
//绘制黑线
for(int i=0;i<9;++i){
if(i%3!=0){
continue;
}
//以下两行代码用户绘制横向的单元格
//明线和黑线才能合成一条雕刻的线
canvas.drawLine(0, i*height, getWidth(), i*height,darkPain);
canvas.drawLine(0, i*height+1, getWidth(), i*height+1,hiliPaint);
//纵向
canvas.drawLine(i*width, 0, i*width, getHeight(), darkPain);
canvas.drawLine(i*width+1, 0, i*width+1, getHeight(), hiliPaint);
}
//绘制文字
Paint numberPain=new Paint();
numberPain.setColor(Color.BLACK);
numberPain.setStyle(Paint.Style.STROKE);
numberPain.setTextSize(height*0.75f);
numberPain.setTextAlign(Paint.Align.CENTER);
FontMetrics fm=numberPain.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), i*width+x, j*height+y, numberPain);
}
}
super.onDraw(canvas);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction()!=MotionEvent.ACTION_DOWN){
return super.onTouchEvent(event);
}
int selectedX=(int) (event.getX()/width);
int selectedY=(int)(event.getY()/height);
int used[]=game.getUsedTilesByCoor(selectedX, selectedY);
return true;
}
}