[Unity3D + algorithm] Make A 2048 hour

Source: Internet
Author: User

2048 is another popular lightweight mobile game after FlappyBird, which is easy to play. Recently I want to leave my original company-because I want to play games, although there are not many games, but I still love developing games, so I want to go to a game company and feel a little embarrassed and grateful to my boss, may the original company grow better and better. In the words of the wolf, I will be back, haha! I am about to start a new company. I have heard that there will be a lot of pressure and there will be no end to working overtime. I am not afraid of working overtime. I am still young, working hard, and working overtime is nothing. In fact, as long as I can make something on my own, I feel that I have a sense of accomplishment. I still like to spend more time doing things. Recently, I was in a transitional period. I wrote my company's work summary, but I am not very busy. I spent more than an hour today, I thought about the 2048 algorithm and implemented it. Maybe the algorithm is not so good. I hope to criticize and discuss it!


The implementation is still rough and the main logic code is provided for your reference only. You are welcome to provide a better algorithm!

Using UnityEngine; using System. colleabel; 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 --) {// get the current element GameObject go = GameObject. find ("L" + (k + 1 ). toString () + I. toString (); print ("Current object" + go. name); UILabel I = go. getComponentInChildren <UILabel> (); // obtain the next element GameObject goNext = GameObject. find ("L" + k. toString () + I. toString (); print ("Next object" + goNext. name); UILabel INext = goNext. getComponentInChildren <UILabel> (); // compare the Code 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 =. 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 ++) {// obtain the current element GameObject go = GameObject. find ("L" + (k-1 ). toString () + I. toString (); print ("Current object" + go. name); UILabel I = go. getComponentInChildren <UILabel> (); // obtain the next element GameObject goNext = GameObject. find ("L" + k. toString () + I. toString (); print ("Next object" + goNext. name); UILabel INext = goNext. getComponentInChildren <UILabel> (); // compare the Code 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 =. 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 --) {// obtain the current element GameObject go = GameObject. find ("L" + I. toString () + (k + 1 ). toString (); print ("Current object" + go. name); UILabel I = go. getcomponentinchildh N <UILabel> (); // obtain the next element GameObject goNext = GameObject. find ("L" + I. toString () + k. toString (); print ("Next object" + goNext. name); UILabel INext = goNext. getComponentInChildren <UILabel> (); // compare the Code 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 =. 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 ++) {// obtain the current element GameObject go = GameObject. find ("L" + I. toString () + (k-1 ). toString (); print ("Current object" + go. name); UILabel I = go. getcomponentinchildh N <UILabel> (); // obtain the next element GameObject goNext = GameObject. find ("L" + I. toString () + k. toString (); print ("Next object" + goNext. name); UILabel INext = goNext. getComponentInChildren <UILabel> (); // compare the Code 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 =. 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 ;}}}

Welcome to my bib

Related Article

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.