赶来赶去还是把作业赶完了。 每周都这么忙, 以后可如何是好(T - T)

不吐槽了,我觉得写游戏还是挺有意思的~

作业要求:

1. 3种难度的N数码游戏;

2. 记录步数和耗时;

3. 能自动完成。

下载游戏>>

上图:

代码:

NDigittal.cs

    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 NDigital
    {
        public partial class Form1 : Form
        {
            public System.Windows.Forms.Button[,] Btn = new Button[ClassCommon.MaxN, ClassCommon.MaxN];
            public Form1()
            {
                InitializeComponent();
            }
            private void GetStep(int sx, int sy, int ex, int ey)
            {
                Btn[sx, sy].Enabled = true;
                Btn[ex, ey].Enabled = false;
                Btn[sx, sy].Text = Btn[ex, ey].Text;
                Btn[ex, ey].Text = "";
            }
            private void RandomStep()
            {
                Random rd = new Random();
                for (int i = 0; i < ClassCommon.RandomN; ++i)
                {
                    int tx, ty;
                    do
                    {
                        int k = rd.Next() % 4;
                        tx = ClassCommon.NowX + ClassCommon.dir[k, 0];
                        ty = ClassCommon.NowY + ClassCommon.dir[k, 1];
                    } while (tx < 0 || ty < 0 || tx >= ClassCommon.N || ty >= ClassCommon.N);
                    GetStep(ClassCommon.NowX, ClassCommon.NowY, tx, ty);
                    ClassCommon.F[i].x = ClassCommon.NowX;
                    ClassCommon.F[i].y = ClassCommon.NowY;
                    ClassCommon.NowX = tx;
                    ClassCommon.NowY = ty;
                }
            }
            private void PutButton()
            {
                for (int i = 0; i < ClassCommon.N; ++i)
                    for (int j = 0; j < ClassCommon.N; ++j)
                        Btn[i, j].Font = new System.Drawing.Font("微软雅黑", 30F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                for (int i = 0; i < ClassCommon.N; ++i)
                    for (int j = 0; j < ClassCommon.N; ++j)
                        Btn[j, i].Text = Convert.ToString(i * ClassCommon.N + j + 1);
                Btn[ClassCommon.N - 1, ClassCommon.N - 1].Text = "";
                for (int i = 0; i < ClassCommon.N; ++i)
                    for (int j = 0; j < ClassCommon.N; ++j)
                        Btn[i, j].Enabled = true;
                Btn[ClassCommon.N - 1, ClassCommon.N - 1].Enabled = false;
                RandomStep();
            }
            private void NewGame()
            {
                timer1.Start();
                ClassCommon.Time = 0;
                ClassCommon.Step = 0;
                ClassCommon.NowX = ClassCommon.N - 1;
                ClassCommon.NowY = ClassCommon.N - 1;
                toolStripStatusLabel2.Text = "耗时:0 秒";
                toolStripStatusLabel1.Text = "已走:0 步  ";
                ClassCommon.RandomN = ClassCommon.N * ClassCommon.N * ClassCommon.N;
                ClassCommon.GameN = 0;
                panel1.Controls.Clear();
                panel1.Location = new System.Drawing.Point(2, 菜单ToolStripMenuItem.Height + 4);
                this.Size = new System.Drawing.Size(ClassCommon.L * ClassCommon.N + 13, ClassCommon.L * ClassCommon.N + 菜单ToolStripMenuItem.Size.Height + statusStrip1.Size.Height + 40);
                for (int i = 0; i < ClassCommon.N; ++i)
                    for (int j = 0; j < ClassCommon.N; ++j)
                    {
                        Btn[i, j] = new Button();
                        Btn[i, j].Location = new System.Drawing.Point(panel1.Location.X + i * ClassCommon.L, panel1.Location.Y + j * ClassCommon.L - 菜单ToolStripMenuItem.Size.Height);
                        Btn[i, j].Size = new System.Drawing.Size(ClassCommon.L, ClassCommon.L);
                        Btn[i, j].Margin = new System.Windows.Forms.Padding(0, 0, 0, 0);
                        Btn[i, j].Name = Convert.ToString(i) + Convert.ToString(j);
                        Btn[i, j].Click += new EventHandler(ButtonClick);
                        panel1.Controls.Add(Btn[i, j]);
                    }
                PutButton();
            }
            private void ButtonClick(object sender, EventArgs e)
            {
                Button t = (Button)sender;
                int x = t.Name[0] - '0';
                int y = t.Name[1] - '0';
                int deltaX = ClassCommon.NowX - x;
                int deltaY = ClassCommon.NowY - y;
                if (deltaX < 0)
                    deltaX *= -1;
                if (deltaY < 0)
                    deltaY *= -1;
                int delta = deltaX + deltaY;
                if (delta > 1) return;
                GetStep(ClassCommon.NowX, ClassCommon.NowY, x, y);
                ClassCommon.G[ClassCommon.GameN].x = ClassCommon.NowX;
                ClassCommon.G[ClassCommon.GameN].y = ClassCommon.NowY;
                ++ClassCommon.GameN;
                ClassCommon.NowX = x;
                ClassCommon.NowY = y;
                ++ClassCommon.Step;
                toolStripStatusLabel1.Text = "已走:" + Convert.ToString(ClassCommon.Step) + " 步  ";
                if (JudgeGame())
                {
                    timer1.Stop();
                    MessageBox.Show("恭喜你闯关成功\n点击确定重新开始游戏", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    NewGame();
                }
            }
            private bool JudgeGame()
            {
                for (int i = 0; i < ClassCommon.N; ++i)
                    for (int j = 0; j < ClassCommon.N; ++j)
                    {
                        if (i == ClassCommon.N - 1 && j == ClassCommon.N - 1)
                            continue;
                        if (Btn[j, i].Text != Convert.ToString(i * ClassCommon.N + j + 1))
                            return false;
                    }
                return true;
            }
            private void Form1_Load(object sender, EventArgs e)
            {
                NewGame();
            }
            private void 初级ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                ClassCommon.N = 3;
                NewGame();
            }
            private void 中级ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                ClassCommon.N = 4;
                NewGame();
            }
            private void 高级ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                ClassCommon.N = 5;
                NewGame();
            }
            private void 重新开始ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                timer1.Stop();
                DialogResult result = MessageBox.Show("确定要重新开始?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                if (result == DialogResult.OK)
                    NewGame();
                timer1.Start();
            }
            private void 退出ToolStripMenuItem1_Click(object sender, EventArgs e)
            {
                timer1.Stop();
                DialogResult result = MessageBox.Show("确定要退出游戏?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                if (result == DialogResult.OK)
                    this.Close();
                timer1.Start();
            }
            private void 关于ToolStripMenuItem1_Click(object sender, EventArgs e)
            {
                timer1.Stop();
                MessageBox.Show("作者:林海鸿\n学号:13055712\n网站:haipz.com\n", "关于", MessageBoxButtons.OK, MessageBoxIcon.Information);
                timer1.Start();
            }
            public static bool Delay(int delayTime)
            {
                DateTime now = DateTime.Now;
                int s;
                do
                {
                    TimeSpan spand = DateTime.Now - now;
                    s = spand.Milliseconds;
                    Application.DoEvents();
                }
                while (s < delayTime);
                return true;
            }
            private void 自动完成ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                this.Enabled = false;
                while (ClassCommon.GameN != 0)
                {
                    if (Delay(50))
                        --ClassCommon.GameN;
                    GetStep(ClassCommon.NowX, ClassCommon.NowY, ClassCommon.G[ClassCommon.GameN].x, ClassCommon.G[ClassCommon.GameN].y);
                    ClassCommon.NowX = ClassCommon.G[ClassCommon.GameN].x;
                    ClassCommon.NowY = ClassCommon.G[ClassCommon.GameN].y;
                    ++ClassCommon.Step;
                    toolStripStatusLabel1.Text = "已走:" + Convert.ToString(ClassCommon.Step) + " 步  ";
                }
                while (ClassCommon.RandomN != 0)
                {
                    if (Delay(50))
                        --ClassCommon.RandomN;
                    GetStep(ClassCommon.NowX, ClassCommon.NowY, ClassCommon.F[ClassCommon.RandomN].x, ClassCommon.F[ClassCommon.RandomN].y);
                    ClassCommon.NowX = ClassCommon.F[ClassCommon.RandomN].x;
                    ClassCommon.NowY = ClassCommon.F[ClassCommon.RandomN].y;
                    ++ClassCommon.Step;
                    toolStripStatusLabel1.Text = "已走:" + Convert.ToString(ClassCommon.Step) + " 步  ";
                }
                timer1.Stop();
                MessageBox.Show("已经自动完成\n点击确定重新开始游戏", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Enabled = true;
                NewGame();
            }
            private void timer1_Tick(object sender, EventArgs e)
            {
                ++ClassCommon.Time;
                toolStripStatusLabel2.Text = "耗时:" + Convert.ToString(ClassCommon.Time) + " 秒";
            }
            protected override bool ProcessDialogKey(Keys keyData)
            {
                int k = -1;
                switch (keyData)
                {
                    case Keys.Up: k = 3;
                        break;
                    case Keys.Down: k = 2;
                        break;
                    case Keys.Left: k = 1;
                        break;
                    case Keys.Right: k = 0;
                        break;
                }
                if (k != -1)
                {
                    int x = ClassCommon.NowX + ClassCommon.dir[k, 0];
                    int y = ClassCommon.NowY + ClassCommon.dir[k, 1];
                    if (x >= 0 && y >= 0 && x < ClassCommon.N && y < ClassCommon.N)
                    {
                        GetStep(ClassCommon.NowX, ClassCommon.NowY, x, y);
                        Btn[x, y].Focus();
                        ClassCommon.G[ClassCommon.GameN].x = ClassCommon.NowX;
                        ClassCommon.G[ClassCommon.GameN].y = ClassCommon.NowY;
                        ++ClassCommon.GameN;
                        ClassCommon.NowX = x;
                        ClassCommon.NowY = y;
                        ++ClassCommon.Step;
                        toolStripStatusLabel1.Text = "已走:" + Convert.ToString(ClassCommon.Step) + " 步  ";
                        if (JudgeGame())
                        {
                            timer1.Stop();
                            MessageBox.Show("恭喜你闯关成功\n点击确定重新开始游戏", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            NewGame();
                        }
                    }
                }
                return base.ProcessDialogKey(keyData);
            }
        }
    }

ClassCommon.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NDigital
{
	public class ClassCommon
	{
		public struct Data
		{
			public int	x;
			public int	y;
		}
		public static int	Num	= 10000;
		public static Data[]	F	= new Data[Num];
		public static Data[]	G	= new Data[Num];
		public static int	N	= 3;
		public static int	L	= 100;
		public static int	MaxN	= 20;
		public static int	RandomN = N * N * N;
		public static int	GameN	= 0;
		public static int	NowX	= N - 1;
		public static int	NowY	= N - 1;
		public static int[, ] dir = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };
		public static int	Time	= 0;
		public static int	Step	= 0;
	}
}

转载保留版权:http://haipz.com/blog/i/788 - 海胖博客