自学C#时写的一款《捡麻将》游戏

游戏说明:

一、游戏目标:
消除所有的麻将牌。
二、游戏规则:
(一)消除麻将:
点击选择一张麻将牌,再单击另外一张相邻的同字牌,就可以消除。
(二)移动:
1、选择一张麻将牌,单击准备移动到的位置,即可移动。
2、麻将牌只能在同行或同列上移动,不能行列同时改变。
3、有足够的空位才能移动(不能移动到有牌的地方)。
4、被移动牌在移动方向上的相邻牌同时跟随移动。
5、每次移动后,必须且只能消除选择移动的那张牌。
三、游戏技巧:牌断开的点越少越好

**

游戏效果演示:

**请添加图片描述
**

再现流程:

先说明一下,本人自学C#写的这个游戏,代码不够精练、算法不是最佳是必然的,当时也没怎么写注释,敬请谅解。
**
1、使用新建一个项目,取名“MaJiang”(建议VS2010及以上版本)
在这里插入图片描述

2、在右侧解决方案管理器中,单击选择Form1.designer.cs文件,用下面提供的Form1.designer.cs代码覆盖左侧窗口中原有代码
在这里插入图片描述
3、建立两个类:OneMJ.cs(一张牌)、AllMJ.cs(所有牌),具体方法是:

(1)在解决方案资源管理器中的项目名上,右键,添加,新建项
在这里插入图片描述
(2)在弹出的添加新项窗口中,选择“类”,在“名称”文本框中输入类名
在这里插入图片描述
(3)确定后,将下面提供的两个类的代码复制对应的类中,保存。

4、现在可以尝试运行了!

