"Android Learning" uses Rxjava to complete puzzle-flop games

Source: Internet
Author: User

No contact with Rxjava before the beginning of Android, with the native control ImageView wrote a flop game.


However, the bug is that if you click on the same card twice, you will be counted into the error.


I was not quite sure how to prevent the control from multiple clicks, then the solution was to compare the ID of the last click on the second click and not count if it was consistent.


Now with the Rxjava, you can implement this feature more succinctly.


See:





Prevent multiple codes: (Only one click on the control in two seconds)

Rxview. clicks (pictureboxs[i])        . Throttlefirst (2timeunit.  SECONDS)        . Subscribeon (androidschedulers.  Mainthread())        . Subscribe (action1<void> () {            @Override             Call (Void avoid) {dosomething ();});  


Background Timings:


Observable.interval(1, Timeunit.SECONDS). Compose ( This. <long>bindtolifecycle ()). Observeon (androidschedulers.Mainthread()). Subscribeon (schedulers.Newthread()). Subscribe (NewAction1<long> () {@Override                       Public voidPager(Long along) {TvShow. SetText ("" + Integer.toString(alltime++));};



The game logic is simple.


Randomly 6 different numbers, assigned to array num[0]-----num[5],

Then let Num[0]---num[5] order of the scrambled values assigned to NUM[6]---num[11].

So 0---11 have 6 pairs of the same, and the order is chaotic.

And then what. Control IMAGEVIEW[12] The value hidden behind them is the value in the array num.

After every 2 clicks, see if the num[x] value of the clicked control is equal.

Equal to hide the control, without waiting for the card to be unified display back and error number +1.

It's simple, right?

The control removes the particle effect on GitHub, forgetting the address, and I'm sorry, but thank the author very much.


There are many ways to vary the number of random numbers, because the array is small, can be directly two for loop, can also be implemented recursively :


static intGetNumber(int[] A, intIndex, intMinValue, intMaxValue, intTemp, Random R) { for(intI=0; I< index; I++)    {if(a[I] = = temp) {intnewtemp= R.nextint (MaxValue) +minvalue;            A[index] =newtemp;returnGetNumber(A, Index, MinValue, MaxValue, newtemp, R;        }    }returnTemp;}






GitHub Address: Https://github.com/VongVia1209/MemoryCard

"Android Learning" uses Rxjava to complete puzzle-flop games

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.