Segment 4
Android mini game----Sudoku (iv)
OK, the chessboard is drawn out. So how do we fill in the numbers in the blank squares?
At this point the button will play its role.
We set a flag in the Controller class game to indicate the number that the current user wants to fill,
The listener listens to which button the current user clicks, and then sets it with the game's set method.
First, set up the listener in Mainactivity.java.
Public classMainactivityextendsActivity {Privategame game; PrivateButton btn_1; PrivateButton btn_2; PrivateButton btn_3; PrivateButton Btn_4; PrivateButton btn_5; PrivateButton btn_6; PrivateButton btn_7; PrivateButton Btn_8; PrivateButton Btn_9; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.main); Game=game.getgameinstance (); //Get ViewFindbuttonviewbyid (); //Setting up ListenersSetbuttononclicklistener (); } Private voidSetbuttononclicklistener () {Btn_1.setonclicklistener (NewOnbtnclicklistener (1)); Btn_2.setonclicklistener (NewOnbtnclicklistener (2)); Btn_3.setonclicklistener (NewOnbtnclicklistener (3)); Btn_4.setonclicklistener (NewOnbtnclicklistener (4)); Btn_5.setonclicklistener (NewOnbtnclicklistener (5)); Btn_6.setonclicklistener (NewOnbtnclicklistener (6)); Btn_7.setonclicklistener (NewOnbtnclicklistener (7)); Btn_8.setonclicklistener (NewOnbtnclicklistener (8)); Btn_9.setonclicklistener (NewOnbtnclicklistener (9)); } Private voidFindbuttonviewbyid () {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); } classOnbtnclicklistenerImplementsonclicklistener{//which numeric keypad button is pressed with the ID identification intID; PublicOnbtnclicklistener (intID) { This. ID =ID; } @Override Public voidOnClick (view view) {Game.setselectcurnumber ( This. ID); } }}
The listener class uses an inner class implementation, and we rewrite the listener class's construction method, passing an ID parameter as the indicator when setting the listener for the button.
Then how to draw in the chessboard, in fact, each draw a number, Surfaceview will redraw, but fast, and we control
The other redrawn parts are unchanged, so the effect of drawing only one number on the visual effect.
Imagine that the process of drawing numbers is to take the int array one by one out of the corresponding squares in the checkerboard, then we want to draw a new number
You can do that.
To differentiate the initialized array, we set up a temporary array to hold the data in the game
// Temporary array storage game checkerboard corresponding values Private int New int [9*9];
Of course, he needs to initialize, initialize the required data as well as the board's initialization data,
Public Game () { = frompuzzlestring (init_str); = frompuzzlestring (init_str); }
/*** Generates an array-shaped Sudoku initialization data based on a string of data*/ protected int[] frompuzzlestring (String str) {int[] sudo =New int[Str.length ()]; for(inti=0;i<sudo.length;i++){ //charAt () returns the char value decomposition string at the specified index populated into the array//minus 0 makes a type conversion to make it an int typeSudo[i]=str.charat (i)-' 0 '; } returnsudo; }
OK, then we can calculate the current coordinates when the user fills in the number and click on the grid, and update the data in the temporary array.
The data in the temporary array is then redrawn on the board to reach the effect of populating the data.
@Override Public Booleanontouchevent (Motionevent event) {if(Event.getaction ()! =Motionevent.action_down) { return Super. Ontouchevent (event); } Clicked=true; //The coordinate point of the screen that the user clicked onSelectedx = (int) (Event.getx ()/width); Selectedy= (int) (Event.gety ()/height); //Invalid click on the board to return directly if(!Game.isvaildclick (Selectedx, Selectedy)) { return Super. Ontouchevent (event); } //do not exceed the board click Initialize the already existing numbers to return directly if(!game.gettilestring (Selectedx, Selectedy). Equals ("")){ return Super. Ontouchevent (event); } game.setnewtilestring (Selectedx, Selectedy, Game.getselectcurnumber ()); //displays the relative coordinates of the selected squares//Toast.maketext (GetContext (), "(" +selectedx+ "," +selectedy+ ")", Toast.length_short). Show ();Draw (); number++; if(Game.iswin ()) {showwindialog (); //Toast.maketext (GetContext (), "Congratulations on your completion!! ", Toast.length_long). Show (); } return true; }
/*** Draw newly filled data *@paramCanvas *@paramPaint*/ Private voidinflatenewnum (canvas canvas, paint paint) {setfontstyle (paint); //set number to display center in cellFontMetrics FM =Paint.getfontmetrics (); floatx = WIDTH/2; floaty = height/2-(fm.ascent+fm.descent)/2; for(inti = 0; I < 9; i++) { for(intj = 0; J < 9; J + +) {Canvas.drawtext (Game.getnewtilestring (i, J), I*width+x, j*height+y, paint); } } }
When does the user finish the game?
/*** Determine if the Sudoku is completed successfully * As long as there are 0 in the temporary array * It must be the fill of the unfinished board * determine that the temporary array does not contain 0 o'clock matches the correct string * The same is done*/ Public BooleanIswin () { for(inti = 0; i < temp_sudoku.length; i++) { if(0 = =Temp_sudoku[i]) { return false; }} StringBuilder SB=NewStringBuilder (); for(inti = 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 finished.
The game is also calculated for the number of operation steps, that is, the number of redraw the board.
The menu has the ability to open the auxiliary bar, you can highlight 9 numbers, not in detail here.
The source has a class about the database, where you want to read the initial data string and the correct data string from the database, and can save the temporary data string on exit, the time is not complete. The Undo button of the button area is intended to be undone or not implemented. You can add these features yourself.
The complete data used here is as follows
The above is for reference only.
Attached source code:
Http://pan.baidu.com/s/1gdJ3TKJ
Attach apk (in bin directory, unsigned):
Http://pan.baidu.com/s/1qWPrlNm
Android mini game----Sudoku (iv)