Using Xamarin to develop mobile application example - Sudoku game adds fallback and timing functions

Posted by abgoosht on Wed, 26 Jan 2022 05:09:14 +0100

The project code can be downloaded from Github: https://github.com/zhenl/ZL.Shudu . The code is updated with the progress of the project.

We have created a basic interface that can run Sudoku games. Now we add some functions to the game. The first is the timing function. As an intelligence game, Sudoku completion time is an important indicator. There is also the fallback function. In many cases, you need to try and make mistakes in the process of the game. If you enter a dead game, you need to be able to go back to the previous steps and try a new path. Without this function, you can only start again to reduce the attraction of the game.

The realization of timing function is relatively simple. As long as the time is recorded at the beginning and completion, the time difference is calculated and displayed. The fallback function can be completed by using the stack. Put each operation step on the stack and pop up a record from the stack, which is the step of fallback.

First, add two variables to record the start time and fallback process:

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

Then, at the beginning of the game, record the start time and clear the stack:

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

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

After entering the number, record the input position and value and store it in the stack:

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

Add events to respond to fallback:

        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 = "";
            }
        }

When finished, the time taken is displayed:

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

We also need to add the function of new games. At present, there is only one game, and add some more. Now we can only simply implement this function, save some games in the array, and randomly select one when starting the game. The code is as follows:

        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}
        }
        };

Modify the function to generate a new game:

        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);
        }

Well, the game is basically completed here. The project code can be downloaded from Github: https://github.com/zhenl/ZL.Shudu . The code is updated with the progress of the project.
Now you can start a new game on your mobile phone. When you play for a while, suddenly a phone comes in, answers the phone, and then returns to the game. What? The original progress has been lost! Next, we need to save the current progress locally.

Topics: C# xamarin