void OnStackSizeChanged(object sender, EventArgs args) { double width = stackLayout.Width; double height = stackLayout.Height; if (width <= 0 || height <= 0) { return; } // Orient StackLayout based on portrait/landscape mode. stackLayout.Orientation = (width < height) ? StackOrientation.Vertical : StackOrientation.Horizontal; // Calculate square size and position based on stack size. squareSize = Math.Min(width, height) / NUM; absoluteLayout.WidthRequest = NUM * squareSize; absoluteLayout.HeightRequest = NUM * squareSize; foreach (View view in absoluteLayout.Children) { XuzzleRandSquare square = (XuzzleRandSquare)view; square.SetLabelFont(0.4 * squareSize, FontAttributes.Bold); AbsoluteLayout.SetLayoutBounds(square, new Rectangle(square.Col * squareSize, square.Row * squareSize, squareSize, squareSize)); } }
async Task AnimateSquare(int row, int col, int newRow, int newCol, uint length) { // The Square to be animated. XuzzleRandSquare animaSquare = squares [row, col]; // The destination rectangle. Rectangle rect = new Rectangle(squareSize * emptyCol, squareSize * emptyRow, squareSize, squareSize); // This is the actual animation call. await animaSquare.LayoutTo(rect, length); // Set several variables and properties for new layout. squares [newRow, newCol] = animaSquare; animaSquare.Row = newRow; animaSquare.Col = newCol; squares [row, col] = null; emptyRow = row; emptyCol = col; }
async void OnSquareTapped(object parameter) { if (isBusy) { return; } isBusy = true; XuzzleRandSquare tappedSquare = (XuzzleRandSquare)parameter; await ShiftIntoEmpty(tappedSquare.Row, tappedSquare.Col); isBusy = false; // Check for a "win". if (isPlaying) { int index; for (index = 0; index < NUM * NUM - 1; index++) { int row = index / NUM; int col = index % NUM; XuzzleRandSquare square = squares [row, col]; if (square == null || square.Index != index) { break; } } // We have a winner! if (index == NUM * NUM - 1) { isPlaying = false; await DoWinAnimation(); } } }
void getRandomText() { // AbsoluteLayout to host the squares. absoluteLayout = new AbsoluteLayout() { HorizontalOptions = LayoutOptions.Center, VerticalOptions = LayoutOptions.Center }; using (reader) { // Seed a random number in the range 0 to your count. Random random = new Random(); int randomNumber = random.Next(0, lines.Length); // Read the random line. text = lines.Skip(randomNumber - 1).Take(1).First().ToUpper().Trim(); text = Regex.Replace(text, @"\s+", ""); while (text.Length < 15) { randomNumber = random.Next(0, lines.Length); string text2 = lines.Skip(randomNumber - 1).Take(1).First().ToUpper().Trim(); text2 = Regex.Replace(text2, @"\s+", ""); text = string.Concat(text, text2); } text = text.Truncate(15); } // Create XuzzleSquare's for all the rows and columns. index = 0; for (int row = 0; row < NUM; row++) { for (int col = 0; col < NUM; col++) { // But skip the last one! if (row == NUM - 1 && col == NUM - 1) { break; } // Instantiate XuzzleSquare. square = new XuzzleRandSquare(text [index], winText [index], index) { Row = row, Col = col }; // Add tap recognition. TapGestureRecognizer tapGestureRecognizer = new TapGestureRecognizer { Command = new Command(OnSquareTapped), CommandParameter = square }; square.GestureRecognizers.Add(tapGestureRecognizer); // Add it to the array and the AbsoluteLayout. squares [row, col] = square; absoluteLayout.Children.Add(square); index++; } } }
void getRandomText () { // AbsoluteLayout to host the squares. absoluteLayout = new AbsoluteLayout () { HorizontalOptions = LayoutOptions.Center, VerticalOptions = LayoutOptions.Center }; using (reader) { // Seed a random number in the range 0 to your count. Random random = new Random(); int randomNumber = random.Next(0, lines.Length); // Read the random line. text = lines.Skip(randomNumber - 1).Take(1).First().ToUpper().Trim(); text = Regex.Replace (text, @"\s+", ""); while (text.Length < 15) { randomNumber = random.Next(0, lines.Length); string text2 = lines.Skip(randomNumber - 1).Take(1).First().ToUpper().Trim(); text2 = Regex.Replace (text2, @"\s+", ""); text = string.Concat(text, text2); } text = text.Truncate (15); } // Create XuzzleSquare's for all the rows and columns. index = 0; for (int row = 0; row < NUM; row++) { for (int col = 0; col < NUM; col++) { // But skip the last one! if (row == NUM - 1 && col == NUM - 1) break; // Instantiate XuzzleSquare. square = new XuzzleRandSquare (text [index], winText [index], index) { Row = row, Col = col }; // Add tap recognition. TapGestureRecognizer tapGestureRecognizer = new TapGestureRecognizer { Command = new Command (OnSquareTapped), CommandParameter = square }; square.GestureRecognizers.Add (tapGestureRecognizer); // Add it to the array and the AbsoluteLayout. squares [row, col] = square; absoluteLayout.Children.Add (square); index++; } } }