Develop Android version 2048 (four) score, reset, end from scratch

Source: Internet
Author: User
Tags sqlite database

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

first, the calculation and preservation of fractionsfirst of all, 2048 of this game's score contains, 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 practical for the current game, the game will be able to empty 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 easy. We just 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 this. The integration rules for most 2048 games are the number of points that 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 kept on the phone so that even if we turn off the game, the highest score will not be lost. There are several ways that I know about Android to save data: 1, stored in the Android-brought SQLite database 2, stored in the form of Java files in the mobile 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've learned right now) compared to 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\ Folder below. I checked, most of the scenarios that use this class are used to save some analogy settings information and so on, which is less suitable for storing too complex or too much data. But for us, it's just to keep the highest score. Some of the writing on the Internet, 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, assuming no, a file named Topscore is created SP = Context.getsharedpreferences ("Topscore", context. Mode_private);} /** * Used to read 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 ();}}

When we instantiate and invoke the Read method in it, an XML file is generated in the phone's folder, and the following 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, it was possible to 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 game reset is very easy, because I before, the fill blank lattice, random production of two numbers, such as the initialization of the operation is placed in the init () method, so assume that the game needs to start again, then we just need to gridlayout the view in the empty, and reset some global variables to their initial values, and then call the init () method to be able.
/** * Empty the interface, and again initialize */public void Reset () {spacelist.clear (); Numberlist.clear (); score = 0;gridlayout.removeallviews (); Init ();}



Iii. inference of the end of the game   when a slide is followed by a number on all the blanks in the GridLayout, and there are no numbers to merge, we decide the game is over. My logical handling here is to assume that, without a blank lattice, the entire number lattice is traversed, assuming that there is no same number lattice at the top or bottom of the lattice (the actual inference is that the right and the bottom can only be inferred), even if it ends.
to achieve this inference, I have added two methods to the NumberList class:
/** * To obtain its corresponding number by means of the corresponding horizontal ordinate axis * @param x  Horizontal axis * @param y  ordinate * @return   lattice the corresponding number of exponent */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);}} /** * Infer If there is also 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 the game is over, a dialog box pops up for the user to choose to end or start 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 (" Start Again ", 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 this game is basically finished, this program is I learn Android since the first small program, although there are many not intact, 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 and like me, from the beginning to learn Android friends, everyone together. then I will use my spare time to keep up 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

Develop Android version 2048 (four) score, reset, end from scratch

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.