数独小游戏
2016-06-13 09:31
453 查看
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(); } }
MainActivity.java
package com.xhly.shudu; import android.app.Activity; import android.os.Bundle; import com.xhly.shudu.view.ShuduView; public class MainActivity extends Activity { @Override protected 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; } @Override protected 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() { @Override public void onClick(View v) { returnResult(t); } }); } keypad_cls.setOnClickListener(new View.OnClickListener() { @Override public 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(); } @Override protected 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); } @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 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); } @Override public 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(); } }
colors.xml
<?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>
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树