Developing Android from scratch 2048 (four) score, reset, end

Source: Internet
Author: User

The main content of this article is based on the previous article, adding a score calculation (including the current score and the highest score), the game end of the judgment and the game interface reset the three parts of the function.

first, the calculation and preservation of fractionsfirst of all, 2048 of this game's score includes, two parts of the current game score and the highest score, the current game score we can save in the program's variables, because it is only useful for the current game, the game can be emptied at the end, and the highest score needs to be saved in one way on the phone, This way, when we open the game again next time, we can read the highest score of the previous operation. 1. Current scorethe calculation of the current score is still very simple. We only need to add the resulting score to the variable of the score we declared each time the merge occurs, and then update the control to display the current score. I took a look at the current integration rule for most 2048 games is how many points will be added after the combined lattice score. For example, 2 and 2 are combined into 4, then add 4 points.    2, the highest scoreThe highest score needs to be saved in the phone so that even if we turn off the game program, the highest score will not be lost. As far as I know how to save data on the Android there are several ways: 1, saved in the Android-brought SQLite this database 2, through Java as a file stored in the phone storage 3, using Android to provide Sharepreferences class to save and so on (of course there are certainly more methods available, which is just what I have learned) compared several ways, in this 2048 program, I chose the relatively simple sharepreferences class to save the highest score. Sharepreferences is a lightweight Android-provided class for data preservation, saved in XML format in \data\data\<package name>\shared_prefs\ Directory below. I checked, most of the scenarios used in this class are for storing things like setup information, which are less suitable for storing too complex or too much data. But for us, it's just to keep the highest score. refer to some of the online writing, the highest score of the class is so written:
Package Com.example.t2048;import Android.content.context;import Android.content.sharedpreferences;import android.content.sharedpreferences.editor;/** * This class is used to save and read the highest score * @author Mr.wang *  */public class Topscore {private Sharedpreferences Sp;public Topscore (Context context) {//Read Perference file, if not, create a file named Topscore SP = Context.getsharedpreferences ("Topscore", context. Mode_private);} /** * is used to read the highest score * @return highest score */public int Gettopscode () {//Pair de-key "topscore" corresponding value int topscore = Sp.getint ("Topscore", 0); return t Opscore;} /** * for write highest score * @param topscore new highest score */public void Settopscode (int topscore) {//Use editor class to write perference file Editor editor = sp. Edit (); Editor.putint ("Topscore", Topscore); Editor.commit ();}}

After we instantiate and invoke the Read method in it, we generate an XML file in the phone directory, and here is the graph I cut on my phone:


then, I wrote a method for updating fractions in the activity, and in the method of merging, call this method.
/** * This method is used to update the score * @param add new score */public void Updatescore (int add) {score + = Add;scoretext.settext (score+ ""); if (Score> ; Topscore.gettopscode ()) Topscore.settopscode (score); Topscoretext.settext (Topscore.gettopscode () + "");}


second, the game resetThe reset of the game is very simple, because I used to fill the blanks, randomly produce two numbers, such as the initialization of the operation is placed in the init () method, so if the game needs to start again, then we only need to gridlayout the view in the empty, and reset some global variables to their initial values, then call the init () method.
/** * Empty interface, reinitialize */public void Reset () {spacelist.clear (); Numberlist.clear (); score = 0;gridlayout.removeallviews (); Init ();}



third, the end of the game Judgment   after a swipe, all the blanks in the GridLayout have numbers, and there are no numbers to combine, we decide the game is over. My logic here is that if you traverse all the numbers in the absence of a blank lattice, if you do not have the same number lattice in the upper and lower sides of the lattice (the actual judgment is only right and bottom), then the end is done.
to achieve this, I have added two methods to the NumberList class:
/** * The corresponding number is obtained by means of the horizontal ordinate of the lattice * @param x  Horizontal axis * @param y  ordinate * @return   lattice corresponds to the exponent of the number */public int getnumberbyxy (int x, int y) {if (x<0 | | x>3 | | y<0 | | y>3) return-1;else {int order = Stufflist.indexof (4*x+y); return Numberlist.get ( order);}} /** * To determine if there is a number lattice that can be merged * @return has this return True */public Boolean haschance () {for (int x = 0;x<=3;x++) {for (int y=0;y<=3;y++) {i F (y<3) {if (Getnumberbyxy (x, y) ==getnumberbyxy (×, y+1)) return true; if (x<3) {if (Getnumberbyxy (x, y) ==getnumberbyxy (x+1, y) return True;}}} return false;}

Then, when you are sure that the game is over, a dialog box pops up for the user to choose to end or start over again:
public void Over () {new Alertdialog.builder (this). Settitle ("Ay! End "). Setmessage (" The game is over, your score for this bureau is "+score+" points, continue to refuel Oh!) "). Setpositivebutton (" Restart ", new Onclicklistener () {public void OnClick (Dialoginterface dialog, int which) {reset ();}}). Setnegativebutton ("End Game", new Onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) { MainActivity.this.finish ();}}). Show ();}

to now the main content of the game is basically completed, this program is I learn Android since the first small program, although there are many imperfect places, but in the process of development still learned a lot of things. Sure enough to read more, or better write a few pieces of code to learn faster. Hope, like me, from the beginning to learn Android friends, we work together. then I will use my spare time to constantly refine and optimize this little game. How can be considered the first Android program, can not be so careless to end off.
finally attach the source code of this article, you are welcome to correct me: http://download.csdn.net/detail/johnsonwce/7276123

Developing Android from scratch 2048 (four) score, reset, end

Related Article

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.