示例#1
0
        private bool MoveGrid(GameServiceModel game, Enum direction)
        {
            var isMoved = false;

            switch (direction)
            {
            case Left:
                isMoved = MoveLeft(game);
                break;

            case Right:
                isMoved = MoveRight(game);
                break;

            case Up:
                isMoved = MoveUp(game);
                break;

            case Down:
                isMoved = MoveDown(game);
                break;

            default:
                break;
            }

            UpdateMaxNumber(game);

            return(isMoved);
        }
示例#2
0
        private GameServiceModel MoveGameField(int[,] gameField, int currentScore, Enum direction)
        {
            var gameServiceModel = new GameServiceModel()
            {
                Field        = gameField,
                CurrentScore = currentScore
            };

            var isMoved = this.gameManager.MoveKey(gameServiceModel, direction);

            if (isMoved)
            {
                AddRandomNumber(gameServiceModel.Field);
            }
            else
            {
                var hasZeroValues = HasZeroValues(gameServiceModel.Field);

                if (!hasZeroValues)
                {
                    var isOver = IsGameOver(gameServiceModel.Field);
                    gameServiceModel.IsFinished = isOver;
                }
            }

            return(gameServiceModel);
        }
示例#3
0
        private bool MoveRight(GameServiceModel game)
        {
            var isMoved = false;
            var field   = game.Field;

            for (int i = 0; i < field.GetLength(0); i++)
            {
                var lastPositiveValue = field[i, field.GetLength(1) - 1];
                var zeroValues        = lastPositiveValue != 0 ? 0 : 1;

                for (int k = field.GetLength(1) - 2; k >= 0; k--)
                {
                    var currentNumber = field[i, k];

                    if (currentNumber != 0)
                    {
                        if (currentNumber == lastPositiveValue)
                        {
                            field[i, k + zeroValues + 1] *= 2;
                            field[i, k]        = 0;
                            lastPositiveValue  = 0;
                            game.CurrentScore += field[i, k + zeroValues + 1];
                            zeroValues++;
                            isMoved = true;
                        }
                        else if (zeroValues > 0)
                        {
                            field[i, k + zeroValues] = currentNumber;
                            field[i, k]       = 0;
                            lastPositiveValue = currentNumber;
                            isMoved           = true;
                        }
                        else
                        {
                            lastPositiveValue = currentNumber;
                        }
                    }
                    else
                    {
                        zeroValues++;
                    }
                }
            }

            return(isMoved);
        }
示例#4
0
        private void UpdateMaxNumber(GameServiceModel game)
        {
            var maxNumber = 0;

            for (int i = 0; i < game.Field.GetLength(0); i++)
            {
                for (int k = 0; k < game.Field.GetLength(1); k++)
                {
                    if (game.Field[i, k] > maxNumber)
                    {
                        maxNumber = game.Field[i, k];
                    }
                }
            }

            game.MaxNumber = maxNumber;
        }
示例#5
0
        private bool MoveUp(GameServiceModel game)
        {
            var isMoved = false;
            var field   = game.Field;

            for (int i = 0; i < field.GetLength(0); i++)
            {
                var lastPositiveValue = field[0, i];
                var zeroValues        = lastPositiveValue != 0 ? 0 : 1;

                for (int k = 1; k < field.GetLength(1); k++)
                {
                    var currentNumber = field[k, i];

                    if (currentNumber != 0)
                    {
                        if (currentNumber == lastPositiveValue)
                        {
                            field[k - zeroValues - 1, i] *= 2;
                            field[k, i]        = 0;
                            lastPositiveValue  = 0;
                            game.CurrentScore += field[k - zeroValues - 1, i];
                            zeroValues++;
                            isMoved = true;
                        }
                        else if (zeroValues > 0)
                        {
                            field[k - zeroValues, i] = currentNumber;
                            field[k, i]       = 0;
                            lastPositiveValue = currentNumber;
                            isMoved           = true;
                        }
                        else
                        {
                            lastPositiveValue = currentNumber;
                        }
                    }
                    else
                    {
                        zeroValues++;
                    }
                }
            }

            return(isMoved);
        }
示例#6
0
 public void SetInit()
 {
     this.games = new GameService();
     this.game  = new GameViewModel
     {
         Field = new int[, ]
         {
             { 2, 0, 2, 128 },
             { 8, 8, 8, 0 },
             { 8, 8, 2, 0 },
             { 16, 16, 16, 16 }
         }
     };
     this.serviceGame = new GameServiceModel()
     {
         Field        = this.game.Field,
         CurrentScore = this.game.CurrentScore
     };
 }
示例#7
0
 public bool MoveKey(GameServiceModel game, Enum direction)
 => MoveGrid(game, direction);