Android mini game----Sudoku (iii)

Source: Internet
Author: User

Segment 3

Android mini game----Sudoku (iii)

Well, since we're going to draw out the numbers in the Sudoku board, we need to know some of the features of Sudoku first.

The Sudoku board is a total of 81 numbers, then whether we can use an array to represent the entire chessboard corresponding to the number
Imagine that we initialize an array of 9x9,
Private int[] Sudoku = new Int[9*9]

// Initializing Data    Private int New int [9*9];

Using a one-dimensional array here, of course, using a two-dimensional array is more straightforward, as is the case with personal preference.

So we can make sudoku[0] correspond to the number of the first square on the Sudoku board (upper left corner),

SUDOKU[1] corresponds to the first row of the second square number, and so on.

The 81 numbers of Sudoku are regular, this can be found on the Internet to initialize the numbers and fill out the number of the chessboard.

We're looking for a data like this, initialized in string string, and let it be populated into an int array by decomposition

    // Sudoku initializes the string    of puzzle data Private Final String init_str =    "061030020050008107000007034"                                    + "009006078003209500570300900"                                    + " 190700000802400060040010250 ";         // correct answer string    Private Final String correct_str = "761934825354628197928157634"                                    + "219546378483279516576381942"                                    + " 195762483832495761647813259 ";

/*** 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; }

/**      * Returns the data string     that should be filled in at this location according to the coordinates in the nine grid *    /public string gettilestring ( int x,int  y) {        int v = gettileinteger (x, y);         if (v = = 0)            {return "";        }         Else {            return  string.valueof (v);        }    }

With the gettilestring (int x,int y) method, we can represent the coordinates according to the position of the checkerboard squares.

The number that should be filled in to the current grid.

How do we draw the coordinates that the checkerboard squares represent?

First, we rewrite the Surfaveview onsizechanged method, not rewrite can also, the main purpose is to determine the height and width of the checkerboard square.

    @Override    protectedvoid onsizechanged (intint int int OLDH) {        /*         * Calculate cell width and height         * Set the width and height of the box to 1/9 squares                  of the width of the screen w */ this. Width = w/9f;         this. Height = w/9f;         Super . Onsizechanged (W, H, OLDW, OLDH);    }

Here we set the width of the height of 1/9, so the view is the complete square.

When the user clicks on the screen, according to click the event to get the coordinates of the click, in doing the operation can get the current click

The coordinates represented by the squares.

// The coordinate point        of the screen that the user clicked on Selectedx = (int) (Event.getx ()/ width);         = (int) (event.gety ()/height);

To draw the initial data, you do not need to know the coordinates represented by the squares.

as follows, with nested loops, facilitates sudoku arrays, and does not draw if matched to empty

 for (int i = 0; i < 9; i++) {            for (int j = 0; J < 9; J + +) {                 if< /c8> (!game.gettilestring (i, J). Equals ("")) {                    Paint.setcolor (getresources (). GetColor (R.color.shudu_init_ NUMBER_BG));                    Canvas.drawrect (i*width, J*height, I*width+width, j*height+width, paint);         }}}

The numeric keypad below the chessboard is laid out in XML, Keypad.xml

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:layout_gravity= "Center"android:orientation= "vertical" >        <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal"        >        <ButtonAndroid:id= "@+id/num_1"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "@string/num_1"Android:layout_weight= "1"            />        <ButtonAndroid:id= "@+id/num_2"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "@string/num_2"Android:layout_weight= "1"            />        <ButtonAndroid:id= "@+id/num_3"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "@string/num_3"Android:layout_weight= "1"            />        <ButtonAndroid:id= "@+id/num_4"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "@string/num_4"Android:layout_weight= "1"            />        <ButtonAndroid:id= "@+id/num_5"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "@string/num_5"Android:layout_weight= "1"            />    </LinearLayout>        <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal"        >        <ButtonAndroid:id= "@+id/num_6"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/num_6"Android:layout_weight= "1"            />        <ButtonAndroid:id= "@+id/num_7"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/num_7"Android:layout_weight= "1"            />        <ButtonAndroid:id= "@+id/num_8"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/num_8"Android:layout_weight= "1"            />        <ButtonAndroid:id= "@+id/num_9"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/num_9"Android:layout_weight= "1"            />        <ButtonAndroid:id= "@+id/undo"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "undo"Android:layout_weight= "1"            />    </LinearLayout>    </LinearLayout>
View Code

And then come in Main.xml include.

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Android:background= "@android: Color/white"Tools:context=". Mainactivity " >      <FramelayoutAndroid:layout_width= "Match_parent"Android:layout_height= "350DP"        >        <Com.sphere.shudu.GameSFViewxmlns:android= "Http://schemas.android.com/apk/res/android"Android:id= "@+id/panelsurfaceview"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"            />    </Framelayout>                <includeAndroid:id= "@+id/keypadview"Layout= "@layout/keypad" /></LinearLayout>

350dip is used here, the device for hdpi, if it is other type, please configure the height size yourself.

Finally, R.layout.main can be transmitted in the setcontentview of activity.

In this way, the activity contains both the Surfaceview display and the button area of the operation.

Android mini game----Sudoku (iii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.