示例#1
0
        private void TileTouched(object sender, View.TouchEventArgs e)
        {
            // Check if the touch has finished
            if (e.Event.Action == MotionEventActions.Up)
            {
                TextTileView view = (TextTileView)sender;

                // Calculate the distance between the touched tile and the empty spot
                double xDiff    = Math.Pow(view.PositionX - emptySpot.X, 2);
                double yDiff    = Math.Pow(view.PositionY - emptySpot.Y, 2);
                double distance = Math.Sqrt(xDiff + yDiff);

                // If they're adjacent, move the tile
                if (distance == 1)
                {
                    view.LayoutParameters = GetTileLayoutParams(emptySpot.X, emptySpot.Y);
                    int emptyX = emptySpot.X;
                    int emptyY = emptySpot.Y;
                    emptySpot.X    = view.PositionX;
                    emptySpot.Y    = view.PositionY;
                    view.PositionX = emptyX;
                    view.PositionY = emptyY;
                }
            }
        }
        private void ShowHelp(object sender, EventArgs e)
        {
            if (solver == null)
            {
                return;
            }

            List <SolutionMove> solution = solver.get_solution();

            if (solution.Count > 0)
            {
                SolutionMove hintMove = solution[0];
                int          node_id  = board[hintMove.move_y][hintMove.move_x] + 1;

                // find the correct textTile and make it blink
                TextTileView textTile = (TextTileView)tilesArray[Array.IndexOf(coordsArray, node_id)];

                ValueAnimator colorAnim = ObjectAnimator.OfInt(textTile, "BackgroundColor", Color.DarkGreen, Color.Green);
                colorAnim.SetDuration(350);
                colorAnim.SetEvaluator(new ArgbEvaluator());
                colorAnim.RepeatCount = 2;
                colorAnim.RepeatMode  = ValueAnimatorRepeatMode.Reverse;
                colorAnim.Start();
            }
        }
        private void MakeTiles()
        {
            if (effects_enabled)
            {
                audioManager.PlaySoundEffect(SoundEffect.Standard);
            }
            // Clean up the GridLayout
            mainLayout.RemoveAllViews();

            // Calculate the width/height of the tiles
            tileWidth = (int)(gameViewWidth / gridSize);

            // Initialize ArrayLists
            tilesArray  = new ArrayList();
            coordsArray = PuzzleMixer.GetPuzzleArray((gridSize * gridSize), gridSize, NumChanges, true, rnd);

            board = CreateBoard();

            int tileIndex = 0;

            for (int row = 0; row < gridSize; row++)
            {
                for (int col = 0; col < gridSize; col++)
                {
                    if (coordsArray[tileIndex] == 0) // this is the empty tile
                    {
                        emptySpot       = new Point(row, col);
                        board[row][col] = gridSize * gridSize - 1;
                        tileIndex++;
                        continue;
                    }
                    board[row][col] = coordsArray[tileIndex] - 1;

                    // Create the tile (TextTileView)
                    TextTileView textTile = new TextTileView(this)
                    {
                        // Set the tile with the layout parameter
                        LayoutParameters = GetTileLayoutParams(row, col),
                        TextSize         = 40,
                        Text             = coordsArray[tileIndex].ToString(),
                        Gravity          = GravityFlags.Center,
                        PositionX        = row,
                        PositionY        = col
                    };
                    textTile.SetBackgroundColor(Color.Green);
                    textTile.SetTextColor(Color.Black);
                    // Subscribe to the tile's Touch event handler
                    textTile.Touch += TileTouched;
                    // Add the tile to the game's grid layout
                    mainLayout.AddView(textTile);
                    // Keep the tile and its coordenate in the arrays
                    tilesArray.Add(textTile);
                    tileIndex++;
                }
            }
            ResetSolver();
            NumberOfMoves      = 0;
            movesTextView.Text = string.Format("Moves: {0}", 0);
        }
        private void TileTouched(object sender, View.TouchEventArgs e)
        {
            // Check if the touch has finished
            if (e.Event.Action == MotionEventActions.Up)
            {
                TextTileView view = (TextTileView)sender;
                // Calculate the distance between the touched tile and the empty spot
                double yDiff = (view.PositionX - emptySpot.X);
                double xDiff = (view.PositionY - emptySpot.Y);

                // If they're adjacent, move the tile
                if ((Math.Abs(xDiff) == 1 && yDiff == 0) || (xDiff == 0 && Math.Abs(yDiff) == 1))
                {
                    int emptyX   = emptySpot.X;
                    int emptyY   = emptySpot.Y;
                    int textPosX = view.PositionX;
                    int textPosY = view.PositionY;

                    view.LayoutParameters = GetTileLayoutParams(emptyX, emptyY);

                    emptySpot.X    = textPosX;
                    emptySpot.Y    = textPosY;
                    view.PositionX = emptyX;
                    view.PositionY = emptyY;

                    int tmp = board[textPosX][textPosY];
                    board[textPosX][textPosY] = board[emptyX][emptyY];
                    board[emptyX][emptyY]     = tmp;

                    NumberOfMoves++;
                    if (CheckCorrect())    // player won!
                    {
                        bool newHighScore = false;
                        // update high score in shared settings
                        if (NumberOfMoves < highScore || highScore == 0)
                        {
                            highScore = NumberOfMoves;
                            editor.PutInt(highscoreString, highScore);
                            editor.Apply();
                            newHighScore = true;
                        }
                        var fragmentTrans = FragmentManager.BeginTransaction();
                        fragmentTrans.AddToBackStack(null);
                        var gameOverDialog = GameOverDialogFragment.NewInstance(gridSize, NumberOfMoves, highScore, userName, newHighScore);
                        gameOverDialog.RestartGameEvent += Reset;
                        gameOverDialog.ExitGameEvent    += OnExitGame;
                        gameOverDialog.Cancelable        = false;
                        gameOverDialog.Show(fragmentTrans, "GameOverDialog");
                    }
                    ResetSolver();
                    movesTextView.Text = string.Format("Moves: {0}", NumberOfMoves.ToString());
                    if (effects_enabled)
                    {
                        audioManager.PlaySoundEffect(SoundEffect.KeyClick);
                    }
                }
            }
        }
示例#5
0
        private void MakeTiles()
        {
            // Clean up the GridLayout
            mainLayout.RemoveAllViews();

            // Calculate the width/height of the tiles
            tileWidth = gameViewWidth / 4;

            // Initialize ArrayLists
            tilesArray  = new ArrayList();
            coordsArray = new ArrayList();

            // Counter for the tile numbers
            int count = 1;

            for (int row = 0; row < 4; row++)
            {
                for (int col = 0; col < 4; col++)
                {
                    // Create the tile (TextTileView)
                    TextTileView textTile = new TextTileView(this);

                    // Set the tile with the layout parameter
                    textTile.LayoutParameters = GetTileLayoutParams(row, col);

                    textTile.SetBackgroundColor(Color.Green);
                    textTile.Text = count++.ToString();
                    textTile.SetTextColor(Color.Black);
                    textTile.TextSize = 40;
                    textTile.Gravity  = GravityFlags.Center;

                    // Subscribe to the tile's Touch event handler
                    textTile.Touch += TileTouched;

                    // Add the tile to the game's grid layout
                    mainLayout.AddView(textTile);

                    // Keep the tile and its coordenate in the arrays
                    tilesArray.Add(textTile);
                    coordsArray.Add(new Point(row, col));
                }
            }

            mainLayout.RemoveView((TextTileView)tilesArray[15]);
            tilesArray.RemoveAt(15);
            Randomize();
        }