Starting from scratch Android 2048 (v) Undo implementation

Source: Internet
Author: User

The content of this article is to add the function of revocation on the basis of the previous article. Undo actually restores the current user interface to what it was before the sliding value. The main principle of my undo is to record the grid and corresponding numbers on each slide after each swipe, and of course the score, write the data to a stack, then click Undo, Pop the top of the stack, read the objects in the next stack, and redraw the interface based on the data stored in the object.


Here is the class I used to store each interface condition, in which the main three data in the interface, the blank lattice, the number lattice, and the current score are saved.

Package Com.example.t2048;import Java.util.arraylist;import Java.util.list;public class History {// corresponding to the empty sorting private list<integer> spacelist;//in the main activity corresponding to the list of numbers in the main activity private numberlist numberlist;// Corresponds to the current score in the main activity private int score = 0;/** * Construction method * @param sl  Empty sorting * @param nl  Number list * @param s   current score */history ( List<integer> SL, numberlist nl, int s) {spacelist = new arraylist<integer> (SL); numberlist = new NumberList (NE W arraylist<integer> (Nl.getstufflist ()), New Arraylist<integer> (Nl.getnumberlist ())); this.score = s;} Public list<integer> getspacelist () {return this.spacelist;} Public NumberList getnumberlist () {return this.numberlist;} public int Getscore () {return this.score;} /** * Print log */public void Printlog () {System.out.println (this.spaceList.toString ()); This.numberList.printLog (); System.out.println ("Score:" +this.score);}}

I then declared in the main activity that a stack was used to save the history of each

Save history for undo operation stack

The history of the current interface is pressed into the stack after each initialization and every valid swipe

History of History = New History (Spacelist, NumberList, score); Historystack.push (history);


To perform the undo operation, I wrote the following method, the code is very simple:

/** * Undo operation, get the last record in the operation record stack, and redraw the interface */public void GoBack () {//should have at least one valid swipe before revoking if (Historystack.size () >=2) {// The current interface is recorded on the stack to eject Historystack.pop ();//The second object in the stack is the record of the interface before this operation history = Historystack.peek (); numberlist = History.getnumberlist (); spacelist = History.getspacelist (); score = History.getscore ();//adjust method, redraw interface Drawviews ( Spacelist, NumberList, Score);}} /** * Redraw the interface according to parameters * @param spacelist blank sorting * @param numberlist number sorting * @param score current score */public void draw Views (list<integer> spacelist, numberlist numberlist, int score) {Scoretext.settext (score+ ""); Gridlayout.removeallviews (); for (int. i=0; i<16; i++) {View view = View.inflate (this, r.layout.item, null); ImageView Image = (ImageView) View.findviewbyid (r.id.image), if (Numberlist.contains (i)) Image.setbackgroundresource (icons[ Numberlist.getnumberbyindex (i)]); Elseimage.setbackgroundresource (Icons[0]); Gridlayout.addview (view);}}

When the above code is completed, the GoBack method is bound to the Undo button's Onclicklistener and the completion of the Undo function is implemented.


This article is basically the principle of this, so that the implementation of the Code feel better understanding, but there are great problems in efficiency, such as the user's operation recorded a lot of memory occupancy rate will be higher (no actual detection of how many), power consumption will certainly increase a lot, I think that's one of the reasons why some versions of 2048 offer only a few steps to undo functionality. If you have any better ways to achieve, welcome messages or private messages, more exchanges to promote common progress ha.


Finally, attached to the current, all the source code, if there is a problem I hope you criticize

: http://download.csdn.net/detail/johnsonwce/7290697

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.