给老婆写的的带返回的2048(数据库存储),虽然很烂。但是还是贴出来了
class="code_img_closed" src="/Upload/Images/2014111116/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('c13cee86-a294-4981-8dd9-109c8036d770',event)" src="/Upload/Images/2014111116/2B1B950FA3DF188F.gif" alt="" />1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context=".MyActivity"> 10 11 <LinearLayout 12 android:layout_width="match_parent" 13 android:layout_height="match_parent" 14 android:orientation="vertical"> 15 16 <LinearLayout 17 android:layout_width="match_parent" 18 android:layout_height="match_parent" 19 android:layout_weight="9" 20 android:orientation="vertical"> 21 22 <TextView 23 android:layout_width="match_parent" 24 android:layout_height="wrap_content" 25 android:gravity="center" 26 android:text="亲爱的,你最棒!" 27 android:textSize="30dp" /> 28 29 <LinearLayout 30 android:layout_width="match_parent" 31 android:layout_height="wrap_content" 32 android:gravity="center"> 33 34 <TextView 35 android:layout_width="wrap_content" 36 android:layout_height="wrap_content" 37 android:text="分数:" 38 android:textSize="30dp" /> 39 40 <TextView 41 android:id="@+id/score" 42 android:layout_width="wrap_content" 43 android:layout_height="wrap_content" 44 android:textSize="30dp" 45 android:text="0"/> 46 </LinearLayout> 47 48 <TextView 49 android:layout_width="match_parent" 50 android:layout_height="wrap_content" 51 android:gravity="center" 52 android:text="亲爱的,别忘了分享哦" 53 android:textSize="30dp" /> 54 55 <TextView 56 android:layout_width="match_parent" 57 android:layout_height="wrap_content" 58 android:gravity="center" 59 android:text="^_~" 60 android:textSize="30dp" /> 61 </LinearLayout> 62 63 <LinearLayout 64 android:id="@+id/gameView" 65 android:layout_width="match_parent" 66 android:layout_height="match_parent" 67 android:layout_weight="5" 68 android:background="#ffbbada0" 69 android:orientation="vertical" 70 android:padding="5dp"> 71 72 <LinearLayout 73 android:layout_width="match_parent" 74 android:layout_height="match_parent" 75 android:layout_weight="1"> 76 77 <TextView 78 android:id="@+id/t0" 79 android:layout_width="match_parent" 80 android:layout_height="match_parent" 81 android:layout_margin="5dp" 82 android:layout_weight="1" 83 android:background="#ffcdc1b4" 84 android:gravity="center" 85 android:text="1" 86 android:textSize="30dp"/> 87 88 <TextView 89 android:id="@+id/t1" 90 android:layout_width="match_parent" 91 android:layout_height="match_parent" 92 android:layout_margin="5dp" 93 android:layout_weight="1" 94 android:gravity="center" /> 95 96 <TextView 97 android:id="@+id/t2" 98 android:layout_width="match_parent" 99 android:layout_height="match_parent" 100 android:layout_margin="5dp" 101 android:layout_weight="1" 102 android:gravity="center" /> 103 104 <TextView 105 android:id="@+id/t3" 106 android:layout_width="match_parent" 107 android:layout_height="match_parent" 108 android:layout_margin="5dp" 109 android:layout_weight="1" 110 android:gravity="center" /> 111 </LinearLayout> 112 113 <LinearLayout 114 android:layout_width="match_parent" 115 android:layout_height="match_parent" 116 android:layout_weight="1"> 117 118 <TextView 119 android:id="@+id/t4" 120 android:layout_width="match_parent" 121 android:layout_height="match_parent" 122 android:layout_margin="5dp" 123 android:layout_weight="1" 124 android:gravity="center" /> 125 126 <TextView 127 android:id="@+id/t5" 128 android:layout_width="match_parent" 129 android:layout_height="match_parent" 130 android:layout_margin="5dp" 131 android:layout_weight="1" 132 android:gravity="center" /> 133 134 <TextView 135 android:id="@+id/t6" 136 android:layout_width="match_parent" 137 android:layout_height="match_parent" 138 android:layout_margin="5dp" 139 android:layout_weight="1" 140 android:gravity="center" /> 141 142 <TextView 143 android:id="@+id/t7" 144 android:layout_width="match_parent" 145 android:layout_height="match_parent" 146 android:layout_margin="5dp" 147 android:layout_weight="1" 148 android:gravity="center" /> 149 </LinearLayout> 150 151 <LinearLayout 152 android:layout_width="match_parent" 153 android:layout_height="match_parent" 154 android:layout_weight="1"> 155 156 <TextView 157 android:id="@+id/t8" 158 android:layout_width="match_parent" 159 android:layout_height="match_parent" 160 android:layout_margin="5dp" 161 android:layout_weight="1" 162 android:gravity="center" /> 163 164 <TextView 165 android:id="@+id/t9" 166 android:layout_width="match_parent" 167 android:layout_height="match_parent" 168 android:layout_margin="5dp" 169 android:layout_weight="1" 170 android:gravity="center" /> 171 172 <TextView 173 android:id="@+id/t10" 174 android:layout_width="match_parent" 175 android:layout_height="match_parent" 176 android:layout_margin="5dp" 177 android:layout_weight="1" 178 android:gravity="center" /> 179 180 <TextView 181 android:id="@+id/t11" 182 android:layout_width="match_parent" 183 android:layout_height="match_parent" 184 android:layout_margin="5dp" 185 android:layout_weight="1" 186 android:gravity="center" /> 187 </LinearLayout> 188 189 <LinearLayout 190 android:layout_width="match_parent" 191 android:layout_height="match_parent" 192 android:layout_weight="1"> 193 194 <TextView 195 android:id="@+id/t12" 196 android:layout_width="match_parent" 197 android:layout_height="match_parent" 198 android:layout_margin="5dp" 199 android:layout_weight="1" 200 android:gravity="center" /> 201 202 <TextView 203 android:id="@+id/t13" 204 android:layout_width="match_parent" 205 android:layout_height="match_parent" 206 android:layout_margin="5dp" 207 android:layout_weight="1" 208 android:gravity="center" /> 209 210 <TextView 211 android:id="@+id/t14" 212 android:layout_width="match_parent" 213 android:layout_height="match_parent" 214 android:layout_margin="5dp" 215 android:layout_weight="1" 216 android:gravity="center" /> 217 218 <TextView 219 android:id="@+id/t15" 220 android:layout_width="match_parent" 221 android:layout_height="match_parent" 222 android:layout_margin="5dp" 223 android:layout_weight="1" 224 android:gravity="center" /> 225 </LinearLayout> 226 </LinearLayout> 227 </LinearLayout> 228 229 </RelativeLayout>页面布局
1 import android.content.Context; 2 import android.database.Cursor; 3 import android.database.sqlite.SQLiteDatabase; 4 import android.graphics.Color; 5 import android.support.v7.app.ActionBarActivity; 6 import android.os.Bundle; 7 import android.util.Log; 8 import android.view.GestureDetector; 9 import android.view.Menu; 10 import android.view.MenuItem; 11 import android.view.MotionEvent; 12 import android.view.View; 13 import android.widget.LinearLayout; 14 import android.widget.TextView; 15 16 import java.util.Random; 17 18 19 public class MyActivity extends ActionBarActivity implements GestureDetector.OnGestureListener, View.OnTouchListener { 20 21 private TextView[] allTextView; 22 private int[] numData; 23 private String[] colorData; 24 private TextView score; 25 SQLiteDatabase db; 26 int scoreNum = 0; 27 boolean isDoAdd; 28 29 private GestureDetector mGestureDetector; 30 31 @Override 32 protected void onCreate(Bundle savedInstanceState) { 33 super.onCreate(savedInstanceState); 34 setContentView(R.layout.activity_my); 35 init(); 36 mGestureDetector = new GestureDetector(this, this); 37 LinearLayout gameView = (LinearLayout) findViewById(R.id.gameView); 38 score = (TextView) findViewById(R.id.score); 39 score.setOnTouchListener(new View.OnTouchListener() { 40 @Override 41 public boolean onTouch(View view, MotionEvent motionEvent) { 42 backStateFromDb(); 43 return false; 44 } 45 }); 46 gameView.setOnTouchListener(this); 47 gameView.setClickable(true); 48 } 49 50 @Override 51 protected void onPause() { 52 saveStateToDb(); 53 db.close(); 54 super.onPause(); 55 } 56 57 @Override 58 protected void onResume() { 59 super.onResume(); 60 backStateFromDb(); 61 } 62 63 64 private void gameStart() { 65 this.scoreNum = 0; 66 this.setScore(0); 67 for (int i = 0; i < 16; i++) { 68 setNum(i, 1); 69 } 70 addOneNum(); 71 addOneNum(); 72 } 73 74 75 @Override 76 public boolean onDown(MotionEvent motionEvent) { 77 return false; 78 } 79 80 @Override 81 public void onShowPress(MotionEvent motionEvent) { 82 83 } 84 85 86 @Override 87 public boolean onSingleTapUp(MotionEvent motionEvent) { 88 return false; 89 } 90 91 @Override 92 public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent2, float v, float v2) { 93 return false; 94 } 95 96 @Override 97 public void onLongPress(MotionEvent motionEvent) { 98 99 } 100 101 @Override 102 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 103 isDoAdd = false; 104 saveStateToDb(); 105 if (Math.abs(velocityX) > Math.abs(velocityY)) { 106 if (velocityX > 0) { 107 moveRight(); 108 } else { 109 moveLeft(); 110 } 111 } else { 112 if (velocityY > 0) { 113 moveDown(); 114 } else { 115 moveTop(); 116 } 117 } 118 if (isDoAdd) { 119 addOneNum(); 120 } else { 121 deleteOneDataFromDb(); 122 } 123 return false; 124 } 125 126 127 private void addOneNum() { 128 int[] nullCell = new int[16]; 129 int count = 0; 130 for (int i = 0; i < 16; i++) { 131 if (numData[i] == 1) { 132 nullCell[count] = i; 133 count++; 134 } 135 } 136 if (count == 0) { 137 return; 138 } 139 Random r = new Random(); 140 int index = r.nextInt(count); 141 int data = r.nextInt(10); 142 data = data == 0 ? 1 : 0; 143 setNum(nullCell[index], (data + 1) * 2); 144 } 145 146 private void moveDown() { 147 //Toast.makeText(getApplicationContext(),"下",Toast.LENGTH_SHORT).show(); 148 for (int y = 0; y < 4; y++) { 149 for (int x = 3; x > 0; x--) { 150 int p = (x * 4) + y; 151 for (int z = 1; z < 4; z++) { 152 int q = (x - z) * 4 + y; 153 if (q < y) { 154 break; 155 } 156 if (numData[q] == 1) { 157 continue; 158 } else if (numData[p] == 1 && numData[q] == 1) { 159 continue; 160 } else if (numData[p] == 1 && numData[q] != 1) { 161 setNum(p, numData[q]); 162 setNum(q, 1); 163 continue; 164 } else if (numData[p] != numData[q]) { 165 break; 166 } else { 167 setNum(p, numData[p] * 2); 168 setScore(numData[p] * 2); 169 setNum(q, 1); 170 break; 171 } 172 } 173 } 174 } 175 } 176 177 private void moveTop() { 178 //Toast.makeText(getApplicationContext(),"上",Toast.LENGTH_SHORT).show(); 179 for (int y = 0; y < 4; y++) { 180 for (int x = 0; x < 3; x++) { 181 int p = (x * 4) + y; 182 for (int z = 1; z < 4; z++) { 183 int q = (x + z) * 4 + y; 184 if (q > 12 + y) { 185 break; 186 } 187 if (numData[q] == 1) { 188 continue; 189 } else if (numData[p] == 1 && numData[q] == 1) { 190 continue; 191 } else if (numData[p] == 1 && numData[q] != 1) { 192 setNum(p, numData[q]); 193 setNum(q, 1); 194 continue; 195 } else if (numData[p] != numData[q]) { 196 break; 197 } else { 198 setNum(p, numData[p] * 2); 199 setScore(numData[p] * 2); 200 setNum(q, 1); 201 break; 202 } 203 } 204 } 205 } 206 } 207 208 private void moveLeft() { 209 // Toast.makeText(getApplicationContext(),"左",Toast.LENGTH_SHORT).show(); 210 for (int x = 0; x < 4; x++) { 211 for (int y = 0; y < 3; y++) { 212 int p = (x * 4) + y; 213 for (int z = 1; z < 4; z++) { 214 int q = p + z; 215 if (q > (x + 1) * 4 - 1) { 216 break; 217 } 218 if (numData[q] == 1) { 219 continue; 220 } else if (numData[p] == 1 && numData[q] == 1) { 221 continue; 222 } else if (numData[p] == 1 && numData[q] != 1) { 223 setNum(p, numData[q]); 224 setNum(q, 1); 225 continue; 226 } else if (numData[p] != numData[q]) { 227 break; 228 } else { 229 setNum(p, numData[p] * 2); 230 setScore(numData[p] * 2); 231 setNum(q, 1); 232 break; 233 } 234 } 235 } 236 } 237 } 238 239 private void moveRight() { 240 //Toast.makeText(getApplicationContext(),"右",Toast.LENGTH_SHORT).show(); 241 for (int x = 0; x < 4; x++) { 242 for (int y = 3; y > 0; y--) { 243 int p = (x * 4) + y; 244 for (int z = 1; z < 4; z++) { 245 int q = p - z; 246 if (q < x * 4) { 247 break; 248 } 249 if (numData[q] == 1) { 250 continue; 251 } else if (numData[p] == 1 && numData[q] == 1) { 252 continue; 253 } else if (numData[p] == 1 && numData[q] != 1) { 254 setNum(p, numData[q]); 255 setNum(q, 1); 256 continue; 257 } else if (numData[p] != numData[q]) { 258 break; 259 } else { 260 setNum(p, numData[p] * 2); 261 setScore(numData[p] * 2); 262 setNum(q, 1); 263 break; 264 } 265 } 266 } 267 } 268 } 269 270 271 private void setScore(int s) { 272 scoreNum += s; 273 score.setText(scoreNum + ""); 274 } 275 276 private void init() { 277 dbCreate(); 278 findAllTextView(); 279 setColorData(); 280 numData = new int[16]; 281 } 282 283 private void dbCreate() { 284 db = openOrCreateDatabase("My2048Database.db", Context.MODE_PRIVATE, null); 285 db.execSQL("create table if not exists GameRecord(" + 286 "Id INTEGER PRIMARY KEY,\n" + 287 " Game VARCHAR NOT NULL,\n" + 288 " Score INTEGER NOT NULL )"); 289 } 290 291 private void saveStateToDb() { 292 String gameNumState = ""; 293 for (int i = 0; i < 16; i++) { 294 gameNumState += (numData[i] + ","); 295 } 296 db.execSQL("insert into GameRecord(Game,Score) values('" + 297 gameNumState + "','" + 298 scoreNum + "')"); 299 Log.d("DB:", "Save"); 300 } 301 302 private void backStateFromDb() { 303 Cursor c = db.rawQuery("select * from GameRecord order by Id desc limit 1", null); 304 if (!c.moveToNext()) { 305 gameStart(); 306 return; 307 } 308 int id = c.getInt(c.getColumnIndex("Id")); 309 String gameStateNnm = c.getString(c.getColumnIndex("Game")); 310 int score = c.getInt(c.getColumnIndex("Score")); 311 this.scoreNum = 0; 312 this.setScore(score); 313 Log.d("DB:", "Score-" + score); 314 Log.d("DB:", "gameDb-" + gameStateNnm); 315 String[] numString = gameStateNnm.split(","); 316 for (int i = 0; i < 16; i++) { 317 this.setNum(i, Integer.parseInt(numString[i])); 318 } 319 Log.d("DB:", "dbId-" + id + ""); 320 db.delete("GameRecord", "Id = ?", new String[]{id + ""}); 321 Log.d("DB:", "Back"); 322 } 323 324 private void deleteOneDataFromDb() { 325 Cursor c = db.rawQuery("select * from GameRecord order by Id desc limit 1", null); 326 if (!c.moveToNext()) { 327 return; 328 } 329 int id = c.getInt(c.getColumnIndex("Id")); 330 db.delete("GameRecord", "Id = ?", new String[]{id + ""}); 331 Log.d("DB:", "DeleteOneNoUse"); 332 } 333 334 private void setNum(int index, int num) { 335 numData[index] = num; 336 int colorIndex = (int) Math.log((double) num); 337 if (colorIndex > 15) { 338 colorIndex = 15; 339 } 340 if (num == 1) { 341 allTextView[index].setText(""); 342 allTextView[index].setBackgroundColor(Color.parseColor(colorData[colorIndex])); 343 } else { 344 allTextView[index].setText("" + num); 345 allTextView[index].setBackgroundColor(Color.parseColor(colorData[colorIndex])); 346 if (num > 512) { 347 allTextView[index].setTextSize(20); 348 } else { 349 allTextView[index].setTextSize(30); 350 } 351 } 352 isDoAdd = true; 353 } 354 355 private void setColorData() { 356 colorData = new String[16]; 357 colorData[0] = "#cdc1b4"; 358 colorData[1] = "#eee4da"; 359 colorData[2] = "#ede0c8"; 360 colorData[3] = "#ece0c8"; 361 colorData[4] = "#ecd0c8"; 362 colorData[5] = "#ebe0c8"; 363 colorData[6] = "#ede0c0"; 364 colorData[7] = "#ede0c8"; 365 colorData[8] = "#aae0c8"; 366 colorData[9] = "#edaac8"; 367 colorData[10] = "#ede0aa"; 368 colorData[11] = "#99e0c8"; 369 colorData[12] = "#88e0c8"; 370 colorData[13] = "#77e0c8"; 371 colorData[14] = "#66e0c8"; 372 colorData[15] = "#55e0c8"; 373 } 374 375 private void findAllTextView() { 376 allTextView = new TextView[16]; 377 allTextView[0] = (TextView) findViewById(R.id.t0); 378 allTextView[1] = (TextView) findViewById(R.id.t1); 379 allTextView[2] = (TextView) findViewById(R.id.t2); 380 allTextView[3] = (TextView) findViewById(R.id.t3); 381 allTextView[4] = (TextView) findViewById(R.id.t4); 382 allTextView[5] = (TextView) findViewById(R.id.t5); 383 allTextView[6] = (TextView) findViewById(R.id.t6); 384 allTextView[7] = (TextView) findViewById(R.id.t7); 385 allTextView[8] = (TextView) findViewById(R.id.t8); 386 allTextView[9] = (TextView) findViewById(R.id.t9); 387 allTextView[10] = (TextView) findViewById(R.id.t10); 388 allTextView[11] = (TextView) findViewById(R.id.t11); 389 allTextView[12] = (TextView) findViewById(R.id.t12); 390 allTextView[13] = (TextView) findViewById(R.id.t13); 391 allTextView[14] = (TextView) findViewById(R.id.t14); 392 allTextView[15] = (TextView) findViewById(R.id.t15); 393 } 394 395 396 @Override 397 public boolean onCreateOptionsMenu(Menu menu) { 398 // Inflate the menu; this adds items to the action bar if it is present. 399 getMenuInflater().inflate(R.menu.my, menu); 400 return true; 401 } 402 403 @Override 404 public boolean onOptionsItemSelected(MenuItem item) { 405 // Handle action bar item clicks here. The action bar will 406 // automatically handle clicks on the Home/Up button, so long 407 // as you specify a parent activity in AndroidManifest.xml. 408 int id = item.getItemId(); 409 if (id == R.id.action_settings) { 410 try { 411 db.execSQL("delete from GameRecord"); 412 } catch (Exception e) { 413 Log.d("DB:", e.getMessage()); 414 } 415 416 Log.d("DB:", "Restart"); 417 gameStart(); 418 return true; 419 } 420 return super.onOptionsItemSelected(item); 421 } 422 423 @Override 424 public boolean onTouch(View view, MotionEvent motionEvent) { 425 426 return mGestureDetector.onTouchEvent(motionEvent); 427 } 428 }java代码
忘了截图,喜欢的自己运行一下