示例#1
0
        /// <summary>
        /// 刷新计算
        /// </summary>
        /// <param name="calculateArray"></param>
        private void RefleshCalculate(List <List <int> > calculateArray)
        {
            int score     = 0;              // 分数
            int maxNumber = 0;              // 最大值

            for (int rowIndex = 0; rowIndex < 4; rowIndex++)
            {
                for (int columnIndex = 0; columnIndex < 4; columnIndex++)
                {
                    Label  labelItem       = LABELARRAY[rowIndex][columnIndex];
                    string calculateNumber = calculateArray[rowIndex][columnIndex].ToString();
                    // 计算分数
                    score += int.Parse(calculateNumber);
                    // 计算最大分值
                    if (int.Parse(calculateNumber) > maxNumber)
                    {
                        maxNumber = int.Parse(calculateNumber);
                    }
                    // 渲染颜色
                    Dictionary <string, object> labelStyle = ClassCalculate.GetNumberLabelStyle(int.Parse(calculateNumber));
                    labelItem.Font      = (Font)labelStyle["font"];
                    labelItem.BackColor = (Color)labelStyle["backColor"];
                    labelItem.ForeColor = (Color)labelStyle["foreColor"];

                    // 输出结果
                    labelItem.Text = calculateNumber;
                }
            }
            // 分值
            lblGameScore.Text = (score * maxNumber).ToString();
            // lblMaxNumber.Text = maxNumber.ToString();
        }
示例#2
0
        private void FrmMain_KeyDown(object sender, KeyEventArgs e)
        {
            string beforeCalculateLabelNumberArrayString = ClassCalculate.GetCurrentLabelNumberString(GetCurrentLabelNumberArray());

            // 执行标签数字计算
            if (e.KeyCode == Keys.Up || e.KeyCode == Keys.W)
            {
                RefleshCalculate(LabelNumberCalculate(0, LABELARRAY));
            }
            else if (e.KeyCode == Keys.Left || e.KeyCode == Keys.A)
            {
                RefleshCalculate(LabelNumberCalculate(1, LABELARRAY));
            }
            else if (e.KeyCode == Keys.Down || e.KeyCode == Keys.S)
            {
                RefleshCalculate(LabelNumberCalculate(2, LABELARRAY));
            }
            else if (e.KeyCode == Keys.Right || e.KeyCode == Keys.D)
            {
                RefleshCalculate(LabelNumberCalculate(3, LABELARRAY));
            }

            // 创建新数字
            List <List <int> > afterCalculateLabelNumberArray       = GetCurrentLabelNumberArray();
            string             afterCalculateLabelNumberArrayString = ClassCalculate.GetCurrentLabelNumberString(afterCalculateLabelNumberArray);

            if (beforeCalculateLabelNumberArrayString != afterCalculateLabelNumberArrayString)
            {
                RefleshCalculate(RandomNewNumberTwoOrFour());
            }

            // 检验游戏是否结束
            int endGameState = 0;

            for (int rowIndex = 0; rowIndex <= 3; rowIndex++)
            {
                for (int columnIndex = 0; columnIndex <= 3; columnIndex++)
                {
                    int currentLabelNumber = afterCalculateLabelNumberArray[rowIndex][columnIndex];
                    if (currentLabelNumber == 0)
                    {
                        endGameState += 1;
                    }

                    if (rowIndex > 0)
                    {
                        if (currentLabelNumber == afterCalculateLabelNumberArray[rowIndex - 1][columnIndex])
                        {
                            endGameState += 1;
                        }
                    }
                    else
                    {
                        if (currentLabelNumber == afterCalculateLabelNumberArray[rowIndex + 1][columnIndex])
                        {
                            endGameState += 1;
                        }
                    }

                    if (columnIndex > 0)
                    {
                        if (currentLabelNumber == afterCalculateLabelNumberArray[rowIndex][columnIndex - 1])
                        {
                            endGameState += 1;
                        }
                    }
                    else
                    {
                        if (currentLabelNumber == afterCalculateLabelNumberArray[rowIndex][columnIndex + 1])
                        {
                            endGameState += 1;
                        }
                    }
                }
            }
            // 1. 判断上下左右四个方位是否与当前值一致, 如果一致则说明还有可以合并的地方
            // 2. 判断当前位置是否为空(0), 如果为空(0)则说明还可以继续创建新数字
            if (endGameState == 0)
            {
                btnRestart.Show();
            }
        }