IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> 2048小游戏 -> 正文阅读

[C++知识库]2048小游戏

2048小游戏

界面

主要控件有

        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.Timer timer_load;
        private System.Windows.Forms.Timer timer_gameover;
        private System.Windows.Forms.MenuStrip menuStrip1;
        private System.Windows.Forms.ToolStripMenuItem 游戏ToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 新游戏ToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 信息统计ToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 选项ToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 退出ToolStripMenuItem;

运行时界面:
运行界面

代码

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

namespace _2048
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.CenterToScreen();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            this.timer_load.Interval = 100;
            this.timer_gameover.Interval = 100;
            timer_load_Tick(null, null);
        }
        private void timer_load_Tick(object sender, EventArgs e)
        {
            this.timer_load.Stop();
            this.game.Init(4);
            this.game.Generate();
            CreateLabel();
            ShowMap();
        }
        private void timer_gameover_Tick(object sender, EventArgs e)
        {
            this.timer_gameover.Stop();
            MessageBox.Show("game over");
        }

        private void 新游戏ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.timer_load.Start();
        }
        private void 信息统计ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
        private void 选项ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (this.game.IsGameOver()) return;
            if (Keys.Up == e.KeyCode)
            {
                this.game.Operate(CGame.eOpt.Up);
            }
            else if (Keys.Down == e.KeyCode)
            {
                this.game.Operate(CGame.eOpt.Down);
            }
            else if (Keys.Left == e.KeyCode)
            {
                this.game.Operate(CGame.eOpt.Left);
            }
            else if (Keys.Right == e.KeyCode)
            {
                this.game.Operate(CGame.eOpt.Right);
            }
            this.game.Generate();
            ShowMap();
            if (this.game.IsGameOver())
            {
                this.timer_gameover.Start();
            }
        }

        public void CreateLabel()
        {
            int w = this.game.GetSizeWidth();
            int h = this.game.GetSizeHeight();
            if (0 == w || 0 == h) return;
            int cubeSize = Math.Min(this.panel1.Size.Width / w, this.panel1.Size.Height / h);
            Point pt = new Point();
            Font ft = null;
            Size sz = new Size();
            sz.Width = cubeSize;
            sz.Height = cubeSize;

            if (0 < w)
            {
                pt.Y = 0;

                for (int i = 0; i < w; ++i)
                {
                    pt.X = h * cubeSize;
                    for (int j = 0; j < h; ++j)
                    {
                        Label labelCube = new Label();
                        labelCube.Name = "label_" + i.ToString() + "_" + j.ToString();
                        labelCube.Tag = i.ToString() + "," + j.ToString();
                        labelCube.BorderStyle = BorderStyle.FixedSingle;
                        labelCube.TextAlign = ContentAlignment.MiddleCenter;
                        //labelCube.Click += new System.EventHandler(this.label_Click);
                        pt.X = j * cubeSize;
                        pt.Y = i * cubeSize;
                        labelCube.Location = pt;
                        labelCube.Size = sz;
                        if (null == ft)
                        {
                            ft = new Font(labelCube.Font.Name, 14, labelCube.Font.Style);
                        }
                        labelCube.Font = ft;
                        labelCube.BackColor = SystemColors.ActiveCaption;
//                         if (0 == i % 2)
//                         {
//                             labelCube.BackColor = Color.Red;
//                         }
//                         else
//                         {
//                             labelCube.BackColor = Color.Green;
//                         }
//                         labelCube.Text = "2";
                        this.panel1.Controls.Add(labelCube);
                    }
                    pt.Y += cubeSize;
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public void ShowMap()
        {
            int r = this.game.GetSizeWidth();
            int c = this.game.GetSizeHeight();
            for (int i = 0; i < r; ++i)
            {
                for (int j = 0; j < c; ++j)
                {
                    UpdateCube(i, j);
                }
            }
        }
        public void UpdateCube(int row, int col)
        {
            if (null == game) return;
            int count = this.panel1.Controls.Count;
            if (row >= this.game.GetSizeWidth() || col >= this.game.GetSizeHeight()) return;
            string name = "label_" + row.ToString() + "_" + col.ToString();
            object obj = this.panel1.Controls.Find(name, false).First();
            if (null != obj)
            {
                Label lab = obj as Label;
                ShowCube(lab, this.game.GetCube(row, col));
            }
        }
        public void ShowCube(Label labelCube, CCube c)
        {
            if (null == labelCube) return;
            if (null == c) return;
            labelCube.Text = (0 == c.GetValue()) ? "" : (Math.Pow(2, c.GetValue())).ToString();
            switch (c.GetValue())
            {
                case 1: labelCube.ForeColor = Color.Blue; break;
                case 2: labelCube.ForeColor = Color.Green; break;
                case 3: labelCube.ForeColor = Color.OrangeRed; break;
                case 4: labelCube.ForeColor = Color.DarkBlue; break;
                case 5: labelCube.ForeColor = Color.DarkOliveGreen; break;
                case 6: labelCube.ForeColor = Color.BlueViolet; break;
                case 7: labelCube.ForeColor = Color.DarkOrange; break;
                case 8: labelCube.ForeColor = Color.PaleVioletRed; break;
                case 9: labelCube.ForeColor = Color.Brown; break;
                case 10: labelCube.ForeColor = Color.DeepPink; break;
                case 11: labelCube.ForeColor = Color.Crimson; break;
                case 12: labelCube.ForeColor = Color.DarkOrchid; break;
                case 13: labelCube.ForeColor = Color.DarkRed; break;
                case 14: labelCube.ForeColor = Color.MediumBlue; break;
                case 15: labelCube.ForeColor = Color.DarkViolet; break;
                case 16: labelCube.ForeColor = Color.Gold; break;
                default: labelCube.ForeColor = Color.Black; break;
            }
        }

        //
        private CGame game = new CGame();
    }

    public partial class CCube
    {
        private int level;

        public CCube()
        {
            this.level = 0;
        }
        public void Clear()
        {
            this.level = 0;
        }
        public int GetValue()
        {
            return this.level;
        }
        public void SetValue(int value)
        {
            if (0 > value) return;
            this.level = value;
        }
        public void swap(ref CCube rhs)
        {
            int value = this.level;
            this.level = rhs.level;
            rhs.level = value;
        }
    }

    public partial class CGame
    {
        public enum eOpt
        {
            Left, 
            Right, 
            Up, 
            Down, 
        }

        public CGame()
        {
            Clear();
        }

        public void Init(int size)
        {
            Init(size, size);
        }
        public void Init(int row, int col)
        {
            if (null == this.map
                || row != this.map.GetLength(0) || col != this.map.GetLength(1))
            {
                this.map = new CCube[row, col];
                this.row = row;
                this.col = col;
            }
            for (int i = 0; i < row; ++i)
            {
                for (int j = 0; j < col; ++j)
                {
                    this.map[i, j] = new CCube();
                }
            }
            Clear();
            this.isGameOver = false;
//             // ---test
//             for (int i = 0; i < row; ++i)
//             {
//                 //if (0 == i % 2) continue;
//                 for (int j = 0; j < col; ++j)
//                 {
//                     this.map[i, j].SetValue(1);
//                 }
//             }
//             // ---test
        }
        public int GetSizeWidth() { return this.row; }
        public int GetSizeHeight() { return this.col; }
        public void Clear()
        {
            if (null == this.map) return;
            int r = this.map.GetLength(0);
            int c = this.map.GetLength(1);
            for (int i = 0; i < r; ++i)
            {
                for (int j = 0; j < c; ++j)
                {
                    this.map[i, j].Clear();
                }
            }
            this.isGameOver = true;
        }
        public bool IsGameOver() { return this.isGameOver; }
        public CCube GetCube(int row, int col)
        {
            if (0 > row || row >= this.row) return null;
            if (0 > col || col >= this.col) return null;
            if (null == this.map) return null;
            return map[row, col];
        }
        private void SetValue(int row, int col, int value)
        {
            if (0 > row || row >= this.row) return;
            if (0 > col || col >= this.col) return;
            if (null == this.map) return;
            this.map[row, col].SetValue(value);
        }

        /// <summary>
        /// 随机生产块
        /// </summary>
        public void Generate()
        {
            Generate(2);
        }
        /// <summary>
        /// 随机生产count个块
        /// </summary>
        /// <param name="count">个数</param>
        public void Generate(int count)
        {
            if (null == this.map) return;
            Random rd = new Random();
            int _count = 0;
            for (int k = 0; k < count; ++k)
            {
                int r = rd.Next(this.row);
                int c = rd.Next(this.col);
                bool bFound = false;
                for (int i = 0; i < this.row; ++i)
                {
                    for (int j = 0; j < this.col; ++j)
                    {
                        int _r = (r + i) % this.row;
                        int _c = (c + j) % this.col;
                        if (0 == this.GetCube(_r, _c).GetValue())
                        {
                            bFound = true;
                            ++_count;
                            SetValue(_r, _c, 1);
                            break;
                        }
                    }
                    if (bFound)
                    {
                        break;
                    }
                }
            }
            //if (_count < count)
            if (0 >= _count)
            {
                this.isGameOver = true;
            }
        }
        public void Operate(eOpt opt)
        {
            switch (opt)
            {
                case eOpt.Up:
                    {
                        OperateUp();
                    }
                    break;
                case eOpt.Down:
                    {
                        OperateDown();
                    }
                    break;
                case eOpt.Left:
                    {
                        OperateLeft();
                    }
                    break;
                case eOpt.Right:
                    {
                        OperateRight();
                    }
                    break;
                default:
                    break;
            }
            foreach (CCube c in this.map)
            {
                if (11 == c.GetValue())
                {
                    this.isGameOver = true;
                    break;
                }
            }
        }
        private void OperateUp()
        {
            for (int j = 0; j < this.col; ++j)
            {
                for (int i = 1; i < this.row; ++i)
                {
                    DealCube_Up(i, j);
                }
            }
        }
        private void OperateDown()
        {
            for (int j = 0; j < this.col; ++j)
            {
                for (int i = this.row - 2; 0 <= i; --i)
                {
                    DealCube_Down(i, j);
                }
            }
        }
        private void OperateLeft()
        {
            for (int i = 0; i < this.row; ++i)
            {
                for (int j = 1; j < this.col; ++j)
                {
                    DealCube_Left(i, j);
                }
            }
        }
        private void OperateRight()
        {
            for (int i = 0; i < this.row; ++i)
            {
                for (int j = this.col - 1; 0 <= j; --j)
                {
                    DealCube_Right(i, j);
                }
            }
        }
        private void DealCube_Up(int row, int col)
        {
            if (0 != this.map[row, col].GetValue())
            {
                int index = row - 1;
                if (0 <= index)
                {
                    if (this.map[row, col].GetValue() == this.map[index, col].GetValue())
                    {// 一样,合并
                        this.map[index, col].SetValue(this.map[index, col].GetValue() + 1);
                        this.map[row, col].Clear();
                    }
                    else if (0 == this.map[index, col].GetValue())
                    {// 空,交换
                        this.map[row, col].swap(ref this.map[index, col]);
                        DealCube_Up(index, col);
                    }
                }
            }
        }
        private void DealCube_Down(int row, int col)
        {
            if (0 != this.map[row, col].GetValue())
            {
                int index = row + 1;
                if (this.row > index)
                {
                    if (this.map[row, col].GetValue() == this.map[index, col].GetValue())
                    {// 一样,合并
                        this.map[index, col].SetValue(this.map[index, col].GetValue() + 1);
                        this.map[row, col].Clear();
                    }
                    else if (0 == this.map[index, col].GetValue())
                    {// 空,交换
                        this.map[row, col].swap(ref this.map[index, col]);
                        DealCube_Down(index, col);
                    }
                }
            }
        }
        private void DealCube_Left(int row, int col)
        {
            if (0 != this.map[row, col].GetValue())
            {
                int index = col - 1;
                if (0 <= index)
                {
                    if (this.map[row, col].GetValue() == this.map[row, index].GetValue())
                    {// 一样,合并
                        this.map[row, index].SetValue(this.map[row, index].GetValue() + 1);
                        this.map[row, col].Clear();
                    }
                    else if (0 == this.map[row, index].GetValue())
                    {// 空,交换
                        this.map[row, col].swap(ref this.map[row, index]);
                        DealCube_Left(row, index);
                    }
                }
            }
        }
        private void DealCube_Right(int row, int col)
        {
            if (0 != this.map[row, col].GetValue())
            {
                int index = col + 1;
                if (this.col > index)
                {
                    if (this.map[row, col].GetValue() == this.map[row, index].GetValue())
                    {// 一样,合并
                        this.map[row, index].SetValue(this.map[row, index].GetValue() + 1);
                        this.map[row, col].Clear();
                    }
                    else if (0 == this.map[row, index].GetValue())
                    {// 空,交换
                        this.map[row, col].swap(ref this.map[row, index]);
                        DealCube_Right(row, index);
                    }
                }
            }
        }


        /// <summary>
        /// 是否结束
        /// </summary>
        private bool isGameOver = true;
        /// <summary>
        /// 行数
        /// </summary>
        private int row;
        /// <summary>
        /// 列数
        /// </summary>
        private int col;
        /// <summary>
        /// 地图数据
        /// </summary>
        private CCube[,] map = null;
    }
}

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-09-03 11:43:10  更:2021-09-03 11:46:09 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/23 20:50:20-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码