Android数独游戏03

来源:互联网 发布:java高级程序设计图片 编辑:程序博客网 时间:2024/05/21 17:07

只是新修改了一个类 Game.java

package com.example.shudu;public class Game {//数独初始化数据的基础   先将字符串分给数组  然后根据数组 单个取出字符  转换为String类型private final String str = "360000000004230800000004200"  +"070460003820000014500013020"  +"001900000007048300000000045";private int num[] = new int[9*9];//用于存储每个单元格已经不可用的数据private int used[][][] = new int[9][9][];public Game(){num = fromString(str);calAllUsedNums();// 我说为什么是空指针错呢,,,,,因为就是空的}//就是根据x 坐标和 y坐标  来完成 单元格内数字的获取 通过 num数组private int getNum(int x,int y){return num[y*9+x];//体现出以0开头的巨大优势}public String getLocaString(int x,int y){int v = getNum(x,y);if(v==0){return "";}else{    return String.valueOf(v);}}private int[] fromString(String str){//其实这样声明数组更加合理  比如 int*;  只不过C语言当中  直接 int a[5]; 不需要newint[] num = new int[str.length()];for(int i=0;i<str.length();i++){//利用charAt(int i) 方法取出单个字符 并 相减 获取  int 对象 num[i] = str.charAt(i)-'0';}return num;}//用于计算所有单元格对应的不可用数据public void calAllUsedNums(){for(int x=0;x<9;x++){for(int y=0;y<9;y++){used[x][y] = calUsedNums(x, y);}}}//获得对应单元格当中不可用的数组public int[] getUsedNums(int x,int y){return used[x][y];}//计算某一单元格当中已经不可用的数据public int[] calUsedNums(int x,int y){int c[] = new int[9];// int[] c = new int[9];for(int i=0;i<9;i++){if(i==x){continue;//其实这句话是可以不要的  当在这个位置填上数 再次修改的时候需要}int t = getNum(i,y);if(t!=0){c[t-1] = t; //其实就是设置为不等于0  设置成1也行}}for(int i=0;i<9;i++){if(i==y){continue;}int t = getNum(x,i);if(t!=0){c[t-1] = t;//数组生成的时候默认是0的}}//还是出了小问题 不够细致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 = getNum(i,j);if(t!=0){c[t-1] = t; //我这样做似乎效率并不高的 }}}//经过上面这三个循环,横向,纵向的九宫格的数字都存放在C数组当中了//说什么关于压缩数组。。。。 java 1.5  就有这种方法了 汗 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;}}//理解了不,c才是数组对象  return c1;}}

ShuduView.java

package com.example.shudu;import android.app.AlertDialog;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Paint.FontMetrics;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;private Game game = new  Game();public ShuduView(Context context) {super(context);}@Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh) {//计算当前 单元格的宽度和高度this.width = w/9f;this.height = h/9f;super.onSizeChanged(w, h, oldw, oldh);}@Overrideprotected void onDraw(Canvas canvas) {//生成用于绘制当前 背景色的画笔 Paint backgroundPaint = new Paint();//设置背景颜色画笔的颜色backgroundPaint.setColor(getResources().getColor(R.color.shudu_background));//绘制背景色 左边界都是0  右边界位置是宽下边界是高 覆盖整个屏幕canvas.drawRect(0,0,getWidth(),getHeight(),backgroundPaint);Paint darkPaint = new Paint();darkPaint.setColor(getResources().getColor(R.color.shudu_dark));Paint hilitePaint = new Paint();hilitePaint.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, hilitePaint);canvas.drawLine(i*width,0,i*width,getHeight(),lightPaint);canvas.drawLine(i*width+1,0,i*width+1,getHeight(),hilitePaint);}for(int i=0;i<9;i++){if(i%3!=0){continue;}canvas.drawLine(0,i*height,getWidth(),i*height, darkPaint);canvas.drawLine(0,i*height+1,getWidth(),i*height+1, hilitePaint);canvas.drawLine(i*width,0,i*width,getHeight(),darkPaint);canvas.drawLine(i*width+1,0,i*width+1,getHeight(),hilitePaint);}//绘制文字Paint  numberPaint = new Paint();numberPaint.setColor(Color.BLACK);//设置空心numberPaint.setStyle(Paint.Style.STROKE);//设置文字大小为0.75 单元格 大小numberPaint.setTextSize(height*0.75f);//设置文字居中对齐numberPaint.setTextAlign(Paint.Align.CENTER);FontMetrics fm =numberPaint.getFontMetrics();float x = width/2;float y = height/2-(fm.ascent+fm.descent)/2;System.out.println(y);//x默认是‘3’这个字符的左边在屏幕的位置,如果设置了//paint.setTextAlign(Paint.Align.CENTER);//那就是字符的中心,y是指定这个字符baseline在屏幕上的位置for(int i=0;i<9;i++)for(int j=0;j<9;j++){//将getLocaString方法声明成public是有必要的  0 是空字符串 所以不显示的canvas.drawText(game.getLocaString(i,j),i*width+x,j*height+y, numberPaint);}super.onDraw(canvas);}@Overridepublic boolean onTouchEvent(MotionEvent event) {if(event.getAction() !=MotionEvent.ACTION_DOWN ){return super.onTouchEvent(event);//其实return true;也是一样的}//返回值是float类型的int selectX = (int)(event.getX()/width);int selectY = (int)(event.getY()/height);int used[] =  game.getUsedNums(selectX, selectY);for(int i=0;i<used.length;i++){//byte   int  都是lengthSystem.out.println(used[i]);}StringBuffer  sb = new StringBuffer();for(int i=0;i<used.length;i++){sb.append(used[i]);}//生成一个LayoutInflater对象//LayoutInflater  layoutInflater = LayoutInflater.from(ShuduView.this);  这样写还是不行的LayoutInflater  layoutInflater = LayoutInflater.from(this.getContext()); //使用LayoutInflater 对象根据一个布局文件 生成一个布局文件View layoutView = layoutInflater.inflate(R.layout.dialog,null);//从生成好的layoutView当中,取出相应的控件TextView textView = (TextView)layoutView.findViewById(R.id.usedTextId);//设置textView的内容为已经使用的内容为哪些textView.setText(sb.toString());//生成一个对话框当的Builder对象AlertDialog.Builderbuilder = new AlertDialog.Builder(this.getContext());//设置对话框所显示的内容builder.setView(layoutView);//生成对话框对象,并将其显示出来AlertDialog dialog = builder.create();dialog.show();//return super.onTouchEvent(event);return true;//用于设置回调函数一直处于等待调用状态}}


原创粉丝点击