Example #1
0
        private async System.Threading.Tasks.Task <bool> MovePiece(int from, int to)
        {
            int boardLoc  = from; //need move
            int direction = to;   // to
            int blackLoc  = -1;

            foreach (splitImage item in this.countImage)
            {
                if (item.bitmapImage == simpleImage1)
                {
                    blackLoc = item.numberLocation;  //to
                }
            }

            // Check if we can move
            if ((boardLoc == blackLoc + 1) || (boardLoc == blackLoc - 1) ||
                (boardLoc == blackLoc + 4) || (boardLoc == blackLoc - 4))
            {
                splitImage temp = new splitImage();

                foreach (splitImage item in this.countImage)
                {
                    if (item.numberLocation == from)
                    {
                        foreach (splitImage subItem in this.countImage)
                        {
                            if (subItem.numberLocation == to)
                            {
                                temp.currentLocalX  = item.currentLocalX;
                                temp.currentLocalY  = item.currentLocalY;
                                temp.numberLocation = item.numberLocation;

                                item.currentLocalX  = subItem.currentLocalX;
                                item.currentLocalY  = subItem.currentLocalY;
                                item.numberLocation = subItem.numberLocation;

                                subItem.currentLocalX  = temp.currentLocalX;
                                subItem.currentLocalY  = temp.currentLocalY;
                                subItem.numberLocation = temp.numberLocation;


                                Grid.SetColumn(item.bitmapImage, item.currentLocalX);
                                Grid.SetRow(item.bitmapImage, item.currentLocalY);

                                Grid.SetColumn(subItem.bitmapImage, subItem.currentLocalX);
                                Grid.SetRow(subItem.bitmapImage, subItem.currentLocalY);

                                return(true);
                            }
                        }
                    }
                }
            }
            return(false);
        }
Example #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="file"></param>
        async private void BitmapTransformTest(StorageFile file)
        {
            Random r = new Random();

            int z = 0;

            for (int x = 0; x < 4; x++)
            {
                for (int y = 0; y < 4; y++)
                {
                    Image simpleImage = new Image();
                    var   fileStream  = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);

                    BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream);

                    // create a new stream and encoder for the new image
                    InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream();
                    BitmapEncoder enc = await BitmapEncoder.CreateForTranscodingAsync(ras, decoder);

                    // convert the entire bitmap to a 800px by 800px bitmap
                    enc.BitmapTransform.ScaledHeight = 800;
                    enc.BitmapTransform.ScaledWidth  = 800;

                    BitmapBounds bounds = new BitmapBounds();
                    bounds.Height = 200;
                    bounds.Width  = 200;
                    bounds.X      = Convert.ToUInt32(0 + x * 200);
                    bounds.Y      = Convert.ToUInt32(0 + y * 200);
                    enc.BitmapTransform.Bounds = bounds;

                    // write out to the stream
                    try
                    {
                        await enc.FlushAsync();
                    }
                    catch (Exception ex)
                    {
                        string s = ex.ToString();
                    }

                    BitmapImage bImg = new BitmapImage();
                    bImg.SetSource(ras);

                    int theCurrentLocalX  = r.Next(0, 4);
                    int theCurrentLocalY  = r.Next(0, 4);
                    int theNumberLocation = 4 * theCurrentLocalX + theCurrentLocalY;

                    if (countImage != null)
                    {
                        for (int j = 0; j < countImage.Count(); j++)
                        {
                            if (theCurrentLocalX == countImage[j].getCurrentLocalX() && theCurrentLocalY == countImage[j].getCurrentLocalY())
                            {
                                theCurrentLocalX  = r.Next(0, 4);
                                theCurrentLocalY  = r.Next(0, 4);
                                theNumberLocation = 4 * theCurrentLocalX + theCurrentLocalY;
                                j = -1;
                            }
                        }
                    }
                    if (x == 3 && y == 3)
                    {
                        StorageFile file1 = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/blank.png"));

                        //await file1.CopyAsync(ApplicationData.Current.LocalFolder, "image.png");
                        BitmapImage img = new BitmapImage();

                        img = await LoadImage(file1);

                        simpleImage1.Source = img;



                        //simpleImage.Source = null; // image element in xaml
                        splitImage splitImage = new splitImage(theCurrentLocalX, theCurrentLocalY, x, y, theNumberLocation, z, 0, simpleImage1);
                        countImage.Add(splitImage);
                        Grid.SetRow(simpleImage1, theCurrentLocalY);
                        Grid.SetColumn(simpleImage1, theCurrentLocalX);
                        gridGameField.Children.Add(simpleImage1);
                    }
                    else
                    {
                        simpleImage.Source = bImg; // image element in xaml
                        splitImage splitImage = new splitImage(theCurrentLocalX, theCurrentLocalY, x, y, theNumberLocation, z, 0, simpleImage);
                        countImage.Add(splitImage);
                        Grid.SetRow(simpleImage, theCurrentLocalY);
                        Grid.SetColumn(simpleImage, theCurrentLocalX);
                        gridGameField.Children.Add(simpleImage);
                    }

                    z++;
                }
            }
        }