博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数独游戏设计与实现之第三篇——项目开发的过程
阅读量:4671 次
发布时间:2019-06-09

本文共 5591 字,大约阅读时间需要 18 分钟。

分成四个类:CrosswordsActivity,DialogView,Game,MainView

CrosswordsActivity(主类):

package com.Crosswords.Activity;import android.app.Activity;import android.os.Bundle;public class CrosswordsActivity extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(new MainView(this));    }}

 DialogView(对话框的一个类):

package com.Crosswords.Activity;import android.app.Dialog;import android.content.Context;import android.os.Bundle;import android.view.View;public class DialogView extends Dialog{    MainView mainview;   // View []views=new View[9];    View[] views=new  View[12];	int  []used;	public DialogView(Context context,int []used,MainView mainview) {		super(context);		this.used=used;		this.mainview=mainview;	}	@Override	public void onCreate(Bundle buildle){		super.onCreate(buildle);		setTitle("请输入数字");		setContentView(R.layout.dialog);		this.getView();		for(int i=0;i

 Game(数独的算法类):

package com.Crosswords.Activity;public class Game {	String str=			"500200008"+			"700008400"+			"000390060"+			"001630009"+			"000000654"+			"968400000"+			"005006071"+			"609005803"+			"080903046";	//全部的数字	int []crosswords=new int[9*9];	//各个坐标已经使用的数字	int [][][]usedwords=new int[9][9][];	//用来标记能够修改	int [][]flag =new int[9][9];	public Game() {		// TODO Auto-generated constructor stub		crosswords=getCrossword(str);		flag=getflag();		//GetAllUsed();	}	//得到所有已经使用数组	public void GetAllUsed(){		for(int i=0;i<9;i++){			for(int j=0;j<9;j++){				usedwords[i][j]=getUsedWords(i, j);			}		}					}		//用来计算单元格中已经使用的格子	public int[] getUsedWords(int x,int y){		int [] c=new int[9];		//计算一行		int temp;		for(int i=0;i<9;i++){			temp=gettitle(i, y);			if(x==i){				continue;			}else{				if(temp!=0){					c[temp-1]=temp;				}			}		}		//计算一列		for(int i=0;i<9;i++){			temp=gettitle(x, i);			if(y==i){				continue;			}else{				if(temp!=0){					c[temp-1]=temp;				}							}		}		//计算单元格		//这个表达式是为了求出点击事件的具体大方块位置		int stax=(x/3)*3;		int stay=(y/3)*3;		for(int i=stax;i

 MainView(计算单元格的一个类):

package com.Crosswords.Activity;import android.app.AlertDialog;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Paint.Align;import android.graphics.Paint.FontMetrics;import android.text.style.LineBackgroundSpan;import android.view.LayoutInflater;import android.view.MotionEvent;import android.view.View;import android.view.ViewDebug.FlagToString;import android.widget.TextView;public class MainView extends View{float width;float height;int selectedX;int selectedY;Game game=new Game();//画笔不能重用	public MainView(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		super.onSizeChanged(w, h, oldw, oldh);		//计算单元格		width=w/9;		height=h/9;	}	//画笔事件	@Override	protected void onDraw(Canvas canvas) {		// TODO Auto-generated method stub		super.onDraw(canvas);		Paint backgroundpaint=new Paint();		backgroundpaint.setColor(Color.parseColor("#C2E7C2"));		canvas.drawRect(0,0,getWidth(),getHeight(), backgroundpaint);			//#BFCDDB小格子颜色		Paint linepaint1=new Paint();		linepaint1.setColor(Color.parseColor("#BFCDDB"));		for (int i = 0; i < 10; i++) {			canvas.drawLine(0,i*getHeight()/9, getWidth(), i*getHeight()/9, linepaint1);			canvas.drawLine(i*getWidth()/9, 0, i*getWidth()/9, getHeight(), linepaint1);		}		Paint linepaint2=new Paint();		linepaint2.setColor(Color.parseColor("#3399FF"));		for(int i=0;i<4;i++){			canvas.drawLine(0, i*getHeight()/3, getWidth(), i*getHeight()/3, linepaint2);			canvas.drawLine(i*getWidth()/3, 0, i*getWidth()/3, getHeight(), linepaint2);		}		Paint numberpaint=new Paint();		numberpaint.setStyle(Paint.Style.STROKE);		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;		String text;		for (int i = 0; i < 9; i++) {			for(int j=0;j<9;j++){				text=game.gettitleString(i, j);				if(game.flag(i,j)==1){					numberpaint.setColor(Color.BLACK);					canvas.drawText(text, width*i+x, height*j+y, numberpaint);				}else{					numberpaint.setColor(Color.parseColor("#6B6743"));					canvas.drawText(text, width*i+x, height*j+y, numberpaint);				}			}		}			}	//监听点击面板事件	@Override	public boolean onTouchEvent(MotionEvent event){		if(event.getAction()!=MotionEvent.ACTION_DOWN){			return super.onTouchEvent(event);		}		selectedX=(int)(event.getX()/width);		selectedY=(int)(event.getY()/height);		int [] usedwords=game.getUsedWords(selectedX, selectedY);		StringBuffer usedstr=new StringBuffer();		for(int x:usedwords){			usedstr.append(x+" ");		}		//使用自定义的对话框		/*		LayoutInflater inflater=LayoutInflater.from(this.getContext());		View view=inflater.inflate(R.layout.dialog, null);		TextView text=(TextView)view.findViewById(R.id.used);		text.setText(usedstr.toString());		AlertDialog.Builder builder=new AlertDialog.Builder(this.getContext());		builder.setView(view);		AlertDialog  dialog=builder.create();		dialog.show();		*/		//判断坐标是否是原来的数独		if(game.flag(selectedX, selectedY)==1){			DialogView dialog=new DialogView(getContext(), usedwords,this);			dialog.show();		}		return true;	}	     public void SetSelectTitle(int title){             if(game.SetTitleValid(selectedX, selectedY, title)){		            	 //刷新页面            	 invalidate();         }	}}

 

转载于:https://www.cnblogs.com/liaozhuoshen/p/4839192.html

你可能感兴趣的文章
多线程Lock版生产者和消费者模式
查看>>
zoj3802:easy 2048 again(状压dp)
查看>>
Jenkins 自动化集成之路 Linux 安装 maven
查看>>
vue 自学笔记(七) 组件细节问题
查看>>
CSUOJ 1856 Sokoban 模拟
查看>>
List实体去重
查看>>
python函数回顾:abs()
查看>>
初识大数据(四. 大数据与人工智能的关系)
查看>>
netty 入门(一)
查看>>
Intellij Idea 15 下新建 Hibernate 项目以及如何添加配置
查看>>
《火星!火星!》
查看>>
大道至简读书笔记一
查看>>
php apache 配置后不能正常显示html文件的解决方法
查看>>
FILE类型指针的头文件
查看>>
牛客网暑期ACM多校训练营(第五场)J-plan (模拟)
查看>>
如何做一个跨平台的游戏App?
查看>>
五、椒盐排骨
查看>>
loj136 (最小瓶颈路,多次询问)
查看>>
4.1字符类型统计
查看>>
discuz核心函数库function_core的函数注释
查看>>