`

2048源码(核心算法有,缺少几个anctionbar,以后补上)

阅读更多
2048游戏基本上有四部分组成,
1:主activity,包含游戏块的16个方格,上面统计分数的模块
2:底下的gridview,监听上下左右的滑动,进行事件处理,
3:每一个卡片,里面的内容很简单,只有一个text,记录显示的数字
4:Actionbar,是游戏用重新开始,设置等功能(这个在底下可以下载的代码里面还没有实现)


写代码的流程
1:设计游戏的布局,基本是两块,上面是分数模块,下面是gridview
2:代码实现gridview(组合控件)
3:判断处理使gridview可以监听上下左右滑动的事件
4:实现gridview中的card类,里面就是一个textview(组合控件)
5:在gridview中添加card
6:在游戏的初始时随机添加两个数字
7:实现游戏逻辑,进行上下左右的事件处理
8:计分,实现上面分数统计的模块
9:检查游戏结束
10:优化处理,不同数字具有不同的颜色
11:添加动画效果,我的其他文章中有专门对动画进行的阐述
12:添加anctionbar继续进行优化


主activity的页面布局:activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#EAEAEE"
    tools:context=".MainActivity" >
	    
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="5" >
        
         <LinearLayout
             android:layout_marginLeft="30dp" 
             android:layout_marginTop="30dp"
             android:layout_marginBottom="30dp"
             android:layout_width="120dp"
             android:layout_height="250dp"
             android:background="@drawable/yellowrounded_half_bg">
             
             <TextView
                 android:id="@+id/now_most_score"
	             android:gravity="center"
                 android:layout_width="fill_parent"
                 android:textSize="80px"
                 android:textColor="#FFFFFF" 
                 android:textStyle="bold"
                 android:layout_height="100dp" />

	      </LinearLayout>
	      
         <LinearLayout
             android:layout_marginLeft="170dp" 
             android:layout_marginTop="5dp"
             android:layout_width="80dp"
             android:layout_height="80dp"
             android:orientation="vertical"
             android:background="@drawable/orangerounded_half_bg">
             
             <TextView
                 android:layout_width="fill_parent"
                 android:textSize="26px"
                 android:text="@string/score"
                 android:textColor="#FF6666" 
                 android:textStyle="bold"
                 android:layout_height="40dp" />
             
             <TextView
                 android:id="@+id/now_all_score"
                 android:layout_width="fill_parent"
                 android:textSize="26px"
                 android:textColor="#FFFFFF" 
                 android:textStyle="bold"
                 android:text="0"
                 android:layout_height="40dp" />

	      </LinearLayout>
	      
         <LinearLayout
             android:layout_marginLeft="265dp" 
             android:layout_marginTop="5dp"
             android:layout_width="80dp"
             android:orientation="vertical"
             android:layout_height="80dp"
             android:background="@drawable/orangerounded_half_bg">
             
             <TextView
                 android:layout_width="fill_parent"
                 android:textSize="26px"
                 android:text="@string/main_score"
                 android:textColor="#FF6666" 
                 android:textStyle="bold"
                 android:layout_height="40dp" />
             
             <TextView
                 android:id="@+id/history_most_score"
                 android:layout_width="fill_parent"
                 android:textSize="26px"
                 android:text="0"
                 android:textColor="#FFFFFF" 
                 android:textStyle="bold"
                 android:layout_height="40dp" />

	      </LinearLayout>
	      
         <ImageView 
             android:layout_marginLeft="265dp" 
             android:layout_marginTop="105dp"
             android:layout_width="80dp"
             android:layout_height="80dp"
             android:background="@drawable/sf"   />
         
         <LinearLayout
             android:layout_marginLeft="170dp" 
             android:layout_marginTop="105dp"
             android:layout_width="80dp"
             android:layout_height="80dp"
             android:orientation="vertical"
             android:background="@drawable/orangerounded_half_bg">
             
             <TextView
                 android:layout_width="fill_parent"
                 android:textSize="26px"
                 android:text="@string/author"
                 android:textColor="#FF6666" 
                 android:textStyle="bold"
                 android:layout_height="40dp" />
             
             <TextView
                 android:layout_width="fill_parent"
                 android:textSize="26px"
                 android:textColor="#FFFFFF" 
                 android:textStyle="bold"
                 android:text="@string/author_name"
                 android:layout_height="40dp" />

	      </LinearLayout>
        
    </RelativeLayout>
    
    <com.example.game2048.MyGridView
	    android:id="@+id/girdlayout"
	    android:layout_width="fill_parent"
	    android:layout_height="0dp"
	    android:layout_weight="9"  >
	</com.example.game2048.MyGridView>
	
</LinearLayout>


MainActivity.java
package com.example.game2048;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {
	
	private TextView now_most_score,now_all_score,history_most_score;
	private int most_score =  0 , all_score = 0;
	private static MainActivity mainactivity;

	public MainActivity() {
		super();
		mainactivity = this;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		now_most_score = (TextView) findViewById(R.id.now_most_score);
		now_all_score = (TextView) findViewById(R.id.now_all_score);
		history_most_score = (TextView) findViewById(R.id.history_most_score);
	}
	
	public static MainActivity getMainActivity(){
		return mainactivity;
	}
	
	public void clearScore(){
		
		most_score =  0;
		all_score = 0;
		
		showScore();
	}
	
	private void showScore(){
		SharedPreferences mySharedPreferences= getSharedPreferences("my2048", Activity.MODE_PRIVATE); 
		SharedPreferences.Editor editor = mySharedPreferences.edit(); 
		now_most_score.setText( most_score + "");
		now_all_score.setText( all_score + "");
		history_most_score.setText(mySharedPreferences.getInt("his_score", 0)+"");
		if(all_score > Integer.parseInt(history_most_score.getText().toString()) ){
			history_most_score.setText(all_score+"");
			editor.putInt("his_score", all_score); 
			//提交当前数据 
			editor.commit(); 
		}
	}
	
	public void addScore(int p,int s){
		most_score = p;
		all_score += s;
		showScore();
	}
	
}


MyGridView.java
package com.example.game2048;

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

import android.app.AlertDialog;
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 MyGridView extends GridLayout {
	
	private MyCard[][] cards = new MyCard[4][4];
	private List<Point> emptyPoints = new ArrayList<Point>();

	public MyGridView(Context context) {
		super(context);
		initGame();
	}
	
	public MyGridView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initGame();
	}
	
	public MyGridView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initGame();
	}
	
	private void initGame(){
		setColumnCount(4);
//		setBackgroundColor(0xffFAF9DE);
//		setBackground(getResources().getDrawable(R.drawable.bk_pq));
		
		setOnTouchListener(new OnTouchListener() {
			
			private float startx,starty,offerx,offery;
			
			@Override
			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:
					offerx = arg1.getX() - startx;
					offery = arg1.getY() - starty;
					if(Math.abs(offerx) > Math.abs(offery)){
						//横向偏移量大
						if(offerx > 5){
							moveright();
						}else if(offerx < -5){
							moveleft();
						}
					}else{
						//纵向偏移量大
						if(offery > 5){
							movedown();
						}else if(offery < -5){
							moveup();
						}
					}
					break;
				}
				return true;
			}
		});
	}
	
	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		
		int cardwidth = (Math.min(w, h)-10)/4;
		
		addCard(cardwidth,cardwidth);
		
		startGanme();
	}

	private void startGanme() {
		for (int y = 0; y < 4; y++) {
			for (int x = 0; x < 4; x++) {
				cards[x][y].setNum(0);
			}
		}
		
		addRandom();
		addRandom();
		showScore(0);
	}

	private void addCard(int cardWidth, int cardHeight) {
		
		MyCard c ;
		
		for (int y = 0; y < 4; y++) {
			for (int x = 0; x < 4; x++) {
				c = new MyCard(getContext());
				c.setNum(2);
				addView(c, cardWidth, cardHeight);
				cards[x][y] = c;
			}
		}
	}
	
	private void addRandom(){
		emptyPoints.clear();
		
		for (int y = 0; y < 4; y++) {
			for (int x = 0; x < 4; x++) {
				if(cards[x][y].getNum()<=0){
					emptyPoints.add(new Point(x, y));
				}
				cards[x][y].setBackGroundColor(cards[x][y].getNum());
			}
		}
		
		Point p = emptyPoints.remove((int)(Math.random()*emptyPoints.size()));
		cards[p.x][p.y].setNum(Math.random()>0.1?2:4); 
	}

	private void moveup(){
		boolean flag = false;
		for (int x = 0; x < 4; x++) {
			for (int y = 0; y < 4; y++) {
				for (int y1 = y+1; y1 < 4; y1++) {
					if(cards[x][y1].getNum() > 0){
						
						if (cards[x][y].getNum()<=0) {
							cards[x][y].setNum(cards[x][y1].getNum());
							cards[x][y1].setNum(0);
							y--;
							flag = true;
						}else if (cards[x][y].equals(cards[x][y1])){
							cards[x][y].setNum(cards[x][y].getNum()*2);
							cards[x][y1].setNum(0);
							showScore(cards[x][y].getNum());
							flag = true;
						}
						break;
					}
				}
			}
		}
		if(flag){
			addRandom();
			if(checkComplete()){
				showAgainDialog();
			}
		}
	}
	
	private void movedown(){
		boolean flag = false;
		for (int x = 0; x < 4; x++) {
			for (int y = 3; y >= 0; y--) {
				for (int y1 = y-1; y1 >= 0; y1--) {
					if(cards[x][y1].getNum() > 0){
						
						if (cards[x][y].getNum()<=0) {
							cards[x][y].setNum(cards[x][y1].getNum());
							cards[x][y1].setNum(0);
							y++;
							flag = true;
						}else if (cards[x][y].equals(cards[x][y1])){
							cards[x][y].setNum(cards[x][y].getNum()*2);
							cards[x][y1].setNum(0);
							showScore(cards[x][y].getNum());
							flag = true;
						}
						break;
					}
				}
			}
		}
		if(flag){
			addRandom();
			if(checkComplete()){
				showAgainDialog();
			}
		}
	}

	private void moveleft(){
		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(cards[x1][y].getNum() > 0){
						
						if (cards[x][y].getNum()<=0) {
							cards[x][y].setNum(cards[x1][y].getNum());
							cards[x1][y].setNum(0);
							x--;
							flag = true;
						}else if (cards[x][y].equals(cards[x1][y])){
							cards[x][y].setNum(cards[x][y].getNum()*2);
							cards[x1][y].setNum(0);
							showScore(cards[x][y].getNum());
							flag = true;
						}
						break;
					}
				}
			}
		}
		if(flag){
			addRandom();
			if(checkComplete()){
				showAgainDialog();
			}
		}
	}

	private void moveright(){
		boolean flag = false;
		for (int y = 0; y < 4; y++) {
			for (int x = 3; x >= 0 ; x--) {
				for (int x1 = x-1; x1 >= 0; x1--) {
					if(cards[x1][y].getNum() > 0){
						
						if (cards[x][y].getNum()<=0) {
							cards[x][y].setNum(cards[x1][y].getNum());
							cards[x1][y].setNum(0);
							x++;
							flag = true;
						}else if (cards[x][y].equals(cards[x1][y])){
							cards[x][y].setNum(cards[x][y].getNum()*2);
							cards[x1][y].setNum(0);
							showScore(cards[x][y].getNum());
							flag = true;
						}
						break;
					}
				}
			}
		}
		if(flag){
			addRandom();
			if(checkComplete()){
				showAgainDialog();
			}
		}
	}

	private void showAgainDialog() {
		new AlertDialog.Builder(getContext()).setTitle("Loser").setMessage("Again").setPositiveButton("Again", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				startGanme();
			}
		}).show();

	}

	private boolean checkComplete() {
		for (int y = 0; y < 4; y++) {
			for (int x = 0; x < 4 ; x++) {
				if(cards[x][y].getNum()==0 || 
					(x > 0 && cards[x-1][y].equals(cards[x][y])) || 
					(x < 3 && cards[x+1][y].equals(cards[x][y])) || 
					(y > 0 && cards[x][y-1].equals(cards[x][y])) || 
					(y < 3 && cards[x][y+1].equals(cards[x][y])) ){
					return false;
				}
			}
		}
		return true;
	}
	
	private void showScore(int s) {
		int large = 0;
		for (int y = 0; y < 4; y++) {
			for (int x = 0; x < 4 ; x++) {
				if(large < cards[x][y].getNum()){
					large = cards[x][y].getNum();
				}
			}
		}
		MainActivity.getMainActivity().addScore(large,s);
	}

}


MyCard.java
package com.example.game2048;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.TextView;

public class MyCard extends FrameLayout {
	
	private int num = 0 ;
	private TextView textview;

	public MyCard(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	public MyCard(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public MyCard(Context context) {
		super(context);
		init();
	}
	
	private void init(){
		textview = new TextView(getContext());
		textview.setTextSize(32);
		textview.setGravity(Gravity.CENTER);
		textview.setBackgroundColor(0x33ffffff);
		
		LayoutParams lp = new LayoutParams(-1, -1);
		lp.setMargins(10, 10, 0, 0);
		addView(textview, lp);
		setNum(0);
	}
	
	public int getNum(){
		return num;
	}

	public void setNum(int num){
		this.num = num;
		if(num<=0){
			textview.setText("");
		}else{
			textview.setText(num+"");
		}
	}
	
	public boolean equals(MyCard c){
		return getNum() == c.getNum();
	}
	
	public void setBackGroundColor(int num){
		switch (num) {
		case 2:
			textview.setBackgroundColor(0x33ffffff);
			break;
		case 4:
			textview.setBackgroundColor(0x33FFFFCC);
			break;
		case 8:
			textview.setBackgroundColor(0x33FF6666);
			break;
		case 16:
			textview.setBackgroundColor(0x33FF3300);
			break;
		case 32:
			textview.setBackgroundColor(0x33FF33FF);
			break;
		case 64:
			textview.setBackgroundColor(0x330033FF);
			break;
		case 128:
			textview.setBackgroundColor(0x3366FF66);
			break;
		case 256:
			textview.setBackgroundColor(0x3300CC33);
			break;
		case 512:
			textview.setBackgroundColor(0x33009933);
			break;
		case 1024:
			textview.setBackgroundColor(0x33CCCC00);
			break;
		case 2048:
			textview.setBackgroundColor(0x33CC0000);
			break;
		case 4096:
			textview.setBackgroundColor(0x33FFCC99);
			break;
		case 8192:
			textview.setBackgroundColor(0x33FFFF00);
			break;
		default:
			textview.setBackgroundColor(0x33ffffff);
			break;
		}
	}

}
分享到:
评论

相关推荐

    java算法大全源码 java算法大全源码

    java算法大全源码java算法大全源码java算法大全源码java算法大全源码java算法大全源码java算法大全源码java算法大全源码java算法大全源码java算法大全源码java算法大全源码java算法大全源码java算法大全源码java算法...

    2048游戏完整源码

    总的来说,2048游戏源码是一个很好的学习资源,它涵盖了基础的编程概念、数据结构和算法,对于初学者和经验丰富的开发者来说都是有价值的。通过研究源码,不仅可以提升编程技能,还能锻炼问题解决和逻辑思考能力。...

    遗传算法源码遗传算法源码遗传算法源码

    在标题中提到的“遗传算法源码遗传算法源码遗传算法源码”,可以理解为提供了一个关于遗传算法的编程实现,可能包含一个或多个源代码文件。这些源代码通常用C++、Python、Java等编程语言编写,用于演示遗传算法的...

    cocos2dx 2048游戏源码

    本文将深入探讨使用cocos2dx框架开发的2048游戏源码,帮助读者理解cocos2dx引擎的运用以及2048游戏的核心算法。 cocos2dx是一款开源的游戏开发框架,基于C++,它提供了丰富的2D和3D图形功能,使得开发者能够高效地...

    ios2048源码

    首先,我们来探讨2048游戏的核心算法。2048的基本玩法是通过上下左右滑动屏幕,将相同数字的方块合并,每次操作后,棋盘上会随机出现一个2或4的方块。关键在于如何设计这个“合并”算法。在iOS版2048中,开发者可能...

    2048源码(qt4)

    这个项目是开发者对网络上已有的2048游戏代码进行学习和改编的结果,确保了程序在Qt环境下能够良好运行。 2048游戏是一个数字拼图游戏,由Gabriele Cirulli于2014年创造。游戏的目标是在4x4的网格上通过滑动来合并...

    易语言源码易语言远程桌面图片核心算法源码.rar

    易语言源码易语言远程桌面图片核心算法源码.rar 易语言源码易语言远程桌面图片核心算法源码.rar 易语言源码易语言远程桌面图片核心算法源码.rar 易语言源码易语言远程桌面图片核心算法源码.rar 易语言源码易语言...

    2048html游戏源码分享

    游戏的核心算法包括: 1. **随机数生成**:JavaScript的`Math.random()`函数用于生成新数字,一般会选择2或4,根据一定的概率进行分配。 2. **棋盘操作**:当用户上下左右滑动时,JavaScript会遍历棋盘上的每个...

    2048安卓源码

    源码中包含的文件名“2048源码”可能是一个整体的源代码包,包含了游戏的主逻辑、用户界面(UI)、事件处理、游戏状态管理等核心部分。 1. **游戏逻辑**:2048的核心在于它的算法。每次滑动操作,源码会遍历所有...

    android_studio 2048源码 可直接运行

    《Android Studio实现2048游戏的源码解析》 在Android开发领域,学习和实践经典游戏源码是提升技能的有效途径。本篇将详细解析使用Android Studio开发的2048游戏源码,帮助开发者深入理解Android应用的构建过程以及...

    2048游戏源码

    2048游戏的源码通常包含以下几个主要部分: 1. **主程序**:负责游戏的初始化、事件处理(如滑动操作)以及游戏状态的管理。 2. **棋盘管理**:实现棋盘的布局、方块的移动和合并,以及新方块的生成。 3. **用户...

    Android开发2048源码

    Game2048的源码主要由以下几个部分组成: 1. **布局文件**:在`res/layout`目录下的XML文件定义了游戏界面的布局。这些布局文件描述了用户界面的组件,如按钮、图像视图和文本视图等,以及它们的排列方式。2048游戏...

    2048源码.zip

    在Android平台上实现2048,主要涉及到以下几个关键的技术点: 1. **用户界面(UI)设计**:2048的界面简洁明了,通常使用LinearLayout或RelativeLayout来构建布局。每个数字方块是一个ImageView或TextView,通过...

    H5小游戏源码 2048游戏源码.zip

    《2048游戏源码解析与H5小游戏开发概论》 2048是一款在2014年风靡全球的数字拼接益智游戏,由意大利开发者Gabriele Cirulli创作。它的核心玩法是通过上下左右滑动屏幕,使相同数字的方块合并,最终目标是达到2048这...

    Game2048_a5源码

    总的来说,Game2048_a5源码提供了一个直观的学习平台,让我们能够深入理解Android游戏开发的各个方面,包括UI设计、事件处理、核心算法实现以及性能优化。通过对这份源码的分析,开发者不仅可以掌握Android应用开发...

    2048小游戏源码

    游戏的核心算法是处理玩家的移动操作,包括方块的移动、合并以及新方块的生成。 源码中,关键的类可能包括`GameBoard`和`Tile`。`GameBoard`类负责管理整个棋盘的状态,包括方块的位置、数值以及游戏的胜负状态。它...

    农历核心算法类库(含源码),源自许剑伟先生的寿星万年历

    农历核心算法类库(含源码),源自许剑伟先生的寿星万年历(v4.11),由 Javascript 源码改写为 C# 源码,在 Visual Studio 2008 中测试通过。强烈推荐您下载一份寿星万年历的源码进行对照参考。 真正的“万年”历,...

    2048小游戏源码(java)

    2048游戏的核心算法涉及数组操作、二维空间的逻辑处理和概率计算,这些都需要深入理解编程和数据结构。 3. **源码**:源代码是程序员用编程语言编写的原始程序,对于学习者来说,通过阅读源码可以理解游戏的工作...

    android2048游戏源码

    以前做过一个2048的算法题,学了几天android,觉得可以实现个安卓版的,也就动手写了个。 包含的东西: GridLayout布局 在activity中动态添加view组件 判断用户在屏幕滑动的的方向 2048算法(参考之前用C++写的,写的...

Global site tag (gtag.js) - Google Analytics