Android version 2048

Source: Internet
Author: User

Although said 2048 is a long time ago compared to the small game of fire, but until recently had the opportunity to study the next 2048 realization of the source code, here simply write my (Bie) 's (ren) idea:

First of all 2048 need to have 16 cards, this card can be used Framelayout subclass to implement (inherit), which can be used to set the background color, the number of display, and so on. So we can simply implement this card:

public class Card extends Framelayout {

private int num = 0;
Private TextView label;

Public Card (Context context) {
Super (context);

label = new TextView (GetContext ());
Label.settextsize (32);
Label.setgravity (Gravity.center);
Label.setbackgroundcolor (0X33FFFFFF);
Layoutparams LP = new Layoutparams (-1,-1);
Lp.setmargins (10, 10, 0, 0);
AddView (label, LP);
Setnum (0);
}

**********************************************

Secondly, we need to add 16 cards to the screen, how should we implement this screen? We can implement the subclass of GridLayout (inheritance) and use a two-dimensional array to store the status of each card:

public class Gameview extends GridLayout {

Private Card cardsmap[][] = new CARD[4][4];
Private list<point> Emptypoint = new ArrayList ();//Record empty position

***********************************************

private void Addcards (int cardwidth, int cardheight) {//Add card
Card cd;
for (int x = 0; x < 4; × x + +) {
for (int y = 0; y < 4; y++) {
cd = new Card (GetContext ());
Cd.setnum (0);
AddView (CD, Cardwidth, Cardheight);
Cardsmap[x][y] = CD;
}
}

}

******************************************

Then we need to judge the user's gestures. By setting a Setontouchlistener (New Ontouchlistener ()) to listen to the user's actions, when the user presses the screen, record the coordinates at this time, when the user's finger left the screen, calculate the offset of the coordinates, According to the relative difference of the coordinate offset, the user's intention is to go up, down, or left, to the right:

public boolean OnTouch (View arg0, motionevent arg1) {

Switch (arg1.getaction ()) {
Case Motionevent.action_down:
StartX = Arg1.getx ();
Starty = Arg1.gety ();
Break
Case MOTIONEVENT.ACTION_UP:
OffsetX = Arg1.getx ()-StartX;
OffsetY = Arg1.gety ()-starty;

if (Math.Abs (OffsetX) > Math.Abs (OffsetY)) {
if (OffsetX <-3) {
Slideleft ();
} else if (OffsetX > 2) {
Slideright ();
}
} else {
if (OffsetY <-3) {
Slideup ();
} else if (OffsetY > 3) {
Slidedown ();
}
}
Break

}
return true;

}

**************************************

After judging the user's intentions, we should deal with the corresponding events, here only the left-swipe processing code, the other three-directional processing similar:

Boolean flag = false;

for (int y = 0; y < 4; y++) {
for (int x = 0; x < 4; × x + +) {
for (int x1 = x + 1; x1 < 4; x1++) {
if (Cardsmap[x1][y].getnum () > 0) {
if (cardsmap[x][y].getnum () <= 0) {
Cardsmap[x][y].setnum (Cardsmap[x1][y].getnum ());
Cardsmap[x1][y].setnum (0);
x--;
Flag = true;

} else if (Cardsmap[x][y].equals (Cardsmap[x1][y])) {
Cardsmap[x][y].setnum (Cardsmap[x][y].getnum () * 2);
Cardsmap[x1][y].setnum (0);
Mainactivity.getmainactivity (). Addmyscore (
Cardsmap[x][y].getnum ());
Flag = true;
}
Break
}
}
}
}
if (flag) {
Addrandom ();//randomly add a card (actually set the number of the card in the corresponding position)
Checkcomplete ();//Determine if the game is over

**************************************************

The above is the basic logic of the 2048 implementation of the Android version, which may be somewhat less detailed. If you need detailed source code, you can contact me ha.

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.