数独小游戏
来源:互联网 发布:身体域网络的作用范围 编辑:程序博客网 时间:2024/04/28 03:04
Game.java
package com.xhly.shudu;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();/* * for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { for (int * j2 = 0; j2 < used[i][j].length; j2++) { * System.out.println(used[i][j][j2]); } } } */}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 = getTitle(x, i);if (t != 0)c[t - 1] = t;}for (int i = 0; i < 9; i++) {if (i == x)continue;int t = getTitle(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 = getTitle(i, j);if (t != 0)c[t - 1] = t;}}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;}private int getTitle(int x, int y) {return sudoku[y * 9 + x];}public String getTitleString(int x, int y) {int v = getTitle(x, y);if (v == 0) {return "";} else {return String.valueOf(v);}}protected 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 void setTileCoor(int x,int y,int sel){sudoku[9*y+x] = sel;calculateAllUsedTiles();}}
package com.xhly.shudu;import android.app.Activity;import android.os.Bundle;import com.xhly.shudu.view.ShuduView;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(new ShuduView(this));/*setContentView(R.layout.activity_main);*/}}
KeyDialog.java
package com.xhly.shudu.view;import android.app.Dialog;import android.content.Context;import android.os.Bundle;import android.view.View;import com.xhly.shudu.R;public class KeyDialog extends Dialog {private final View keys[] = new View[9];private int[] used;private ShuduView sv;public Context context;private View keypad_cls;public KeyDialog(Context context, int[] used,ShuduView sv) {super(context);this.used = used;this.sv = sv;this.context = context;}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setTitle("点击输入");setContentView(R.layout.keypad);findViewByIds();for (int i = 0; i < used.length; i++) {if(used[i]!=0){keys[used[i]-1].setVisibility(View.INVISIBLE);}}setListeners();}public void returnResult(int i){sv.setSel(i);dismiss();}public void findViewByIds(){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);keypad_cls = findViewById(R.id.keypad_cls);}private void setListeners() {for (int i = 0; i < keys.length; i++) {final int t = i+1;keys[i].setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {returnResult(t);}});}keypad_cls.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {returnResult(0);}});}}
ShuduView.java
package com.xhly.shudu.view;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;import com.xhly.shudu.Game;import com.xhly.shudu.R;public class ShuduView extends View {private float width;private float height;private float playWindowHeight;private Game game;public Context context;private int selectX;private int selectY;public ShuduView(Context context) {super(context);this .context = context;game = new Game();}@Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh) {this.width = w/9f;this.height = w/9f;playWindowHeight = w;super.onSizeChanged(w, h, oldw, oldh);}@Overrideprotected void onDraw(Canvas canvas) {Paint backgroundPaint = new Paint();backgroundPaint.setColor(getResources().getColor(R.color.shudu_background));canvas.drawRect(0, 0, getWidth(), getHeight(), backgroundPaint);Paint darkPaint = new Paint();darkPaint.setColor(getResources().getColor(R.color.shudu_dark));Paint lightPaint = new Paint();lightPaint.setColor(getResources().getColor(R.color.shudu_light));Paint hitlePaint = new Paint();hitlePaint.setColor(getResources().getColor(R.color.shudu_hitle));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, hitlePaint);canvas.drawLine(i*width, 0, i*width, playWindowHeight, lightPaint);canvas.drawLine(i*width+1, 0, i*width+1, playWindowHeight, hitlePaint);}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, hitlePaint);canvas.drawLine(i*width, 0, i*width, playWindowHeight, darkPaint);canvas.drawLine(i*width+1, 0, i*width+1, playWindowHeight, hitlePaint);}Paint numberPaint = new Paint();numberPaint.setColor(Color.BLACK);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;for (int i = 0; i < 9; i++) {for (int j = 0; j < 9; j++) {canvas.drawText(game.getTitleString(i, j), i*width+x, j*height+y, numberPaint);}}super.onDraw(canvas);}@Overridepublic boolean onTouchEvent(MotionEvent event) {int action = event.getAction();if(action!= MotionEvent.ACTION_DOWN){return super.onTouchEvent(event);}float x = event.getX();float y = event.getY();if(y>playWindowHeight){return super.onTouchEvent(event);}selectX = (int)(x/width);selectY = (int)(y/height);int used[] = game.getUsedTilesByCoor(selectX, selectY);KeyDialog kd = new KeyDialog(context, used,this);kd.show();return true;}public void setSel(int i){game.setTileCoor(selectX, selectY, i);invalidate();}}
<?xml version="1.0" encoding="utf-8"?><resources> <color name="shudu_background">#ffe6f0ff</color> <color name="shudu_hitle">#ffffffff</color> <color name="shudu_light">#64c6d4ef</color> <color name="shudu_dark">#6456648f</color> </resources>
keypad.xml
<?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="wrap_content" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/keypad_1" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="1" /> <Button android:id="@+id/keypad_2" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="2" /> <Button android:id="@+id/keypad_3" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="3" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/keypad_4" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="4" /> <Button android:id="@+id/keypad_5" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="5" /> <Button android:id="@+id/keypad_6" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="6" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/keypad_7" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="7" /> <Button android:id="@+id/keypad_8" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="8" /> <Button android:id="@+id/keypad_9" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="9" /> </LinearLayout> <Button android:id="@+id/keypad_cls" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="清除" /></LinearLayout>
0 0
- ”数独“android小游戏
- 数独小游戏
- 用SVG写的数独小游戏
- 在MTK上实现数独小游戏
- Linux 数独小游戏 curses cs50 作业
- 第二个小游戏实例:数独游戏
- Gtk下的数独小游戏(已添加截图)
- 数独
- 数独
- 数独
- 数独
- 数独
- 数独
- 数独
- 数独
- 数独
- 数独
- 数独
- Mysql----学习笔记
- Android 5.0+ 解析(八)AppBarLayout控件
- 什么是Boot Loader
- 贡献一组摘自快递100的物流数据 哪位有改造成plist记得私信告诉我
- 第3章 服务框架
- 数独小游戏
- spark RDD的5个重要内部属性
- 没有庸俗的企业,只有不停的战场
- python---学习链接
- mybatis批量插入
- Android Gradle Plugin指南(二)——基本项目
- swift支持多线程操作数据库类库-CoreDataManager
- 颜色的三要素:色调,饱和度,和亮度。
- python之scipy