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); } } } }
private void FieldMouseUp(object sender, MouseButtonEventArgs e) { // Game already ended. Do nothing if (this.gameEnded == true) { return; } System.Windows.Controls.Label label = (System.Windows.Controls.Label)sender; (int, int)pos = ((int, int))label.Tag; int row = pos.Item1; int col = pos.Item2; // Reset face to default this.faceLabel.Background = new ImageBrush(new BitmapImage(new Uri("pack://application:,,,/Images/face_unclicked.png"))); // Already open do nothing. if (e.ChangedButton != MouseButton.Middle && mineField[row, col].fieldStatus == FieldStatus.open) { return; } // Check mouse click actions if (e.ChangedButton == MouseButton.Left) { if (mineField[row, col].isMine == true) { // Check for first move. First field should always be without mine. if (this.firstMoveDone == false) { generateField(); FieldMouseUp(sender, e); return; } else { this.dispatcherTimer.Stop(); this.gameEnded = true; this.faceLabel.Background = new ImageBrush(new BitmapImage(new Uri("pack://application:,,,/Images/face_dead.png"))); revealAll(); MessageBox.Show(Application.Current.FindResource("_saperGameOverMessage").ToString()); } } else { revealNeighbours(row, col); } } else if (e.ChangedButton == MouseButton.Middle) { if (mineField[row, col].fieldStatus == FieldStatus.open) { revealOnMiddle(row, col); } else { int startPosRow = (row - 1 < 0) ? row : (row - 1); int startPosCol = (col - 1 < 0) ? col : (col - 1); int endPosRow = (row + 1 >= rows) ? row : (row + 1); int endPosCol = (col + 1 >= cols) ? col : (col + 1); for (int rowNumber = startPosRow; rowNumber <= endPosRow; rowNumber++) { for (int colNumber = startPosCol; colNumber <= endPosCol; colNumber++) { if (mineField[rowNumber, colNumber].fieldStatus == FieldStatus.unopen) { displayField(rowNumber, colNumber); } } } } } else if (e.ChangedButton == MouseButton.Right) { if (mineField[row, col].fieldStatus == FieldStatus.unopen) { mineField[row, col].fieldStatus = FieldStatus.flag; NumberOfFlagsToBePlaced--; displayField(row, col); } else if (mineField[row, col].fieldStatus == FieldStatus.flag) { NumberOfFlagsToBePlaced++; mineField[row, col].fieldStatus = FieldStatus.questionMark; displayField(row, col); } else { mineField[row, col].fieldStatus = FieldStatus.unopen; displayField(row, col); } } if (!this.firstMoveDone) { this.dispatcherTimer.Start(); this.firstMoveDone = true; } NumberOfMoves++; if (winCheck()) { gameEnded = true; this.dispatcherTimer.Stop(); revealAll(true); NumberOfFlagsToBePlaced = 0; this.faceLabel.Background = new ImageBrush(new BitmapImage(new Uri("pack://application:,,,/Images/face_win.png"))); MessageBox.Show(Application.Current.FindResource("_saperWinMessage").ToString() + NumberOfMoves.ToString()); } }