1. program running
Ii. Code Implementation
1. main. xml
2. dialog. xml
3. keypad1.xml
4. MainActivity
package com.njupt.shudu;import android.os.Bundle;import android.app.Activity;import android.view.Menu;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(new ShuduView(this));}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
5. ShuduView
Package com. njupt. 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 {// the width and height of the cell private floa T width; private float height; private Game game = new Game (); private int selectedX; private int selectedY; public ShuduView (Context context) {super (context );} /*** w: width of the current view ** h: height of the current view **/@ Overrideprotected void onSizeChanged (int w, int h, int oldw, int oldh) {// calculate the width and height of the current cell. this. width = w/9f; this. height = h/9f; super. onSizeChanged (w, h, oldw, oldh) ;}@ Overrideprotected void onDraw (Canvas can Vas) {// generate the Paint backgroundPaint = new Paint () used to draw the background color; // set the Paint color backgroundPaint. setColor (getResources (). getColor (R. color. shudu_background); // draws the background color 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);/*** draw the line used to split the small nine cells (split the screen into 81 grids) */for (int I = 0; I <9; ++ I) {/*** canvas. drawLine (0, I * height, getWidth (), I * height, lightPaint) * 1st, 2 parameters: coordinates of the start point * 3rd, 4 parameters: coordinates of the Terminal * 5th parameters: the paint brush used */canvas. drawLine (0, I * height, getWidth (), I * height, lightPaint); // draw a canvas. drawLine (0, I * height + 1, getWidth (), I * hei Ght + 1, hilitePaint); // It is also a horizontal line, in order to highlight the effect canvas. drawLine (I * width, 0, I * width, getHeight (), lightPaint); canvas. drawLine (I * width + 1, 0, I * width + 1, getHeight (), hilitePaint );} /*** draw a line used to split the screen into nine big cells */for (int I = 0; I <9; ++ I) {if (I % 3 = 0) {continue;} canvas. drawLine (0, I * height, getWidth (), I * height, darkPaint); // draw a canvas. drawLine (0, I * height + 1, getWidth (), I * height + 1, hilitePaint); // also It is a horizontal line, in order to compare and highlight the carved effect canvas. drawLine (I * width, 0, I * width, getHeight (), darkPaint); canvas. drawLine (I * width + 1, 0, I * width + 1, getHeight (), hilitePaint);} // draw the number Paint numberPaint = new Paint (); numberPaint. setColor (Color. BLACK); numberPaint. setStyle (Paint. style. STROKE); numberPaint. setTextSize (height * 0.75f); numberPaint. setTextAlign (Paint. align. CENTER); // set the alignment mode FontMetrics fm = numberPaint. getFontMetrics (); Float x = width/2; float y = height/2-(fm. ascent + fm. descent)/2; // generate the initialization data 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, numberPaint) ;}} super. onDraw (canvas) ;}@ Overridepublic boolean onTouchEvent (MotionEvent event) {if (event. getAction ()! = MotionEvent. ACTION_DOWN) {return super. onTouchEvent (event);} // determines the cell selectedX = (int) (event. getX ()/width); selectedY = (int) (event. getY ()/height); int used [] = game. getUsedTilesByCoor (selectedX, selectedY); StringBuffer sb = new StringBuffer (); for (int I = 0; I <used. length; ++ I) {// used to verify whether sb is correct. append (used [I]);} // generate a LayoutInflater object // LayoutInflater layoutInflater = LayoutInflater. from (this. getContext (); // use the LayoutInflater object to generate a View based on a layout file. // View layoutView = layoutInflater. inflate (R. layout. dialog, null); // extract the corresponding control from the generated TextView // TextView textView = (TextView) layoutView. findViewById (R. id. usedTextId); // sets the content of TextView // textView. setText (sb. toString (); // generate the Builder object of a dialog box // AlertDialog. builder builder = new AlertDialog. builder (this. getContext (); // The displayed content is displayed in the Setting Dialog Box. // builder. setView (layoutView); // generate and display the dialog box object // AlertDialog dialog = builder. create (); // dialog. show (); // KeyDialog keyDialog = new KeyDialog (getContext (), used, this); keyDialog. show (); return true;} public void setSelectedTile (int tile) {if (game. setTileIfValid (selectedX, selectedY, tile) {invalidate ();}}}
7. Game
Package com. njupt. shudu; public class Game {// basic private final String str = 360000000004230800000004200 + 070460003820000014500013020 + 001900000007048300000000045; private int sudoku [] = new int [9*9]; // store the data that is unavailable for each cell. private int used [] [] [] [] = new int [9] [9] []; public Game () {sudoku = fromPuzzleString (str); calculateAllUsedTiles ();}/*** Based on the coordinates in the nine cells, returns the number * @ param x * @ param y * @ return */pri for the specified coordinate. Vate int getTile (int x, int y) {return sudoku [y * 9 + x];} /*** obtain the unavailable data of the Cell Based on the x axis and y axis * @ param x * @ param y * @ return */public String getTileString (int x, int y) {int v = getTile (x, y); if (v = 0) {return;} else {return String. valueOf (v) ;}}/*** generates an integer Array Based on a string, the so-called data initialization for a Sudoku game * @ param src * @ return */protected int [] fromPuzzleString (String src) {int [] sudo = new int [src. length ()]; for (int I = 0; I <su Do. length; ++ I) {sudo [I] = src. charAt (I)-'0';} return sudo;}/*** calculate the unavailable data of all cells */public void calculateAllUsedTiles () {for (int x = 0; x <9; ++ x) {for (int y = 0; y <9; ++ y) {used [x] [y] = calculateUsedTiles (x, y );}}} /*** retrieve unavailable data in a cell * @ param x * @ param y * @ return */public int [] getUsedTilesByCoor (int x, int y) {return used [x] [y];}/*** calculate the data that is unavailable in a cell * @ param x * @ param y */public I Nt [] calculateUsedTiles (int x, int y) {int c [] = new int [9];/*** calculated on the y axis (column) the numbers in the direction are not available... */for (int I = 0; I <9; ++ I) {if (I = y) {// if this is the lattice continue clicked by the user ;} int t = getTile (x, I); if (t! = 0) {c [t-1] = t ;}} for (int I = 0; I <9; ++ I) {if (I = x) {continue;} int t = getTile (I, y); if (t! = 0) {c [t-1] = t ;}}/*** the numbers used in the calculation of small 9-Square cells .. */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 = getTile (I, j); if (t! = 0) {c [t-1] = t ;}}/*** remove 0 from c */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 ;}public boolean setTileIfValid (int x, int y, int value) {int tiles [] = getUesdTiles (x, y); if (value! = 0) {for (int tile: tiles) {if (tile = value) {return false ;}} setTile (x, y, value ); // Add the number entered by the user to calculateAllUsedTiles (); // update the return true number that can be used by the cell;} protected int [] getUesdTiles (int x, int y) {return used [x] [y];} private void setTile (int x, int y, int value) {sudoku [y * 9 + x] = value ;}}
8. KeyDialog
Package com. njupt. shudu; import android. app. dialog; import android. content. context; import android. OS. bundle; import android. view. view;/*** this class is used to implement Dialog and customize the Dialog box function... * @ author Administrator **/public class KeyDialog extends Dialog {// used to store the private final View keys [] = new View [9] object representing the buttons in the Dialog box; private final int used []; private ShuduView shuduView;/*** the second parameter of the constructor stores the number used by the current cell * @ param context * @ par Am used */public KeyDialog (Context context, int [] used, ShuduView shuduView) {super (context); this. used = used; this. shuduView = shuduView;}/*** when a Dialog is displayed for the first time, the onCreate method is called */@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // sets the title setTitle (KeyDialog) of the Dialog box; // sets the layout file setContentView (R. layout. keypad1); findViews (); // display the available numbers in a cell for (int I = 0; I <used. length; ++ I) {if (used [I]! = 0) {keys [used [I]-1]. setVisibility (View. INVISIBLE) ;}/// set the listener setListeners () for all buttons in the dialog box;} private void findViews () {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);}/*** notify ShuduView object, and refresh the data displayed in the earned nine cells * @ param tile */private void returnResult (int tile) {System. out. println (shuduView: + shuduView); shuduView. setSelectedTile (tile); dismiss (); // display in the cancel dialog box} 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 );}});}}}