您的位置:首页 > WinForm >

c# .net 2.0 winform 实现2048算法

2014-07-25 10:39 来源:未知编辑:admin点击:


这游戏前一段时间传的很火,前几天早上上课实在太无聊了,就决定把这游戏自己也写一个。

前后写了一个多小时吧,大概300行左右,没什么复杂算法,不过实在懒得去优化了,但估计优化完能控制在200行以下,有兴趣的朋友可以自己优化一下。


简单玩了2下没有玩到2048等玩到上图给大家


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace WinForm2048
{
    public partial class M2048 : Form
    {
        private int[,] fk = new int[4, 4];
        private int[] sjs = { 2, 4, 2, 2, 2, 2, 4, 2, 2, 2 };
        private bool moved = false;
        public M2048()
        {
            InitializeComponent();
            initsj();
            this.SetStyle(ControlStyles.UserPaint | ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
            this.UpdateStyles();
        }

        private void initsj()
        {
            for (int i = 0; i < 4; i++)
                for (int j = 0; j < 4; j++)
                    fk[i, j] = 0;
            //throw new NotImplementedException();
            int sj1, sj2;
            Random r = new Random();
            sj1 = r.Next(0, 9);
            Thread.Sleep(1);
            sj2 = r.Next(0, 9);
            Thread.Sleep(1);
            int t1 = r.Next(0, 15);
            fk[t1 / 4, t1 % 4] = sjs[sj1];
            while (true)
            {
                Thread.Sleep(1);
                int t2 = r.Next(0, 15);
                if (t1 != t2)
                {
                    t1 = t2;
                    break;
                }
            }
            fk[t1 / 4, t1 % 4] = sjs[sj2];

        }
        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            initKuang(e.Graphics);
            initShuJu(e.Graphics);
        }

        private void initShuJu(Graphics g)
        {
            for (int i = 0; i < 4; i++)
                for (int j = 0; j < 4; j++)
                {
                    SolidBrush sb = new SolidBrush(Color.AntiqueWhite);
                    Point p = new Point(i * 65 + 5 + 12, j * 65 + 5 + 91);
                    Size s = new Size(59, 59);
                    Rectangle rect = new Rectangle(p, s);
                    g.FillRectangle(sb, rect);
                    if (fk[i, j] != 0)
                    {
                        Font f = new Font("宋体", 16, FontStyle.Bold);

                        g.DrawString(fk[i, j].ToString(), f, new SolidBrush(Color.Red), rect);
                    }
                }
        }

        private void initKuang(Graphics g)
        {
            //throw new NotImplementedException();
            for (int i = 0; i < 5; i++)
            {
                Pen p = new Pen(Color.DarkCyan, 6);
                Point p1 = new Point(0 + 12, i * 65 + 3 + 91);
                Point p2 = new Point(260 + 3 + 12, i * 65 + 3 + 91);
                g.DrawLine(p, p1, p2);
                Point p3 = new Point(i * 65 + 3 + 12, 0 + 91);
                Point p4 = new Point(i * 65 + 3 + 12, 260 + 3 + 91);
                g.DrawLine(p, p3, p4);
            }
        }
        private void MoveUP()
        {
            for (int i = 1; i <4; i++)
                for (int j = 0; j < 4; j++)
                {
                    if (fk[j, i] != 0)
                    {
                        for (int k = i - 1; k >=0; k--)
                        {
                            if (fk[j, k] != 0)
                            {
                                if (fk[j, k] == fk[j, i])
                                {
                                    fk[j, k] = fk[j, k] + fk[j, i];
                                    fk[j, i] = 0;
                                    moved = true;
                                    lbl_df.Text = (int.Parse(lbl_df.Text) + fk[j, k]).ToString();
                                    break;
                                }
                                else
                                {
                                    if (k != i - 1)
                                    {
                                        fk[j, k + 1] = fk[j, i];// +fk[k, j];
                                        fk[j, i] = 0;
                                        moved = true;
                                    }
                                    break;
                                }
                            }
                            else
                            {
                                if (k == 0)
                                {
                                    fk[j, k] = fk[j, i];// +fk[k, j];
                                    fk[j, i] = 0;
                                    moved = true;
                                    break;
                                }
                            }
                        }
                    }
                }
        }
        private void MoveDown()
        {
            for (int i = 2; i >= 0; i--)
                for (int j = 0; j < 4; j++)
                {
                    if (fk[j, i] != 0)
                    {
                        for (int k = i + 1; k < 4; k++)
                        {
                            if (fk[j, k] != 0)
                            {
                                if (fk[j, k] == fk[j, i])
                                {
                                    fk[j, k] = fk[j, k] + fk[j, i];
                                    fk[j, i] = 0;
                                    moved = true;
                                    lbl_df.Text = (int.Parse(lbl_df.Text) + fk[j, k]).ToString();
                                    break;
                                }
                                else
                                {
                                    if (k != i + 1)
                                    {
                                        fk[j, k-1] = fk[j, i];// +fk[k, j];
                                        fk[j, i] = 0;
                                        moved = true;
                                    }
                                    break;
                                }
                            }
                            else
                            {
                                if (k == 3)
                                {
                                    fk[j, k] = fk[j, i];// +fk[k, j];
                                    fk[j, i] = 0;
                                    moved = true;
                                    break;
                                }
                            }
                        }
                    }
                }
        }
        private void MoveLeft()
        {
            for (int i = 1; i < 4; i++)
                for (int j = 0; j < 4; j++)
                {
                    if (fk[i, j] != 0)
                    {
                        for (int k = i - 1; k >= 0; k--)
                        {
                            if (fk[k, j] != 0)
                            {
                                if (fk[k, j] == fk[i, j])
                                {
                                    fk[k, j] = fk[k, j] + fk[k, j];
                                    fk[i, j] = 0;
                                    moved = true;
                                    lbl_df.Text = (int.Parse(lbl_df.Text) + fk[k, j]).ToString();
                                    break;
                                }
                                else
                                {
                                    if (k != i - 1)
                                    {
                                        fk[k + 1, j] = fk[i, j];// +fk[k, j];
                                        fk[i, j] = 0;
                                        moved = true;
                                    }
                                    break;
                                }
                            }
                            else
                            {
                                if (k == 0)
                                {
                                    fk[k, j] = fk[i, j];// +fk[k, j];
                                    fk[i, j] = 0;
                                    moved = true;
                                    break;
                                }
                            }
                        }
                    }
                }
        }
        private void MoveRight()
        {
            for (int i = 2; i >= 0; i--)
                for (int j = 0; j < 4; j++)
                {
                    if (fk[i, j] != 0)
                    {
                        for (int k = i + 1; k < 4; k++)
                        {
                            if (fk[k, j] != 0)
                            {
                                if (fk[k, j] == fk[i, j])
                                {
                                    fk[k, j] = fk[k, j] + fk[k, j];
                                    fk[i, j] = 0;
                                    moved = true;
                                    lbl_df.Text = (int.Parse(lbl_df.Text) + fk[k, j]).ToString();
                                    break;
                                }
                                else
                                {
                                    if (k != i + 1)
                                    {
                                        fk[k-1, j] = fk[i, j];// +fk[k, j];
                                        fk[i, j] = 0;
                                        moved = true;
                                    }
                                    break;
                                }
                            }
                            else
                            {
                                if (k == 3)
                                {
                                    fk[k, j] = fk[i, j];// +fk[k, j];
                                    fk[i, j] = 0;
                                    moved = true;
                                    break;
                                }
                            }
                        }
                    }
                }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            initsj();
            //panel1.Invalidate();
            this.Invalidate();
            lbl_df.Text = lbl_cs.Text = "0";
        }



        private void M2048_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Up:
                    MoveUP();
                    break;
                case Keys.Down:
                    MoveDown();
                    break;
                case Keys.Left:
                    MoveLeft();
                    break;
                case Keys.Right:
                    MoveRight();
                    break;
            }
            if (moved)
            {
                moved = false;
                Random r = new Random();
                int ns = sjs[r.Next(0, 9)];
                Thread.Sleep(1);
                int canw = 0;
                for (int i = 0; i < 16; i++)
                {
                    if (fk[i / 4, i % 4] == 0)
                        canw++;
                }
                int s = r.Next(0, canw - 1);
                canw = 0;
                for (int i = 0; i < 16; i++)
                {
                    if (fk[i / 4, i % 4] == 0)
                    {
                        if (canw == s)
                        {
                            fk[i / 4, i % 4] = ns;
                            break;
                        }
                        canw++;
                    }
                }
                lbl_cs.Text = (int.Parse(lbl_cs.Text) + 1).ToString();
            }
            Invalidate();
        }
    }
}



论坛下载地址:c# .net 2.0 winform 实现2048算法