Puzzle-code design and implementation
Last weekend, I finished reading "English for Android group" in three hours. This week I mainly studied the design and implementation of code layers.
Compilation and installation on mobile phones, a few tricks, combined with code, only one week to master the overall idea.
Most of the time is actually spent on "refactoring.
The reconstruction process is the process of learning and thinking.
This article is a summary of learning and describes the implementation of this game.
After taking the time to transform the game's unreasonable design method, that is, the grid is N * N + 1, not N * N.
Writing: In the hundreds of articles I have written, there are many cases. The more I write, the more I find that many processes and ideas are consistent.
Therefore, it is necessary to summarize some general knowledge. The description is clear, and the introduction is much clearer.
The article is initially named "visual GUI application development Summary: Android, iOS, Web, Swing, Windows development, etc." and is expected to be published by December 7, 2015.
Code Description
1. Package Structure
Cn. fansunion. puzzle
-- Activity
GlobalConst. java several global Constants
MainActivity. java game entry Activity
PuzzleActivity. java Activity in the jigsaw puzzle game process
-- Adapter
MainGridViewAdapter. The Gridview adapter on the java game portal interface can be understood as the data provider of the GridView.
PuzzleGridViewAdapter. adapter of the java tile interface's GridView
-- Bean
1 element in GridItem. java table
-- Util
GameUtil. java encapsulates game rules
ImageUtil. java image processing tool
ScreenUtil. java screen Tool
2. Basic explanation
GameUtil. java
IsMoveable: determines whether an image can be moved, or "whether it can be exchanged with spaces". Based on the position in the GridView, you can determine whether it is "adjacent" to a space.
SwapItems: switches the positions of two griditems.
IsSuccess: determine whether the current puzzle is complete
GetPuzzleGenerator: generate a random Item, which is to disrupt the positions of N * N numbers.
CanSolve: determines whether a randomly generated Item can be resolved, that is, whether the source image can be restored by moving the exchanged image ".
(The design of this place is also relatively pitfall. I currently think that we can generate an initial puzzle in another way, that is, randomly swap spaces and surrounding images N times. Because the exchange is reversible, there is always a solution)
ImageUtil. java: Cut one image into N x N, and enlarge the image.
ScreenUtil. java: obtains the screen size and density.
GridItem. java: the core Model of the game puzzle. It contains one item in the table, id, image resource id, and image resource, facilitating the implementation of drawing and game rules.
MainGridViewAdapter. java and PuzzleGridViewAdapter. java: Inherit from android. widget. BaseAdapter and reload several methods.
GlobalConst. java: some constants. That's too easy.
3. Game process
Game entry MainActivity. java
Core Process:
A. Set the Subject Page
SetContentView (R. layout. xpuzzle_main );
B. initialize other interfaces and buttons.
InitViews ();
C. Data Adapter
GridView. setAdapter (new MainGridViewAdapter (
MainActivity. this, bitmapList ));
D. Button, interface, and other binding events
GridView. setOnItemClickListener
E. Event Response
Important events
A. Select the game difficulty and save it to the Type field. Select a dialog box.
SelectedTypeTextView. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {// pop-up popup window popupShow (v );}});}
B. The game comes with several images and uses system images.
As shown in a table, click "Item" to listen. The last image is "Taken by the local image library or camera". Other images are directly selected.
GridView. setOnItemClickListener (new OnItemClickListener () {@ Override public void onItemClick (AdapterView
Arg0, View view, int position, long arg3) {if (position = photoResourceIdArray. length-1) {// select the local image library camera showDialogCustom ();} else {// select the default image Intent intent = new Intent (MainActivity. this, PuzzleMain. class); intent. putExtra (GlobalConst. SELECT_PHOTO_ID, photoResourceIdArray [position]); intent. putExtra (GlobalConst. TYPE, type); startActivity (intent );}}});
The Local Image Library and the camera take pictures. There are two similar logic sets. After the user selects the image library, call the callback method of the Image Library camera to save the selected image.
Then go to the "main interface of the puzzle game.
PuzzleActivity. java
Core Process:
A. Set the subject layout
SetContentView (R. layout. xpuzzle_puzzle_detail_main );
B. Obtain the selected image and cut the image
GetIntent (). getExtras (). getInt (GlobalConst. SELECT_PHOTO_ID );
C. initialize other Views
InitViews ();
D. Call GameUtil to generate the initial game data and start the timer. (I suddenly found out that it was unreasonable again. The timer should be enabled after all program Initialization is complete .)
GenerateGame ();
E. Event binding.
// Return button click event
BackButton. setOnClickListener (this );
// Display the source image button click event
ImageButton. setOnClickListener (this );
// Reset button click event
RestartButton. setOnClickListener (this );
// The GridView Click Event (the most important thing is this). Can images be moved, you need to swap images, update drawings, and update steps ".
// When the operation succeeds, perform subsequent processing (such as stopping the timer)
GridView. setOnItemClickListener (new OnItemClickListener () {@ Override public void onItemClick (AdapterView
Arg0, View view, int position, long arg3) {// you can determine whether to move if (GameUtil. isMoveable (position) {// switch the position where the Item and space are clicked. swapItems (GameUtil. gridItemList. get (position), GameUtil. blankGridItem); // obtain the image recreateData () again; // notify the GridView to change the UI puzzleGridViewAdapter. notifyDataSetChanged (); // update the number of steps stepCount ++; stepCountTextView. setText (+ stepCount); // determines if (GameUtil. isSuccess () {// display the complete recres In the last graph AteData (); bitmapItemList. remove (TYPE * TYPE-1); bitmapItemList. add (lastBitmap); // notifies the GridView to change the UI puzzleGridViewAdapter. notifyDataSetChanged (); Toast. makeText (PuzzleMain. this, the puzzle is successful !, Toast. LENGTH_LONG). show (); gridView. setEnabled (false); timer. cancel (); timerTask. cancel ();}}}});
4. Resources
La S, menus, strings, and Java code are easy to understand.
Personal Opinion
The current technology, entry level, and intermediate level are easy to work and make money.
After reaching a certain level, if you want to continue to be advanced, you need to understand the technology.
Everyone has their own understanding of refactoring, code specifications, and game design.
Take into consideration the actual situation.