/// <summary> /// Helper method to get the absolute position with respect to the screen borders /// </summary> private Thickness GetPositionForCell(Cell cell) { var pos = new System.Windows.Point(cell.ActualWidth / 2 - numberSelection.KeyboardSize.Width / 2, cell.ActualHeight / 2 - numberSelection.KeyboardSize.Height / 2); pos = cell.TransformToVisual(LayoutRoot).Transform(pos); if (pos.X < 0) pos.X = 0; else if (pos.X > LayoutRoot.ActualWidth - numberSelection.KeyboardSize.Width) pos.X = LayoutRoot.ActualWidth - numberSelection.KeyboardSize.Width; if (pos.Y < 0) pos.Y = 0; else if (pos.Y > LayoutRoot.ActualHeight - numberSelection.KeyboardSize.Height) pos.Y = LayoutRoot.ActualHeight - numberSelection.KeyboardSize.Height; return new Thickness(pos.X, pos.Y, 0, 0); }
/// <summary> /// Action triggered when user selected a number /// </summary> private void OnNumberChoosen(Cell sender, int number) { var conflictingCells = game.SetNumberByPlayer((int)sender.GetValue(Grid.RowProperty), (int)sender.GetValue(Grid.ColumnProperty), number); foreach (var point in conflictingCells) cells[point.X][point.Y].Blink(); SoundHelper.PlaySound(SoundHelper.SoundType.CellSelectedSound); if (gameState != GameState.NotStarted && game.EmptyCells == 0) GameEnds(); }
/// <summary> /// Creates the grid cells and populates the board grid with the cells /// </summary> /// <returns>9x9 array of empty cells</returns> private Cell[][] CreateGrid() { var darkImage = new BitmapImage(new Uri("/gfx/darkGridItem.png", UriKind.Relative)); var lightImage = new BitmapImage(new Uri("/gfx/lightGridItem.png", UriKind.Relative)); bool lightCell = false; Cell[][] cells = new Cell[GameLogic.RowLength][]; for (int row = 0; row < GameLogic.RowLength; row++) { cells[row] = new Cell[GameLogic.ColumnLength]; if (row % GameLogic.BlockSize != 0) lightCell = !lightCell; for (int col = 0; col < GameLogic.ColumnLength; col++) { // switch image type (light or dark) after each 3 cells in row if (col % GameLogic.BlockSize == 0) lightCell = !lightCell; Cell c = new Cell(); c.SetValue(Grid.RowProperty, row); c.SetValue(Grid.ColumnProperty, col); c.BackgroundImage.Source = lightCell ? lightImage : darkImage; // install event handler c.MouseLeftButtonDown += new MouseButtonEventHandler(OnCellTouched); // set binding to proper BoardValue from BoardViewModel Binding b = new Binding(string.Format("BoardNumbers[{0}][{1}].Value", row, col)); c.SetBinding(Cell.ValueProperty, b); Binding b2 = new Binding(string.Format("BoardNumbers[{0}][{1}].SetByGame", row, col)); c.SetBinding(Cell.SetByGameProperty, b2); cells[row][col] = c; BoardGrid.Children.Add(c); } } return cells; }