Android项目开发之2048

Android项目开发之2048游戏

package com.songjie.game2048;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.GridLayout;

public class Game_View extends GridLayout {

	private List<Point> empty_card = new ArrayList<Point>();
	private Game_Card[][] card_map = new Game_Card[4][4];

	public Game_View(Context context) {
		super(context);

		init_GameView();
	}

	public Game_View(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);

		init_GameView();
	}

	public Game_View(Context context, AttributeSet attrs) {
		super(context, attrs);

		init_GameView();
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);

		int card_size = ((Math.min(w, h)) - 10) / 4;
		add_Card(card_size, card_size);

		start_Game();
	}

	private void start_Game() {

		Act_Main.get_ActMain().clear_Score();

		for (int y = 0; y < 4; y++) {
			for (int x = 0; x < 4; x++) {
				card_map[x][y].set_Num(0);
			}
		}
		add_RandomNum();
		add_RandomNum();
	}

	private void init_GameView() {

		setColumnCount(4);
		setBackgroundColor(0xffbbada0);

		setOnTouchListener(new OnTouchListener() {

			private float start_x, start_y, offset_x, offset_y;

			@Override
			public boolean onTouch(View v, MotionEvent event) {

				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					start_x = event.getX();
					start_y = event.getY();
					break;
				case MotionEvent.ACTION_UP:
					offset_x = event.getX() - start_x;
					offset_y = event.getY() - start_y;

					if (Math.abs(offset_x) > Math.abs(offset_y)) {
						if (offset_x < -5) {
							slide_Left();
						} else if (offset_x > 5) {
							slide_Right();
						}
					} else {
						if (offset_y < -5) {
							slide_Up();
						} else if (offset_y > 5) {
							slide_Down();
						}
					}

					break;
				}

				return true;
			}
		});
	}

	private void add_Card(int card_w, int card_h) {

		Game_Card card;

		for (int y = 0; y < 4; y++) {
			for (int x = 0; x < 4; x++) {
				card = new Game_Card(getContext());
				card.set_Num(0);
				addView(card, card_w, card_h);

				card_map[x][y] = card;
			}
		}
	}

	private void add_RandomNum() {

		empty_card.clear();

		for (int y = 0; y < 4; y++) {
			for (int x = 0; x < 4; x++) {
				if (card_map[x][y].get_Num() <= 0) {
					empty_card.add(new Point(x, y));
				}
			}
		}

		Point p = empty_card.remove((int) (Math.random() * empty_card.size()));
		card_map[p.x][p.y].set_Num(Math.random() > 0.1 ? 2 : 4);
	}

	private void check_Complete() {

		boolean complete = true;

		ALL: for (int y = 0; y < 4; y++) {
			for (int x = 0; x < 4; x++) {
				if (card_map[x][y].get_Num() == 0
						|| (x > 0 && card_map[x][y].equals(card_map[x - 1][y]))
						|| (x < 3 && card_map[x][y].equals(card_map[x + 1][y]))
						|| (y > 0 && card_map[x][y].equals(card_map[x][y - 1]))
						|| (y < 3 && card_map[x][y].equals(card_map[x][y + 1]))) {
					complete = false;
					break ALL;
				}
			}
		}

		is_Complete(complete);
	}

	private void is_Complete(boolean b) {

		if (b) {
			new AlertDialog.Builder(getContext())
					.setTitle("提示:")
					.setMessage("游戏结束!")
					.setPositiveButton("再来一次",
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface arg0,
										int arg1) {
									start_Game();
								}
							}).show();
		}
	}

	private void is_AddNew(boolean b) {
		if (b) {
			add_RandomNum();
			check_Complete();
		}
	}

	private void slide_Left() {

		boolean merg = false;

		for (int y = 0; y < 4; y++) {
			for (int x = 0; x < 4; x++) {

				for (int x1 = x + 1; x1 < 4; x1++) {
					if (card_map[x1][y].get_Num() > 0) {
						if (card_map[x][y].get_Num() <= 0) {
							card_map[x][y].set_Num(card_map[x1][y].get_Num());
							card_map[x1][y].set_Num(0);
							x--;
							merg = true;
						} else if (card_map[x][y].equals(card_map[x1][y])) {
							card_map[x][y]
									.set_Num((card_map[x1][y].get_Num()) * 2);
							card_map[x1][y].set_Num(0);

							Act_Main.get_ActMain().add_Score(
									card_map[x][y].get_Num());
							merg = true;
						}
						break;
					}
				}
			}
		}
		is_AddNew(merg);
	}

	private void slide_Right() {

		boolean merg = false;

		for (int y = 0; y < 4; y++) {
			for (int x = 3; x >= 0; x--) {

				for (int x1 = x - 1; x1 >= 0; x1--) {
					if (card_map[x1][y].get_Num() > 0) {
						if (card_map[x][y].get_Num() <= 0) {
							card_map[x][y].set_Num(card_map[x1][y].get_Num());
							card_map[x1][y].set_Num(0);
							x++;

							merg = true;
						} else if (card_map[x][y].equals(card_map[x1][y])) {
							card_map[x][y]
									.set_Num((card_map[x1][y].get_Num()) * 2);
							card_map[x1][y].set_Num(0);

							Act_Main.get_ActMain().add_Score(
									card_map[x][y].get_Num());
							merg = true;

						}
						break;
					}
				}
			}
		}
		is_AddNew(merg);
	}

	private void slide_Up() {

		boolean merg = false;

		for (int x = 0; x < 4; x++) {
			for (int y = 0; y < 4; y++) {

				for (int y1 = y + 1; y1 < 4; y1++) {
					if (card_map[x][y1].get_Num() > 0) {
						if (card_map[x][y].get_Num() <= 0) {
							card_map[x][y].set_Num(card_map[x][y1].get_Num());
							card_map[x][y1].set_Num(0);
							y--;
							merg = true;
						} else if (card_map[x][y].equals(card_map[x][y1])) {
							card_map[x][y]
									.set_Num((card_map[x][y1].get_Num()) * 2);
							card_map[x][y1].set_Num(0);

							Act_Main.get_ActMain().add_Score(
									card_map[x][y].get_Num());
							merg = true;
						}
						break;
					}
				}
			}
		}
		is_AddNew(merg);
	}

	private void slide_Down() {
		boolean merg = false;
		for (int x = 0; x < 4; x++) {
			for (int y = 3; y >= 0; y--) {

				for (int y1 = y - 1; y1 >= 0; y1--) {
					if (card_map[x][y1].get_Num() > 0) {
						if (card_map[x][y].get_Num() <= 0) {
							card_map[x][y].set_Num(card_map[x][y1].get_Num());
							card_map[x][y1].set_Num(0);
							y++;
							merg = true;
						} else if (card_map[x][y].equals(card_map[x][y1])) {
							card_map[x][y]
									.set_Num((card_map[x][y1].get_Num()) * 2);
							card_map[x][y1].set_Num(0);

							Act_Main.get_ActMain().add_Score(
									card_map[x][y].get_Num());
							merg = true;
						}
						break;
					}
				}
			}
		}
		is_AddNew(merg);
	}
}

相关推荐