Android games, android
Segment 4
Android games-Sudoku (4)
Okay, the board is drawn. So how do we fill in numbers in the blank box?
Then the button plays its role.
In the Controller game, we set a flag to indicate the number that the current user wants to enter,
You can use the listener to monitor which button the current user clicks and set it using the set method of game.
First, set the listener in MainActivity. java.
Public class MainActivity extends Activity {private Game game; private Button btn_1; private Button btn_2; private Button btn_3; private Button btn_4; private Button btn_5; private Button btn_6; private Button btn_7; private Button btn_8; private Button btn_9; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); game = Game. getGameInstance (); // get View findButtonViewById (); // set the listener setButtonOnClickListener ();} private void setButtonOnClickListener () {listener (new OnBtnClickListener (1 )); btn_2.setOnClickListener (new OnBtnClickListener (2); listener (new OnBtnClickListener (3); listener (new OnBtnClickListener (4); listener (new OnBtnClickListener (5 )); listener (new OnBtnClickListener (6); btn_7.setOnClickListener (new OnBtnClickListener (7); listener (new OnBtnClickListener (8); listener (new OnBtnClickListener (9 ));} private void findButtonViewById () {btn_1 = (Button) findViewById (R. id. num_1); btn_2 = (Button) findViewById (R. id. num_2); btn_3 = (Button) findViewById (R. id. num_3); btn_4 = (Button) findViewById (R. id. num_4); btn_5 = (Button) findViewById (R. id. num_5); btn_6 = (Button) findViewById (R. id. num_6); btn_7 = (Button) findViewById (R. id. num_7); btn_8 = (Button) findViewById (R. id. num_8); btn_9 = (Button) findViewById (R. id. num_9);} class OnBtnClickListener implements OnClickListener {// id used to identify which numeric keyboard button is pressed int id; public OnBtnClickListener (int id) {this. id = id ;}@ Override public void onClick (View view) {game. setSelectCurNumber (this. id );}}}
Listener class uses internal class implementation. We override the construction method of the listener class, and send an id parameter as the identifier when setting the listener for the button.
How can we draw a chart on the board? In fact, surfaceview will repaint every time a number is drawn, but the speed is very fast and we control it.
The remaining re-painting parts remain unchanged. Therefore, the visual effect is equivalent to drawing only one number.
Imagine that the process of drawing numbers is to extract the int array one by one and draw it in the corresponding square on the board. Then we need to draw new numbers.
You can also.
To differentiate initialization arrays, we set a temporary array to save game data.
// Temporary array stores the private int [] temp_sudoku = new int [9*9] corresponding value of the board in the game.
Of course, he needs initialization. The initialization data is the same as the initialization data of the Board,
public Game(){ sudoku = fromPuzzleString(init_str); temp_sudoku = fromPuzzleString(init_str); }
/*** Generate an integer array of data initialization data for the sudoku game based on a String */protected int [] fromPuzzleString (String str) {int [] sudo = new int [str. length ()]; for (int I = 0; I <sudo. length; I ++) {// charAt () returns the char value at the specified index. The decomposed string is filled in the array // minus 0 for type conversion to make it int type sudo [I] = str. charAt (I)-'0';} return sudo ;}
Okay. Next, we can calculate the current coordinates and update the data in the temporary array when the user fills in the number and clicks the square,
Redraw the data in the temporary array on the board to fill the data.
@ Override public boolean onTouchEvent (MotionEvent event) {if (event. getAction ()! = MotionEvent. ACTION_DOWN) {return super. onTouchEvent (event);} clicked = true; // The coordinate point selectedX = (int) (event. getX ()/width); selectedY = (int) (event. getY ()/height); // if (! Game. isVaildClick (selectedX, selectedY) {return super. onTouchEvent (event) ;}// if (! Game. getTileString (selectedX, selectedY ). equals ("") {return super. onTouchEvent (event);} game. setNewTileString (selectedX, selectedY, game. getSelectCurNumber (); // display the relative coordinates of the selected box // Toast. makeText (getContext (), "(" + selectedX + "," + selectedY + ")", Toast. LENGTH_SHORT ). show (); draw (); number ++; if (game. isWin () {showWinDialog (); // Toast. makeText (getContext (), "Congratulations !! ", Toast. LENGTH_LONG). show ();} return true ;}
/*** Plot the newly filled data * @ param canvas * @ param paint */private void inflateNewNum (Canvas canvas, Paint paint) {setFontStyle (paint ); // set the number to center in the cell FontMetrics fm = paint. 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. getNewTileString (I, j), I * width + x, j * height + y, paint );}}}
When does it calculate whether the user has completed the game,
/*** Determine whether the Sudoku is successfully completed * as long as there is 0 in the temporary array * It must be filled with the unfinished Board * If the temporary array does not contain 0, it matches with the correct string * if it is the same complete */public boolean isWin () {for (int I = 0; I <temp_sudoku.length; I ++) {if (0 = temp_sudoku [I]) {return false ;}} stringBuilder sb = new StringBuilder (); for (int I = 0; I <temp_sudoku.length; I ++) {sb. append (temp_sudoku [I]);} if (sb. toString (). equals (correct_str) {return true;} return false ;}
Each fill is judged once until the game is completed.
The game also calculates the number of operation steps, that is, the number of times the board is repainted.
In the menu, you can enable the secondary bar function to highlight 9 numbers at the horizontal and vertical positions. This is not detailed here.
The source code contains database classes. Here, you want to read the initial data string and correct data string from the database, and save the temporary data string when you exit. The time limit is not complete. the undo button in the button area is intended to be undone, and is not implemented either. You can add these functions on your own.
The complete data used here is as follows:
The above is for reference only.
Attached source code:
Http://pan.baidu.com/s/1gdJ3TKJ
Attached apk (unsigned under the bin directory ):
Http://pan.baidu.com/s/1qWPrlNm