Android Write 2048 Games _android

Source: Internet
Author: User
Tags abs getcolor

Let's say 2048 game rules:

At the beginning, there are two numbers randomly appearing in the chessboard, which can only be 2 or 4.

Players can choose from top to bottom four directions, if the number in the chessboard appears to shift or merge, as effective mobile

In the direction of the player's choice, if the same number is merged, each effective move can be merged at the same time, but cannot be merged continuously.

All new generated numbers that are merged are added as valid scores for that step

The player chooses the direction row or column front has the space to appear the displacement

Every effective move one step, the space of the chessboard (no number) randomly appears a number (still may be 2 or 4)

The chessboard is filled with numbers, unable to move effectively, negative, game over

2048 on board, award wins, game over

The following code

Mainactivity class

public class Mainactivity extends Appcompatactivity {

 @Override
 protected void OnCreate (Bundle Savedinstancestate) {
 super.oncreate (savedinstancestate);
 Setcontentview (New Gameview (this));
 }


Define a card class to display data

/card type public class cards extends Framelayout {//2 TextView TV;
 private int number = 0;

 int width;
 Public card (context, int-width) {super (context);
 This.width = width;
 Init ();
 private void Init () {TV = new TextView (GetContext ());
 Setpadding (5, 5, 5, 5);
 Framelayout.layoutparams LP = new Layoutparams (width-10, width-10);
 TV.SETLAYOUTPARAMS (LP);
 Tv.setgravity (Gravity.center);
 Tv.settextcolor (Color.White);
 Tv.settextsize (48);
 This.addview (TV);
 SetColor ();
 public void Setnumber (int number) {this.number = number;
 if (number = = 0) tv.settext ("");
 else Tv.settext (number + "");
 SetColor (); 
  @Override public String toString () {return "card{" + "tv=" + TV + ", number=" + number + ", width=" + width +
 '}';
  private void SetColor () {switch (number) {Case 0:tv.setbackgroundcolor (getresources (). GetColor (R.COLOR.C0));
  Break
  Case 2:tv.setbackgroundcolor (Getresources (). GetColor (R.COLOR.C2));
  Break Case 4:TV. SetBackgroundColor (Getresources (). GetColor (r.color.c4));
  Break
  Case 8:tv.setbackgroundcolor (Getresources (). GetColor (R.COLOR.C8));
  Break
  Case 16:tv.setbackgroundcolor (Getresources (). GetColor (R.COLOR.C16));
  Break
  Case 32:tv.setbackgroundcolor (Getresources (). GetColor (R.COLOR.C32));
  Break
  Case 64:tv.setbackgroundcolor (Getresources (). GetColor (R.COLOR.C64));
  Break
  Case 128:tv.setbackgroundcolor (Getresources (). GetColor (r.color.c128));
  Break
  Case 256:tv.setbackgroundcolor (Getresources (). GetColor (r.color.c256));
  Break
  Case 512:tv.setbackgroundcolor (Getresources (). GetColor (r.color.c512));
  Break
  Case 1024:tv.setbackgroundcolor (Getresources (). GetColor (r.color.c1024));
  Break
  Case 2048:tv.setbackgroundcolor (Getresources (). GetColor (r.color.c2048));
 Break
 } public int GetNumber () {return number;
 }

}

Define some of the above colors in the colors of values

<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
 <color name= "Colorprimary" > #3F51B5 </color>
 <color name= "Colorprimarydark" > #303F9F </color>
 <color name= "Coloraccent" > #FF4081 </color>

 <color name= "C0" > #43382a </color>
 <color name= "C2" > #2a8618 </color>
 <color name= "C4" > #3d6319 </color>
 <color name= "C8" > #F2B179 </color>
 <color name= "C16" > #F59563 </color>
 <color name= "C32" > #F57C5F </color>
 < Color Name= "C64" > #5b69b7 </color>
 <color name= "C128" > #b37d3e </color>
 <color Name = "c256" > #3a9096 </color>
 <color name= "c512" > #d7882c </color>
 <color name= "c1024" > #5c1b78 </color>
 <color name= "c2048" > #640548 </color>
</resources>


Layout class and the Operation method inside

public class Gameview extends GridLayout {int[][] values = new INT[4][4];


Card[][] cards = new CARD[4][4];
 integer-128-127 = = Greater than this number two objects public Gameview (context) {super (context);
 Init ();
 Public Gameview (context, AttributeSet attrs) {Super (context, attrs);
 Init ();
 private void Init () {Setcolumncount (4);
 @Override protected void onsizechanged (int w, int h, int oldw, int oldh) {super.onsizechanged (W, H, OLDW, OLDH);
 Newgame (); private void Newgame () {//viewgroup.layoutparams LP = new Viewgroup.layoutparams (int) (Getresources (). Getdisplaymet
 RICS (). WIDTHPIXELS/4), (int) (Getresources (). Getdisplaymetrics (). Widthpixels/4));
Gridlayout.layoutparams params = new Gridlayout.layoutparams ();
Params.width = Getresources (). Getdisplaymetrics (). Widthpixels/4;
 Params.height = Getresources (). Getdisplaymetrics (). Widthpixels/4;
 LOG.E ("TAG", Params.width + "" + params.height);
 This.removeallviews (); Gridlayout.layoUtparams LPA = new Gridlayout.layoutparams (LP);
  for (int i = 0; i < 4; i++) {for (int j = 0; J < 4; J +) {Values[i][j] = 0;
  CARDS[I][J] = new Card (GetContext (), Getresources (). Getdisplaymetrics (). WIDTHPIXELS/4);
  Cards[i][j].setlayoutparams (params);
  CARDS[I][J].UPLV ();
  This.addview (Cards[i][j]);
 }//Create the initial two card int i = (int) (Math.random () * 16);
 int j = 0;
 do {j = (int) (Math.random () *),//0-15 3 3} while (j = = i);
 LOG.E ("TAG", i + "" + j); Values[i/4][i% 4] = Math.random () * < 1?
 4:2; Values[j/4][j% 4] = Math.random () * < 1?
 4:2;
 Setvalues ();
 float OLDX, Oldy;


 int move =-1;
 @Override public boolean ontouchevent (Motionevent event) {float x = Event.getx ();
 Float y = event.gety ();
  Switch (event.getaction ()) {Case MotionEvent.ACTION_DOWN:move =-1;
  OLDX = x;
  Oldy = y;
  Break
Case MotionEvent.ACTION_MOVE:if (Math.Abs (oldx-x) > Math.Abs (oldy-y)) {if (Oldx-x > 15) {//Left   LOG.E ("TAG", "---------->>>");
   move = 1;
   else if (Oldx-x < 15) {//Right LOG.E ("TAG", "---------->>>");
   move = 2;
   } else {if (Oldy-y >) {move = 3;
   else if (Oldy-y < -15) {move = 4;
  }} break;
  Case MOTIONEVENT.ACTION_UP://Record before array int[][] temp = new INT[4][4];
   for (int i = 0; i < 4; i++) {for (int j = 0; J < 4; J +) {Temp[i][j] = values[i][j];
   } switch (move) {case 1:left ();
   Break
   Case 2:right ();
   Break
   Case 3:up ();
   Break
   Case 4:down ();
  Break
  } setvalues (); if (move!=-1) {//is the current array for (int i = 0; i < 4; i++) {for (int j = 0; J < 4; J +) {if values[i][j
    ]!= Temp[i][j]) {Addcard ();
    return true;
  To determine whether the game wins or ends 4 if (Iswin ()) {Toast.maketext (GetContext (), "Game win", Toast.length_short). Show ();
   } if (Isover ()) {this.removeallviews (); TextView TV = new TextView (GetconText ());
   Tv.settext ("Game over, click to start anew");
   This.addview (TV);
   Tv.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {newgame ();
  }
   });
 } break;
 return true;
  private void Addcard () {while (true) {int j = (int) (Math.random () * 16);
  if (values[j/4][j% 4] = = 0) {values[j/4][j% 4] = 2;
  Cards[j/4][j% 4].setnumber (2);
  Return }} public void Left () {//Remove 4 sets for (int i = 0; i < 4; i++) {list<integer> List = new ARRAYLIST&L
  T;> ();
  for (int j = 0; J < 4; J +) {int value = Values[i][j];
  if (value!= 0) list.add (value);
  }//Compare LOG.E ("TAG", list.tostring ());
  if (list.size () = = 0) continue;
  else if (list.size () = = 1) {Values[i][0] = list.get (0);
  for (int j = 0; J < 3; J +) {values[i][j + 1] = 0;
   } else if (list.size () = = 2) {if (List.get (0). Equals (List.get (1))) {Values[i][0] = list.get (0) * 2; Three values complement 0 for (int j = 0; J < 3; + +) {
   Values[i][j + 1] = 0;
   } else {values[i][0] = list.get (0);
   VALUES[I][1] = list.get (1);
   VALUES[I][2] = 0;
  VALUES[I][3] = 0;
   } else if (list.size () = = 3) {if (List.get (0). Equals (List.get (1))) {Values[i][0] = list.get (0) * 2;
   VALUES[I][1] = List.get (2);
   VALUES[I][2] = 0;
  VALUES[I][3] = 0;
   else if (List.get (1). Equals (List.get (2))) {Values[i][0] = list.get (0);
   VALUES[I][1] = List.get (2) * 2;
   VALUES[I][2] = 0;
  VALUES[I][3] = 0;
   else {values[i][0] = list.get (0);
   VALUES[I][1] = list.get (1);
   VALUES[I][2] = List.get (2);
  VALUES[I][3] = 0; } else {if (list.get (0). Equals (List.get (1)) {if List.get (3). Equals (List.get (2))) {Values[i][0] = List.get
   (0) * 2;
   VALUES[I][1] = List.get (2) * 2;
   VALUES[I][2] = 0;
   VALUES[I][3] = 0;
   else {values[i][0] = list.get (0) * 2;
   VALUES[I][1] = List.get (2);
   VALUES[I][2] = List.get (3);
   VALUES[I][3] = 0;
 } else {//1 and 2 are not equal///2 3 equal, unequal  if (List.get (1). Equals (List.get (2))) {Values[i][0] = list.get (0);
   VALUES[I][1] = list.get (1) * 2;
   VALUES[I][2] = List.get (3);
   VALUES[I][3] = 0;
    else {if (List.get (2). Equals (List.get (3))) {Values[i][0] = list.get (0);
    VALUES[I][1] = list.get (1);
    VALUES[I][2] = List.get (2) * 2;
   VALUES[I][3] = 0; }}}} private void Delete () {//LOG.E ("TAG", "--------------------Execution");////for (int i = 0; I & Lt 4;  i++) {//for (int j = 0; J < 3; J + +) {//} card = Cards[i][j];//LOG.E ("TAG", "I:" + i + "J:" + j + "num:" +
Card.getnumber ()); if (card.getnumber () = = 0) {//Boolean issub = false;//for (int k = j; k < 3; k++) {//Cards[i][k].setnum
ber (Cards[i][k + 1].getnumber ());
if (Cards[i][k + 1].getnumber ()!= 0) {//Issub = true;//}/////if (issub)//j--;
Cards[i][3].setnumber (0); else if (card.getnumber () = = Cards[i][j + 1].getnumber ()) {//CARD.UPLV ();//Cards[i][j + 1].setnumber (0);    Back forward/for (int k = j + 1; k < 3; k++) {//Cards[i][k].setnumber (Cards[i][k + 1].getnumber ());//
Cards[i][3].setnumber (0);
j--; //for (int j = 0; J < 4; + +) {//column//for (int i = 3; I >= 1; i--) {//Card card = cards[j][
I];   if (card.getnumber () = 0) {////All line Left///////////////////////////////////////////////// if (cards[j][i-1].getnumber () = = 0) {///From I--"i-1//For" (int k = i-1; k < 3; k++) {//CARDS[J][K].SETN
Umber (Cards[j][k + 1].getnumber ());
}//Cards[j][3].setnumber (0); 
else if (cards[j][i-1].getnumber () = = Card.getnumber ()) {//CARDS[J][I-1].UPLV ();//Card.setnumber (0); for (int k = i; k < 3; k++) {//Cards[j][k].setnumber (Cards[j][k + 1].getnumber ());//}//Cards[j][3].s
Etnumber (0);
 }//////}//} public void Right () {Mirrorh ();
 Left ();
 Mirrorh (); private void Mirrorh () {for (int i = 0; i < 4; i++)
  {int temp = values[i][0];
  Values[i][0] = values[i][3];
  VALUES[I][3] = temp;
  temp = values[i][1];
  VALUES[I][1] = values[i][2];
 VALUES[I][2] = temp;
 } public void Down () {//L int[][] temp = new INT[4][4];
  for (int i = 0; i < 4; i++) {for (int j = 0; J < 4; J +) {Temp[i][j] = Values[3-j][i];
 } values = temp;
 Left ();
 temp = new Int[4][4];
  for (int i = 0; i < 4; i++) {for (int j = 0; J < 4; J +) {Temp[i][j] = values[j][3-i];
 } values = temp;
 public void Up () {Mirrorv ();
 Down ();
 Mirrorv ();
  private void Mirrorv () {for (int j = 0; J < 4; J + +) {int temp = values[0][j];
  VALUES[0][J] = Values[3][j];
  VALUES[3][J] = temp;
  temp = Values[1][j];
  VALUES[1][J] = Values[2][j];
 VALUES[2][J] = temp; } public void Setvalues () {for (int i = 0; i < 4; i++) {for (int j = 0; J < 4; J +) {System.out.print (
  VALUES[I][J] + "");
  Cards[i][j].setnumber (Values[i][j]);} System.out.println (); } public boolean Iswin () {to (int i = 0; i < 4; i++) {for (int j = 0; J < 4; J +) {if (values[i][j)
  = = = 2048) return true;
 return false; public Boolean isover () {to (int i = 0; i < 4; i++) {for (int j = 0; J < 4; J +) {if (values[i][j] = 0
  ) return false;
  }}//full for (int i = 0; i < 4; i++) {for (int j = 0; J < 4; J +) {int value = Values[i][j];
  if (i > 1 && value = = Values[i-1][j]) return false;
  else if (I < 3 && value = = Values[i + 1][j]) return false;
  else if (J > 1 && value = = Values[i][j-1]) return false;
  else if (J < 3 && value = = values[i][j + 1]) return false;
 } return true;
 }
}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.