//this event fires when a drag action is complete //it will be used to snap the tile into place, then call the methods used to update the game logic void ManipulateMe_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e) { //set this true so that the PointerReleased will not activate DontClickSnap = true; Image tempImage = (Image)sender; //get the mytile object tied to this image myTile temp = myTile.GetFromName(tempImage.Name.ToString(), theTiles); //save its original position int oldX = temp.chrdX; int oldY = temp.chrdY; //get the actual position of the tile on the canvas double top = Canvas.GetTop(tempImage); double left = Canvas.GetLeft(tempImage); double DistanceOriginal = 0; double DistanceNew = 0; bool blockMoved = false; //get the direction that the tile is supposed to move in if (temp.Move == 2 && temp.Direct) { //get the distances of between where the tile was coming from, and where it was going DistanceOriginal = myTile.LogicToBoard(temp.chrdY, 'h') - top; DistanceNew = top - myTile.LogicToBoard(temp.chrdY - 1, 'h'); //determine if it is closer the new spot or the original one. if (DistanceOriginal <= DistanceNew) { //put it back to were it came from Canvas.SetTop(tempImage, myTile.LogicToBoard(temp.chrdY, 'h')); } else { //snap it to the new position and set that it a block was moved Canvas.SetTop(tempImage, myTile.LogicToBoard(temp.chrdY - 1, 'h')); blockMoved = true; } } else if (temp.Move == 2 && !temp.Direct) { //get the distances of between where the tile was coming from, and where it was going DistanceNew = myTile.LogicToBoard(temp.chrdY + 1, 'h') - top; DistanceOriginal = top - myTile.LogicToBoard(temp.chrdY, 'h'); if (DistanceOriginal <= DistanceNew) { //put it back to were it came from Canvas.SetTop(tempImage, myTile.LogicToBoard(temp.chrdY, 'h')); } else { //snap it to the new position and set that it a block was moved Canvas.SetTop(tempImage, myTile.LogicToBoard(temp.chrdY + 1, 'h')); blockMoved = true; } } else if (temp.Move == 1 && temp.Direct) { //get the distances of between where the tile was coming from, and where it was going DistanceNew = myTile.LogicToBoard(temp.chrdX + 1, 'w') - left; DistanceOriginal = left - myTile.LogicToBoard(temp.chrdX, 'w'); if (DistanceOriginal <= DistanceNew) { //put it back to were it came from Canvas.SetLeft(tempImage, myTile.LogicToBoard(temp.chrdX, 'w')); } else { //snap it to the new position and set that it a block was moved Canvas.SetLeft(tempImage, myTile.LogicToBoard(temp.chrdX + 1, 'w')); blockMoved = true; } } else if (temp.Move == 1 && !temp.Direct) { //get the distances of between where the tile was coming from, and where it was going DistanceNew = left - myTile.LogicToBoard(temp.chrdX - 1, 'w'); DistanceOriginal = myTile.LogicToBoard(temp.chrdX, 'w') - left; if (DistanceOriginal <= DistanceNew) { //put it back to were it came from Canvas.SetLeft(tempImage, myTile.LogicToBoard(temp.chrdX, 'w')); } else { //snap it to the new position and set that it a block was moved Canvas.SetLeft(tempImage, myTile.LogicToBoard(temp.chrdX - 1, 'w')); blockMoved = true; } } //if a block was moved to a new position if (blockMoved) { //update the list of tile theTiles = TileHandler.UpdateTile(theTiles, oldX, oldY); //update the win sate int correctTiles = TileHandler.CheckWin(theTiles); if (correctTiles == 15) { StatusBar.Text = "You Win!"; } else { StatusBar.Text = "You have " + correctTiles + " in the correct place."; } } }
//This event will fire when the user releases the tile. //It will be used when the user clicks on the block to move it automatically. //It will be blocked if the ManipulateMe_ManipulationCompleted happened because the user was dragging the tile. private new void PointerReleased(object sender, PointerRoutedEventArgs e) { if (!DontClickSnap) { //stop any storyboard actions that are happening theStoryBoard.Stop(); //set the RepositionThemeAnimation to the current Storyboard.SetTarget(RTA, (UIElement)sender); //get the tile info Image tempBoarder = (Image)sender; myTile temp = myTile.GetFromName(tempBoarder.Name.ToString(), theTiles); //save the original position of the tile int oldX = temp.chrdX; int oldY = temp.chrdY; bool blockMoved = false; //check if the tile is able to move and in what direction if (temp.Move == 2 && temp.Direct) { //set the RepositionThemeAnimation movement for the tile RTA.FromVerticalOffset = Constants.BlockHeight; RTA.FromHorizontalOffset = 0; //begin the animation theStoryBoard.Begin(); Canvas.SetTop(tempBoarder, myTile.LogicToBoard(temp.chrdY - 1, 'h')); //set that the block moved blockMoved = true; } else if (temp.Move == 2 && !temp.Direct) { //set the RepositionThemeAnimation movement for the tile RTA.FromVerticalOffset = Constants.BlockHeight * -1; RTA.FromHorizontalOffset = 0; //begin the animation theStoryBoard.Begin(); Canvas.SetTop(tempBoarder, myTile.LogicToBoard(temp.chrdY + 1, 'h')); //set that the block moved blockMoved = true; } else if (temp.Move == 1 && temp.Direct) { //set the RepositionThemeAnimation movement for the tile RTA.FromVerticalOffset = 0; RTA.FromHorizontalOffset = Constants.BlockWidth * -1; //begin the animation theStoryBoard.Begin(); Canvas.SetLeft(tempBoarder, myTile.LogicToBoard(temp.chrdX + 1, 'w')); //set that the block moved blockMoved = true; } else if (temp.Move == 1 && !temp.Direct) { //set the RepositionThemeAnimation movement for the tile RTA.FromVerticalOffset = 0; RTA.FromHorizontalOffset = Constants.BlockWidth; //begin the animation theStoryBoard.Begin(); Canvas.SetLeft(tempBoarder, myTile.LogicToBoard(temp.chrdX - 1, 'w')); //set that the block moved blockMoved = true; } //if the block was actually moved if (blockMoved) { //update the logic of the tile theTiles = TileHandler.UpdateTile(theTiles, oldX, oldY); //check how many tiles are in the correct position int correctTiles = TileHandler.CheckWin(theTiles); //update the status bar if (correctTiles == 15) { StatusBar.Text = "You Win"; } else { StatusBar.Text = "You have " + correctTiles + " in the correct place"; } } } DontClickSnap = false; }
//so now we have the image data from the system, and it is split into all the pieces. //we will now put it onto the canvas public void ConfigureImagesOnCanvas() { //set the hight and width so that they are valid sizes while (Constants.BlockHeight > 150 || Constants.BlockWidth > 150) { Constants.BlockHeight = Constants.BlockHeight * 0.8; Constants.BlockWidth = Constants.BlockWidth * 0.8; } //for each image on the canvas, set its position in the canvas, and its hight and width IRow0Column0.Height = Constants.BlockHeight; IRow0Column0.Width = Constants.BlockWidth; Canvas.SetLeft(IRow0Column0, 0 * Constants.BlockWidth); Canvas.SetTop(IRow0Column0, 0 * Constants.BlockHeight); IRow0Column1.Height = Constants.BlockHeight; IRow0Column1.Width = Constants.BlockWidth; Canvas.SetLeft(IRow0Column1, 1 * Constants.BlockWidth); Canvas.SetTop(IRow0Column1, 0 * Constants.BlockHeight); IRow0Column2.Height = Constants.BlockHeight; IRow0Column2.Width = Constants.BlockWidth; Canvas.SetLeft(IRow0Column2, 2 * Constants.BlockWidth); Canvas.SetTop(IRow0Column2, 0 * Constants.BlockHeight); IRow0Column3.Height = Constants.BlockHeight; IRow0Column3.Width = Constants.BlockWidth; Canvas.SetLeft(IRow0Column3, 3 * Constants.BlockWidth); Canvas.SetTop(IRow0Column3, 0 * Constants.BlockHeight); IRow1Column0.Height = Constants.BlockHeight; IRow1Column0.Width = Constants.BlockWidth; Canvas.SetLeft(IRow1Column0, 0 * Constants.BlockWidth); Canvas.SetTop(IRow1Column0, 1 * Constants.BlockHeight); IRow1Column1.Height = Constants.BlockHeight; IRow1Column1.Width = Constants.BlockWidth; Canvas.SetLeft(IRow1Column1, 1 * Constants.BlockWidth); Canvas.SetTop(IRow1Column1, 1 * Constants.BlockHeight); IRow1Column2.Height = Constants.BlockHeight; IRow1Column2.Width = Constants.BlockWidth; Canvas.SetLeft(IRow1Column2, 2 * Constants.BlockWidth); Canvas.SetTop(IRow1Column2, 1 * Constants.BlockHeight); IRow1Column3.Height = Constants.BlockHeight; IRow1Column3.Width = Constants.BlockWidth; Canvas.SetLeft(IRow1Column3, 3 * Constants.BlockWidth); Canvas.SetTop(IRow1Column3, 1 * Constants.BlockHeight); IRow2Column0.Height = Constants.BlockHeight; IRow2Column0.Width = Constants.BlockWidth; Canvas.SetLeft(IRow2Column0, 0 * Constants.BlockWidth); Canvas.SetTop(IRow2Column0, 2 * Constants.BlockHeight); IRow2Column1.Height = Constants.BlockHeight; IRow2Column1.Width = Constants.BlockWidth; Canvas.SetLeft(IRow2Column1, 1 * Constants.BlockWidth); Canvas.SetTop(IRow2Column1, 2 * Constants.BlockHeight); IRow2Column2.Height = Constants.BlockHeight; IRow2Column2.Width = Constants.BlockWidth; Canvas.SetLeft(IRow2Column2, 2 * Constants.BlockWidth); Canvas.SetTop(IRow2Column2, 2 * Constants.BlockHeight); IRow2Column3.Height = Constants.BlockHeight; IRow2Column3.Width = Constants.BlockWidth; Canvas.SetLeft(IRow2Column3, 3 * Constants.BlockWidth); Canvas.SetTop(IRow2Column3, 2 * Constants.BlockHeight); IRow3Column0.Height = Constants.BlockHeight; IRow3Column0.Width = Constants.BlockWidth; Canvas.SetLeft(IRow3Column0, 0 * Constants.BlockWidth); Canvas.SetTop(IRow3Column0, 3 * Constants.BlockHeight); IRow3Column1.Height = Constants.BlockHeight; IRow3Column1.Width = Constants.BlockWidth; Canvas.SetLeft(IRow3Column1, 1 * Constants.BlockWidth); Canvas.SetTop(IRow3Column1, 3 * Constants.BlockHeight); IRow3Column2.Height = Constants.BlockHeight; IRow3Column2.Width = Constants.BlockWidth; Canvas.SetLeft(IRow3Column2, 2 * Constants.BlockWidth); Canvas.SetTop(IRow3Column2, 3 * Constants.BlockHeight); //set up events for each image foreach (Image b in theBlocks) { //set up the events b.ManipulationDelta += new ManipulationDeltaEventHandler(ManipulateMe_ManipulationDelta); b.ManipulationCompleted += new ManipulationCompletedEventHandler(ManipulateMe_ManipulationCompleted); b.ManipulationMode = ManipulationModes.TranslateX | ManipulationModes.TranslateY; } //Create a list of myTile Objects from the list of images //each mytile will hold the image object as well as game information about this tile. for (int i = 0; i < 15; i++) { myTile temp = new myTile(theBlocks[i].Name.ToString(), theBlocks[i], Canvas.GetLeft(theBlocks[i]), Canvas.GetTop(theBlocks[i])); theTiles.Add(temp); } //add in the empty tile theTiles.Add(new myTile("E", null, Constants.BlockWidth, Constants.BlockHeight)); //update all positions on canvas and the movement of the tiles theTiles = TileHandler.UpdateTile(theTiles, 3, 3); //randomize the board Random random = new Random(); int randIndex = 0; while (randIndex < 100) { int ListIndex = random.Next(14); if (theTiles[ListIndex].Move != 0) { //pointer released is called because it already is able to move a tile based on that tiles attributes PointerReleased(theTiles[ListIndex].TileImage, null); randIndex++; } } }