游戏代码如下:(C#)

1、OneMJ.CS(一张麻将牌类)

using System;
using System.Collections.Generic;
using System.Text;

namespace Majiang
{
   
    class OneMJ
    {
   
        private System.Windows.Forms.ImageList imgList;
        private string _Name;

        public string Name
        {
   
            get {
    return _Name; }
            set {
   
                btnMJ.Name = value;
                btnMJ.Image = imgList.Images[value + ".jpg"];
                _Name = value; }
        }

        private int _Row;

        public int Row
        {
   
            get {
   
                _Row = Index / 17;
                return _Row; }
        }
        private int _Col;

        public int Col
        {
   
            get {
   
                _Col = Index % 17;
                return _Col; }
        }
        private int _index;

        public int Index
        {
   
            get {
    return _index; }
            set {
   
                btnMJ.TabIndex = value;
                btnMJ.TabIndex = value;
                btnMJ.Left = (value % 17) * 53 + 20;
                btnMJ.Top = (value / 17) * 68 + 20;
                btnMJ.Width = 53;
                btnMJ.Height = 68;
                btnMJ.BackColor = System.Drawing.Color.Gray;
                
                picDot.Width = 14;
                picDot.Height = 14; 
                picDot.BackColor = System.Drawing.Color.DarkGray;
                picDot.Top = btnMJ.Top+2;
                picDot.Left = btnMJ.Left+2;
                picDot.SizeMode =System.Windows.Forms.PictureBoxSizeMode.StretchImage;
                picDot.Visible = false;
                // oneMJ.picDot.Visible = false;

                _index = value; }
        }

        private bool _Selected;

        public bool Selected
        {
   
            get {
    return _Selected; }
            set {
   
                if (value == true)
                {
   
                    btnMJ.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
                    btnMJ.Top += 3;
                    btnMJ.Left += 3;
                }
                else
                {
   
                    btnMJ.Top -= 3;
                    btnMJ.Left -= 3;
                    btnMJ.FlatStyle = System.Windows.Forms.FlatStyle.Standard;
                }
                _Selected = value; }
        }


        
        public System.Windows.Forms.Button btnMJ = new System.Windows.Forms.Button();
        public System.Windows.Forms.PictureBox picDot = new System.Windows.Forms.PictureBox();
        public OneMJ(System.Windows.Forms.ImageList imglist)
        {
   
            imgList = imglist;
            picDot.Image = imglist.Images["DOT1.gif"];
        }
         public void Clear()
        {
   
            Name = "";

            picDot.Visible = false;
        }
    }
}

2、AllMJ.cs(所有麻将牌类)

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Majiang
{
   
    //声明一个事件委托
    public delegate void MjBackupEvent(bool redoEnabled);
    static class MoveDirection
    {
   
        public const int Up = 0;
        public const int Down = 1;
        public const int Left = 2;
        public const int Right = 3;
    }
    public class OneStep
    {
   
        public string[] MJlist = new string[136];
        public int LastMovedMJ = -1;
        public int CountClear = 0;
    }
    class AllMJ
    {
   
        public event MjBackupEvent mjBackupEvent;
        public System.Windows.Forms.Form frm = new System.Windows.Forms.Form();
        public int CountClear = 0;
        public int LastMoved = -1;
        public DateTime baginTime;
        const string mjp = "一万一万二万二万三万三万四万四万五万五万六万六万七万七万八万八万九万九万一饼一饼二饼二饼三饼三饼四饼四饼五饼五饼六饼六饼七饼七饼八饼八饼九饼九饼一条一条二条二条三条三条四条四条五条五条六条六条七条七条八条八条九条九条红中红中发财发财白板白板东风东风南风南风西风西风北风北风";
        public List<OneMJ> MjList = new List<OneMJ>();
        public List<OneStep> MJBackUp = new List<OneStep>();
        public AllMJ(System.Windows.Forms.Form frm, System.Windows.Forms.ImageList imglist)
        {
   

            for (int row = 0; row <= 7; row++)
            {
   
                for (int col = 0; col <= 16; col++)
                {
   
                    OneMJ oneMJ = new OneMJ(imglist);
                    int index= row * 17 + col;
                    oneMJ.Index= index;
                    //oneMJ.Name= mj.Substring((row * 17 + col) * 2, 2);
                    MjList.Add(oneMJ);
                    frm.Controls.Add(oneMJ.picDot);
                    frm.Controls.Add(oneMJ.btnMJ);

                }
            }
            string easySetupText = ((System.Windows.Forms.ComboBox)frm.Controls["comboEasy"]).Text.ToString();
            Fresh(easySetupText);
        }

        public void SaveAsFile()
        {
   
            string s="";
            foreach (string one in MJBackUp[0].MJlist)
            {
   
                s += one + ',';
            }
            string path = this.GetType().Assembly.Location;
            FileStream fs = new FileStream(path, FileMode.CreateNew);
            StreamWriter sw = new StreamWriter(fs);
            //开始写入
            sw.Write(s);
            //清空缓冲区
            sw.Flush();
            //关闭流
            sw.Close();
            fs.Close();
        
        }

        public void Backup()
        {
   
            MJBackUp.Add(new OneStep() );
            int c = MJBackUp.Count - 1;
            int i=0;
            foreach (OneMJ omj in MjList)
            {
   
                MJBackUp[c].MJlist[i]= omj.Name;
                i++;
            }
            MJBackUp[c].LastMovedMJ = LastMoved;
            MJBackUp[c].CountClear = CountClear;
            mjBackupEvent(true);
        }
        public void GOBack()
        {
   
            int c = MJBackUp.Count - 1;
            if (c >= 0)
            {
   
                int i = 0;
                foreach (OneMJ omj in MjList)
                {
   
                    omj.Name = MJBackUp[c].MJlist[i];
                    i++;
                }
                LastMoved = MJBackUp[c].LastMovedMJ;
                CountClear = MJBackUp[c].CountClear;
                MJBackUp.RemoveAt(c);
            }
        }
        public bool ClearBoth(OneMJ FirstClick, OneMJ SecondClick)
        {
   
            Backup();
            LastMoved = -1;
            FirstClick.Clear();
            SecondClick.Clear();
            ClearFlag();
            CountClear += 2;
            if (CountClear==136)
            {
   
                
                TimeSpan df = DateTime.Now - baginTime;
                System.Windows.Forms.MessageBox.Show("恭喜过关!,用时" + df.Minutes.ToString() + "分" + (df.Seconds % 60).ToString() + "秒");
                //Fresh();
            }
            return true;
        }
        public void Fresh(string limitEasyText)
        {
   
            string mj 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值