项目代码可以从Github下载:https://github.com/zhenl/ZL.Shudu 。代码随项目进度更新。

我们已经创建了可以运行数独游戏的基本界面,现在为游戏增加一些功能,首先是计时功能,数独作为一种智力游戏,完成时间是重要的指标。还有就是回退功能,在很多情况下,游戏过程中需要试错,如果进入死局,需要能够回到上几步,再试一个新的路径,如果没有这个功能,只能重新开始,降低游戏的吸引力。

计时功能的实现比较简单,只要在开始和完成时记录时间,计算出时间差并显示就可以了。回退功能可以使用堆栈完成,将每一操作步骤放入堆栈,从堆栈中弹出一条记录,就是回退的一步。

首先增加两个变量,记录开始时间和回退过程:

        private DateTime dtBegin;
        private Stack<string> steps = new Stack<string>();

然后,在开始游戏的时候,记录开始时间,并将堆栈清零:

            steps.Clear();
            dtBegin = DateTime.Now;

            this.lbFinish.IsVisible = false;
            this.lbTime.IsVisible = false;
            this.lbMessage.IsVisible = false;

在输入数字后,记录下输入的位置和值,存入堆栈:


            if (!checkval(x, y, num))
            {
                return;
            }
            steps.Push(x + "," + y + "," + num);
            currentButton.Text = currentNumBtn.Text;

增加响应回退的事件:


        private void btn_Reset_Clicked(object sender, EventArgs e)
        {
            if (steps.Count > 0)
            {
                var laststep = steps.Pop();

                var arr = laststep.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                int x = int.Parse(arr[0]), y = int.Parse(arr[1]), num = int.Parse(arr[2]);
                buttons[x, y].Text = "";
            }
        }

在完成的时候,显示耗时时间:

            if (IsFinish())
            {
                lbFinish.IsVisible = true;
                lbTime.IsVisible = true;
                rowResult.Height = 40;
                var diff = (DateTime.Now.Ticks - dtBegin.Ticks) / 10000 / 1000 / 60;
                lbTime.Text = diff + "分钟";
            }

还需要增加新游戏的功能,目前只有一个游戏,再多增加一些,现在只简单实现这个功能,将一些游戏保存在数组中,开始游戏时随机抽取一个。代码如下:

        private static int[,,] chesses =  {
           {
            {5,3,0,0,7,0,0,0,0},
            {6,0,0,1,9,5,0,0,0},
            {0,9,8,0,0,0,0,6,0},
            {8,0,0,0,6,0,0,0,3},
            {4,0,0,8,0,3,0,0,1},
            {7,0,0,0,2,0,0,0,6},
            {0,6,0,0,0,0,2,8,0},
            {0,0,0,4,1,9,0,0,5},
            {0,0,0,0,8,0,0,7,9}
            },
            {
            {3,0,2,4,0,7,9,0,1},
            {0,0,7,1,3,8,5,0,0},
            {6,0,0,0,0,0,0,0,4},
            {0,0,0,0,4,0,0,0,0},
            {0,9,4,0,0,0,1,5,0},
            {0,0,0,0,9,0,0,0,0},
            {4,0,0,0,0,0,0,0,5},
            {0,0,8,9,1,5,6,0,0},
            {5,0,9,6,0,4,7,0,2}
        },{
            {0,8,0,9,0,7,0,1,0},
            {9,0,0,3,8,6,0,0,5},
            {0,0,2,0,4,0,6,0,0},
            {5,9,0,0,0,0,0,2,6},
            {0,2,3,0,0,0,7,9,0},
            {7,4,0,0,0,0,0,5,1},
            {0,0,9,0,7,0,5,0,0},
            {4,0,0,8,5,9,0,0,2},
            {0,5,0,4,0,2,0,3,0}
        },{
            {4,0,8,0,0,0,0,0,5},
            {0,0,9,0,0,4,0,7,0},
            {3,1,0,2,0,0,8,0,0},
            {0,0,5,7,0,0,0,8,0},
            {0,0,0,0,9,0,0,0,0},
            {0,2,0,0,0,3,5,0,0},
            {0,0,2,0,0,9,0,1,4},
            {0,3,0,6,0,0,9,0,0},
            {1,0,0,0,0,0,7,0,6}
        },{
            {0,3,5,0,0,7,0,6,0},
            {8,0,6,0,1,0,0,0,7},
            {0,0,0,0,9,6,0,1,5},
            {4,0,1,8,0,3,0,0,0},
            {0,2,3,0,0,0,6,4,0},
            {0,0,0,6,0,2,3,0,1},
            {3,8,0,7,2,0,0,0,0},
            {6,0,0,0,3,0,7,0,9},
            {0,7,0,9,0,0,4,8,0}
        },{
            {0,0,0,7,0,4,0,0,0},
            {0,0,0,8,0,5,0,0,0},
            {5,8,0,0,0,0,0,4,7},
            {7,6,0,0,0,0,0,3,9},
            {9,2,0,6,1,3,0,7,8},
            {8,5,0,0,0,0,0,2,1},
            {1,7,0,0,0,0,0,9,3},
            {0,0,0,3,0,6,0,0,0},
            {0,0,0,1,0,7,0,0,0}
        },{
            {0,7,0,0,9,0,2,0,0},
            {0,0,0,0,0,7,0,0,5},
            {2,0,4,3,5,0,7,0,0},
            {0,5,0,0,4,0,9,0,0},
            {9,0,3,5,0,1,4,0,8},
            {0,0,7,0,6,0,0,1,0},
            {0,0,8,0,1,9,3,0,2},
            {7,0,0,2,0,0,0,0,0},
            {0,0,2,0,3,0,0,9,0}
        },{
            {3,0,2,4,0,7,9,0,1},
            {0,0,7,1,3,8,5,0,0},
            {6,0,0,0,0,0,0,0,4},
            {0,0,0,0,4,0,0,0,0},
            {0,9,4,0,0,0,1,5,0},
            {0,0,0,0,9,0,0,0,0},
            {4,0,0,0,0,0,0,0,5},
            {0,0,8,9,1,5,6,0,0},
            {5,0,9,6,0,4,7,0,2}
        }
        };

修改生成新游戏的函数:

        private void SetNewGame()
        {
            int k;
            var lst = chesses;
            var leng = lst.GetLength(0);

            if (ra == null)
            {
                ra = new Random();
            }

            k = ra.Next(0, leng);

            var mychess = new int[9, 9];
            for (var i = 0; i < 9; i++)
                for (var j = 0; j < 9; j++)
                {
                    mychess[i, j] = chesses[k, i, j];
                }

            SetGame(mychess);
        }

好了,到这里游戏基本上已经初步完成了,项目代码可以从Github下载:https://github.com/zhenl/ZL.Shudu 。代码随项目进度更新。
现在可以在手机上启动一个新的游戏试试了,当你玩了一会儿,忽然有个电话进来,接完电话,再回到游戏,What?原来的进度都丢了!下一步我们需要在本地保存当前的进度。

01-26 17:19