[Unity3D+算法]一小时做个2048

发布时间:2020-07-06 07:59:41 作者:蓬莱仙羽
来源:网络 阅读:8395

2048是继FlappyBird之后另一个比较热的轻量级的手游,简单易玩。最近要离职原先的公司——因为我想做游戏,虽然玩游戏不是很多,但还是热爱开发游戏,因此就想去一家游戏公司,感觉对老板有一点愧疚和感激,愿原公司发展越来越好,用灰太狼的话讲,我还会回来的,哈哈!即将入职新公司,听说压力会很大,加班无止境,加班其实我到不怕,乘年轻,还有拼劲,加班算什么,其实只要自己能做出东西,感觉有成就感,倒还是喜欢花更多的时间去做东西,最近处于过渡期,写写之前公司的工作小结,还不是很忙,今天花了一个多小时,自己想了一下2048的算法,然后将其实现,可能算法不是那么优,还望批评交流!

效果图

[Unity3D+算法]一小时做个2048

实现的还比较粗糙,贴出主要逻辑代码,仅供参考,欢迎给出更优算法!


using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour
{
    public UILabel valueLabel;
    bool gameover = false;
    void Start()
    {
        gameover = false;
        valueLabel = GameObject.Find("ValueLabel").GetComponentInChildren<UILabel>();
        valueLabel.text = "Game Start";
        valueLabel.color = Color.green;
    }

    // Update is called once per frame
    void Update()
    {
        if (!gameover)
        {
            if (Input.GetKeyDown(KeyCode.D))
            {
                moveR();
                CreateNumber();
            }
            else if (Input.GetKeyDown(KeyCode.A))
            {
                moveL();
                CreateNumber();
            }
            else if (Input.GetKeyDown(KeyCode.W))
            {
                moveU();
                CreateNumber();
            }
            else if (Input.GetKeyDown(KeyCode.S))
            {
                moveD();
                CreateNumber();
            }
        }
    }


    void moveU()
    {
        for (int i = 1; i <= 4; i++)
        {
            bool flag = false;
            for (int j = 2; j <= 4; j++)
            {
                for (int k = j - 1; k >= 1; k--)
                {
                    //获取当前元素
                    GameObject go = GameObject.Find("L" + (k + 1).ToString() + i.ToString());
                    print("当前对象" + go.name);
                    UILabel I = go.GetComponentInChildren<UILabel>();

                    //获取下一个元素
                    GameObject goNext = GameObject.Find("L" + k.ToString() + i.ToString());
                    print("下一个对象" + goNext.name);
                    UILabel INext = goNext.GetComponentInChildren<UILabel>();

                    //比较代码
                    if (I.text != "")
                    {
                        if (INext.text == "")
                        {
                            INext.text = I.text;
                            I.text = "";
                        }
                        else if (I.text == INext.text)
                        {
                            if (!flag)
                            {
                                int a = int.Parse(INext.text) + int.Parse(I.text);
                                INext.text = a.ToString();
                                I.text = "";
                                flag = true;
                            }
                        }
                    }
                }
            }
        }
    }

    void moveD()
    {
        for (int i = 1; i <= 4; i++)
        {
            bool flag = false;
            for (int j = 3; j >= 1; j--)
            {
                for (int k = j + 1; k <= 4; k++)
                {
                    //获取当前元素
                    GameObject go = GameObject.Find("L" + (k-1).ToString() + i.ToString());
                    print("当前对象" + go.name);
                    UILabel I = go.GetComponentInChildren<UILabel>();

                    //获取下一个元素
                    GameObject goNext = GameObject.Find("L" + k.ToString() + i.ToString());
                    print("下一个对象" + goNext.name);
                    UILabel INext = goNext.GetComponentInChildren<UILabel>();

                    //比较代码
                    if (I.text != "")
                    {
                        if (INext.text == "")
                        {
                            INext.text = I.text;
                            I.text = "";
                        }
                        else if (I.text == INext.text)
                        {
                            if (!flag)
                            {
                                int a = int.Parse(INext.text) + int.Parse(I.text);
                                INext.text = a.ToString();
                                I.text = "";
                                flag = true;
                            }
                        }
                    }
                }
            }
        }
    }



    void moveL()
    {
        for (int i = 1; i <= 4; i++)
        {
            bool flag = false;
            for (int j = 2; j <= 4; j++)
            {
                
                for (int k = j - 1; k >=1 ; k--)
                {
                    //获取当前元素
                    GameObject go = GameObject.Find("L" + i.ToString() + (k + 1).ToString());
                    print("当前对象" + go.name);
                    UILabel I = go.GetComponentInChildren<UILabel>();

                    //获取下一个元素
                    GameObject goNext = GameObject.Find("L" + i.ToString() + k.ToString());
                    print("下一个对象" + goNext.name);
                    UILabel INext = goNext.GetComponentInChildren<UILabel>();

                    //比较代码
                    if (I.text != "")
                    {
                        if (INext.text == "")
                        {
                            INext.text = I.text;
                            I.text = "";
                        }
                        else if (I.text == INext.text)
                        {
                            if (!flag)
                            {
                                int a = int.Parse(INext.text) + int.Parse(I.text);
                                INext.text = a.ToString();
                                I.text = "";
                                flag = true;
                            }
                        }
                    }
                }
            }
        }
    }

    void moveR()
    {
        for (int i = 1; i <= 4; i++)
        {
            bool flag = false;
            for (int j = 3; j >= 1; j--)
            {
                
                for (int k = j + 1; k <= 4; k++)
                {
                    //获取当前元素
                    GameObject go = GameObject.Find("L" + i.ToString() + (k - 1).ToString());
                    print("当前对象" + go.name);
                    UILabel I = go.GetComponentInChildren<UILabel>();

                    //获取下一个元素
                    GameObject goNext = GameObject.Find("L" + i.ToString() + k.ToString());
                    print("下一个对象" + goNext.name);
                    UILabel INext = goNext.GetComponentInChildren<UILabel>();

                    //比较代码
                    if (I.text != "")
                    {
                        if (INext.text == "")
                        {
                            INext.text = I.text;
                            I.text = "";
                        }
                        else if (I.text == INext.text)
                        {
                            if (!flag)
                            {
                                int a = int.Parse(INext.text) + int.Parse(I.text);
                                INext.text = a.ToString();
                                I.text = "";
                                flag = true;
                            }
                        }
                    }
                }
            }
        }
    }



    void CreateNumber()
    {
        int count = 0;
        bool flag = false;
        for (int i = 1; i <= 4; i++)
        {
            if (!flag)
            {
                for (int j = 1; j <= 4; j++)
                {
                    GameObject go = GameObject.Find("L" + i.ToString() + j.ToString());
                    UILabel label = go.GetComponentInChildren<UILabel>();
                    if (label.text == "")
                    {
                        int r = Random.Range(1, 10);
                        if (r <= 5)
                        {
                            int value = Random.Range(1, 5);
                            if (value < 4)
                                label.text = "2";
                            else
                                label.text = "4";
                            flag = true;
                            break;
                        }
                    }
                    else
                    {
                        if (++count == 16)
                        {
                            valueLabel.text = "Game Over";
                            valueLabel.color = Color.red;
                            gameover = true;
                        }
                    }
                }
            }
            else
                break;
        }
    }


}

欢迎关注我的微博

推荐阅读:
  1. Unity3D的脚本编辑器有什么组件
  2. 在Unity3d下如何使用WebSocket

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

unity3d 2048 做个

上一篇:改进Android语音对讲系统的方法

下一篇:web安全测试必须注意的五个方面

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》