/// <summary> /// Randomizes the location of each of the value tiles and the empty space /// </summary> private void RandomizeTiles() { _isPuzzleSolved = false; List <Point> tempCoords = new List <Point>(_coords); Random rand = new Random(); foreach (MyTextView view in _tiles) { int randIndex = rand.Next(0, tempCoords.Count); Point randLoc = tempCoords[randIndex]; GridLayout.Spec rowSpec = GridLayout.InvokeSpec(randLoc.Y); GridLayout.Spec colSpec = GridLayout.InvokeSpec(randLoc.X); GridLayout.LayoutParams randLayoutParameters = new GridLayout.LayoutParams(rowSpec, colSpec); randLayoutParameters.Width = _tileWidth - 10; randLayoutParameters.Height = _tileWidth - 10; randLayoutParameters.SetMargins(5, 5, 5, 5); view.XPos = randLoc.X; view.YPos = randLoc.Y; TileIsInCorrectPosition(view); view.LayoutParameters = randLayoutParameters; tempCoords.RemoveAt(randIndex); } _emptyLocation = tempCoords[0]; }
private void randomizeMethod() { Random myRand = new Random(); ArrayList copyCoordsList = new ArrayList(_coordinatesList); foreach (MyImageView any in _tilesList) { int randIndex = myRand.Next(0, copyCoordsList.Count); Point thisRandLoc = (Point)copyCoordsList[randIndex]; GridLayout.Spec rowSpec = GridLayout.InvokeSpec(thisRandLoc.Y); GridLayout.Spec colSpec = GridLayout.InvokeSpec(thisRandLoc.X); GridLayout.LayoutParams randLayoutParams = new GridLayout.LayoutParams(rowSpec, colSpec); any.xPos = thisRandLoc.X; any.yPos = thisRandLoc.Y; randLayoutParams.Width = _tileWidth - 10; randLayoutParams.Height = _tileWidth - 10; randLayoutParams.SetMargins(5, 5, 5, 5); any.LayoutParameters = randLayoutParams; copyCoordsList.RemoveAt(randIndex); } _emptySlot = (Point)copyCoordsList[0]; }
public void TextTile_Touch(object sender, View.TouchEventArgs e) { if (e.Event.Action == MotionEventActions.Up) { MyImageView thisTile = (MyImageView)sender; Console.WriteLine("\r tile is at: \r x={0} \r y={1}", thisTile.xPos, thisTile.yPos); float xDif = (float)System.Math.Pow(thisTile.xPos - _emptySlot.X, 2); float yDif = (float)System.Math.Pow(thisTile.yPos - _emptySlot.Y, 2); float dist = (float)System.Math.Sqrt(xDif + yDif); if (dist == 1) { Point curPoint = new Point(thisTile.xPos, thisTile.yPos); GridLayout.Spec rowSpec = GridLayout.InvokeSpec(_emptySlot.Y); GridLayout.Spec colSpec = GridLayout.InvokeSpec(_emptySlot.X); GridLayout.LayoutParams newLocParams = new GridLayout.LayoutParams(rowSpec, colSpec); thisTile.xPos = _emptySlot.X; thisTile.yPos = _emptySlot.Y; newLocParams.Width = _tileWidth - 10; newLocParams.Height = _tileWidth - 10; newLocParams.SetMargins(5, 5, 5, 5); thisTile.LayoutParameters = newLocParams; _emptySlot = curPoint; } } }
private void randomizeMethod() { // 16 tiles. //16 locations. //go through tiles in order //assign a random coord to them. Random myRandom = new Random(); foreach (ImageView any in tilesArr) { int randomIndex = myRandom.Next(0, coordsArr.Count); Point newRandCoord = coordsArr[randomIndex]; GridLayout.Spec randomRowSpec = GridLayout.InvokeSpec(newRandCoord.X); GridLayout.Spec randomColSpec = GridLayout.InvokeSpec(newRandCoord.Y); GridLayout.LayoutParams randGridParams = new GridLayout.LayoutParams(randomRowSpec, randomColSpec); randGridParams.Width = tileWidth; randGridParams.Height = tileWidth; any.LayoutParameters = randGridParams; coordsArr.RemoveAt(randomIndex); } }
private void RandomMethod() { ArrayList tempCoords = new ArrayList(coordsTile); Random random = new Random(); foreach (MyTextView any in tileArr) { int randomIndex = random.Next(0, tempCoords.Count); Point thisRandomLocation = (Point)tempCoords[randomIndex]; GridLayout.Spec rowSpec = GridLayout.InvokeSpec(thisRandomLocation.Y); GridLayout.Spec collSpec = GridLayout.InvokeSpec(thisRandomLocation.X); GridLayout.LayoutParams randomLayoutParams = new GridLayout.LayoutParams(rowSpec, collSpec); any.xPos = thisRandomLocation.X; any.yPos = thisRandomLocation.Y; randomLayoutParams.Width = tileWidth - 10; randomLayoutParams.Height = tileWidth - 10; randomLayoutParams.SetMargins(6, 6, 6, 6); any.LayoutParameters = randomLayoutParams; tempCoords.RemoveAt(randomIndex); } emptyTile = (Point)tempCoords[0]; }
private void randomizeMethod() { ArrayList tempCoords = new ArrayList(coordsArr); Random myRand = new Random(); foreach (MyTextView any in tilesArr) { int randIndex = myRand.Next(0, tempCoords.Count); Point thisRandLoc = (Point)tempCoords[randIndex]; GridLayout.Spec rowSpec = GridLayout.InvokeSpec(thisRandLoc.Y); GridLayout.Spec colSpec = GridLayout.InvokeSpec(thisRandLoc.X); GridLayout.LayoutParams randLayoutParam = new GridLayout.LayoutParams(rowSpec, colSpec); any.xPos = thisRandLoc.X; any.yPos = thisRandLoc.Y; randLayoutParam.Width = tileWidth - 10; randLayoutParam.Height = tileWidth - 10; randLayoutParam.SetMargins(5, 5, 5, 5); any.LayoutParameters = randLayoutParam; tempCoords.RemoveAt(randIndex); } emptySpot = (Point)tempCoords[0]; }
private void RandomiseTiles() { Random randomiser = new Random(); ArrayList copyCoordList = new ArrayList(coordList); foreach (MyTextView tile in tilesList) { int randIndex = randomiser.Next(0, copyCoordList.Count); Point randomisedLocation = (Point)copyCoordList[randIndex]; GridLayout.Spec rowSpec = GridLayout.InvokeSpec(randomisedLocation.X); GridLayout.Spec colSpec = GridLayout.InvokeSpec(randomisedLocation.Y); GridLayout.LayoutParams randTileParams = new GridLayout.LayoutParams(rowSpec, colSpec); //keep location of tile tile.coordX = randomisedLocation.X; tile.coordY = randomisedLocation.Y; randTileParams.Width = tileWidth - 10; randTileParams.Height = tileWidth - 10; randTileParams.SetMargins(5, 5, 5, 5); tile.LayoutParameters = randTileParams; copyCoordList.RemoveAt(randIndex); } emptySpot = (Point)copyCoordList[0]; }
private void RandomizeTiles() { ArrayList tempCoords = new ArrayList(coordsArray); // 16 elements Random myRand = new Random(); foreach (MyTextView any in tilesArray) // 15 elements { int randIndex = myRand.Next(0, tempCoords.Count); Point thisRandLoc = (Point)tempCoords[randIndex]; GridLayout.Spec rowSpec = GridLayout.InvokeSpec(thisRandLoc.Y); GridLayout.Spec colSpec = GridLayout.InvokeSpec(thisRandLoc.X); GridLayout.LayoutParams randLayoutParam = new GridLayout.LayoutParams(rowSpec, colSpec); any.xPos = thisRandLoc.X; any.yPos = thisRandLoc.Y; randLayoutParam.Width = tileWidth - 10; randLayoutParam.Height = tileWidth - 10; randLayoutParam.SetMargins(5, 5, 5, 5); any.LayoutParameters = randLayoutParam; tempCoords.RemoveAt(randIndex); } Console.WriteLine("\r\r\r\r there are: {0} elements left", tempCoords.Count); emptySpot = (Point)tempCoords[0]; }
private void randomizeMethod( ) { Random myRandom = new Random( ); foreach (ImageView any in tilesArr) { int randomIndex = myRandom.Next(0, coordsArr.Count); Point newRandCoord = coordsArr[randomIndex]; GridLayout.Spec randomRowSpec = GridLayout.InvokeSpec(newRandCoord.X); GridLayout.Spec randomColSPec = GridLayout.InvokeSpec(newRandCoord.Y); GridLayout.LayoutParams randGirdParams = new GridLayout.LayoutParams(randomRowSpec, randomColSPec); randGirdParams.Width = tileWidth; randGirdParams.Height = tileWidth; any.LayoutParameters = randGirdParams; coordsArr.RemoveAt(randomIndex); } }
void TouchTile(object sender, View.TouchEventArgs e) { if (e.Event.Action == MotionEventActions.Up) { MyTextView tile = (MyTextView)sender; Console.WriteLine("tile is at x={0}, y={0}", tile.coordX, tile.coordY); //calculate distance between empty spot and the touched tile float xDif = (float)Math.Pow(tile.coordX - emptySpot.X, 2); float yDif = (float)Math.Pow(tile.coordY - emptySpot.Y, 2); float distance = (float)Math.Sqrt(xDif + yDif); //tile next to empty spot if (distance == 1) { Point currentPoint = new Point(tile.coordX, tile.coordY); GridLayout.Spec rowSpec = GridLayout.InvokeSpec(emptySpot.X); GridLayout.Spec colSpec = GridLayout.InvokeSpec(emptySpot.Y); GridLayout.LayoutParams newLocationParams = new GridLayout.LayoutParams(rowSpec, colSpec); tile.coordX = emptySpot.X; tile.coordY = emptySpot.Y; newLocationParams.Width = tileWidth - 10; newLocationParams.Height = tileWidth - 10; newLocationParams.SetMargins(5, 5, 5, 5); tile.LayoutParameters = newLocationParams; emptySpot = currentPoint; } } }
private void MakeTiles() { tileWidth = gameViewWidth / 4; tilesArray = new ArrayList(); coordsArray = new ArrayList(); int counter = 1; #region MyRegion for (int h = 0; h < 4; h++) { for (int v = 0; v < 4; v++) { MyTextView textTile = new MyTextView(this); GridLayout.Spec rowSpec = GridLayout.InvokeSpec(h); GridLayout.Spec colSpec = GridLayout.InvokeSpec(v); GridLayout.LayoutParams tileLayoutParams = new GridLayout.LayoutParams(rowSpec, colSpec); // 0 1 2 3 v/x // 0 T T T T // 1 T T T T // 2 T T T T // 3 T T T T textTile.Text = counter.ToString(); textTile.SetTextColor(Color.Black); textTile.TextSize = 40; textTile.Gravity = GravityFlags.Center; tileLayoutParams.Width = tileWidth - 10; tileLayoutParams.Height = tileWidth - 10; tileLayoutParams.SetMargins(5, 5, 5, 5); textTile.LayoutParameters = tileLayoutParams; textTile.SetBackgroundColor(Color.Green); Point thisLoc = new Point(v, h); coordsArray.Add(thisLoc); textTile.xPos = thisLoc.X; textTile.yPos = thisLoc.Y; textTile.Touch += TextTile_Touch; tilesArray.Add(textTile); mainLayout.AddView(textTile); counter++; } } #endregion mainLayout.RemoveView((MyTextView)tilesArray[15]); tilesArray.RemoveAt(15); }
private void tileMakerMethod() { // know the grid size ... gridSize // know the tile width and height //know where to place tile in grid layout //know which image to display on tiles tileWidth = screenWidth / gridSize; System.String imageName; int imgNumber = 0; for (int h = 0; h < gridSize; h++) { for (int v = 0; v < gridSize; v++) { ImageView imgTile = new ImageView(this); Point thisCoord = new Point(h, v); coordsArr.Add(thisCoord); GridLayout.Spec rowSpec = GridLayout.InvokeSpec(thisCoord.X); GridLayout.Spec colSpec = GridLayout.InvokeSpec(thisCoord.Y); GridLayout.LayoutParams tileParams = new GridLayout.LayoutParams(rowSpec, colSpec); tileParams.Width = tileWidth; tileParams.Height = tileWidth; if (imgNumber == gridSize * gridSize / 2) { imgNumber = 0; } imageName = imgsArr[imgNumber] as string; int resId = Resources.GetIdentifier(imageName, "drawable", PackageName); imgTile.SetImageResource(resId); imgTile.LayoutParameters = tileParams; imgTile.SetOnTouchListener(this); tilesArr.Add(imgTile); gameGridLayout.AddView(imgTile); imgNumber++; } } // have an array of all tiles //ORDERED from 0 to gridsize X gridSize // and have an array of all coords. }
private void tileMakerMethod( ) { tileWidth = screenWidth / gridSize; System.String imageName; int imgNumber = 0; for (int h = 0; h < gridSize; h++) { for (int v = 0; v < gridSize; v++) { ImageView imgTile = new ImageView(this); Point thisCoord = new Point(h, v); coordsArr.Add(thisCoord); GridLayout.Spec rowSpec = GridLayout.InvokeSpec(h); GridLayout.Spec colSpec = GridLayout.InvokeSpec(v); GridLayout.LayoutParams tileParams = new GridLayout.LayoutParams(rowSpec, colSpec); tileParams.Width = tileWidth; tileParams.Height = tileWidth; if (imgNumber == gridSize * gridSize / 2) { imgNumber = 0; } imageName = imgsArr[imgNumber] as string; int resId = Resources.GetIdentifier(imageName, "drawable", PackageName); imgTile.SetImageResource(resId); imgTile.LayoutParameters = tileParams; imgTile.SetOnTouchListener(this); tilesArr.Add(imgTile); gameGridLayout.AddView(imgTile); imgNumber++; } } }
private GridLayout.LayoutParams GetTileLayoutParams(int x, int y, int colWidth) { GridLayout.Spec rowSpec = GridLayout.InvokeSpec(x); GridLayout.Spec colSpec = GridLayout.InvokeSpec(y); GridLayout.LayoutParams tileLayoutParams = new GridLayout.LayoutParams(rowSpec, colSpec) { Width = colWidth - 20, Height = ViewGroup.LayoutParams.WrapContent }; tileLayoutParams.SetMargins(5, 5, 5, 5); return(tileLayoutParams); }
private GridLayout.LayoutParams GetTileLayoutParams(int x, int y) { // Create the specifications that establish in which row and column the tile is going to be rendered GridLayout.Spec rowSpec = GridLayout.InvokeSpec(x); GridLayout.Spec colSpec = GridLayout.InvokeSpec(y); // Create a new layout parameter object for the tile using the previous specs GridLayout.LayoutParams tileLayoutParams = new GridLayout.LayoutParams(rowSpec, colSpec); tileLayoutParams.Width = tileWidth - 10; tileLayoutParams.Height = tileWidth - 10; tileLayoutParams.SetMargins(5, 5, 5, 5); return(tileLayoutParams); }
private void TilesMethod() { tileWidth = viewGameWidth / 4; tileArr = new ArrayList(); coordsTile = new ArrayList(); int counter = 1; for (int rows = 0; rows < 4; rows++) { for (int colls = 0; colls < 4; colls++) { MyTextView txtTile = new MyTextView(this); txtTile.Text = counter.ToString(); txtTile.SetTextColor(Color.White); txtTile.Gravity = GravityFlags.Center; txtTile.TextSize = 30; GridLayout.Spec rowSpec = GridLayout.InvokeSpec(txtTile.xPos); GridLayout.Spec collSpec = GridLayout.InvokeSpec(txtTile.yPos); GridLayout.LayoutParams tileLayoutParams = new GridLayout.LayoutParams(rowSpec, collSpec); tileLayoutParams.Width = tileWidth - 10; tileLayoutParams.Height = tileWidth - 10; tileLayoutParams.SetMargins(6, 6, 6, 6); txtTile.LayoutParameters = tileLayoutParams; txtTile.SetBackgroundColor(Color.Green); Point point = new Point(colls, rows); coordsTile.Add(point); txtTile.xPos = point.X; txtTile.yPos = point.Y; txtTile.Touch += TxtTile_Touch; tileArr.Add(txtTile); gridLayout.AddView(txtTile); counter++; } } gridLayout.RemoveView((MyTextView)tileArr[15]); tileArr.RemoveAt(15); }
private void MakeTiles() { tileWidth = gameViewWidth / 4; int tileCount = 1; for (int row = 0; row < 4; row++) { for (int col = 0; col < 4; col++) { MyTextView tileText = new MyTextView(this); GridLayout.Spec rowSpec = GridLayout.InvokeSpec(row); GridLayout.Spec colSpec = GridLayout.InvokeSpec(col); GridLayout.LayoutParams tileLayoutParams = new GridLayout.LayoutParams(rowSpec, colSpec); tileText.Text = tileCount.ToString(); tileText.SetTextColor(Color.Black); tileText.TextSize = 40; tileText.Gravity = GravityFlags.Center; tileLayoutParams.Width = tileWidth - 10; tileLayoutParams.Height = tileWidth - 10; tileLayoutParams.SetMargins(5, 5, 5, 5); tileText.LayoutParameters = tileLayoutParams; tileText.SetBackgroundColor(Color.Green); //save coordinates of tile Point tileLocation = new Point(col, row); coordList.Add(tileLocation); tilesList.Add(tileText); //remember the position of the tile tileText.coordX = tileLocation.X; tileText.coordY = tileLocation.Y; tileText.Touch += TouchTile; mainLayout.AddView(tileText); tileCount = tileCount + 1; } } mainLayout.RemoveView((MyTextView)tilesList[15]); tilesList.RemoveAt(15); }
private void TxtTile_Touch(object sender, View.TouchEventArgs e) { if (e.Event.Action == MotionEventActions.Up) { if (tileArr.Contains(sender)) { MyTextView thisTile = (MyTextView)sender; float xDifference = (float)Math.Pow(thisTile.xPos - emptyTile.X, 2); float yDifference = (float)Math.Pow(thisTile.yPos - emptyTile.Y, 2); float distanceXY = (float)Math.Sqrt(xDifference + yDifference); //Tile can move if (distanceXY == 1) { //Memorize where the tile use to be Point currentPoint = new Point(thisTile.xPos, thisTile.yPos); //take the empty tile GridLayout.Spec rowSpec = GridLayout.InvokeSpec(emptyTile.Y); GridLayout.Spec collSpec = GridLayout.InvokeSpec(emptyTile.X); GridLayout.LayoutParams newLayoutParams = new GridLayout.LayoutParams(rowSpec, collSpec); thisTile.xPos = emptyTile.X; thisTile.yPos = emptyTile.Y; newLayoutParams.Width = tileWidth - 10; newLayoutParams.Height = tileWidth - 10; newLayoutParams.SetMargins(6, 6, 6, 6); thisTile.LayoutParameters = newLayoutParams; emptyTile = currentPoint; } //#region Test positions touch tile and empty tile //Console.WriteLine($"tile position\nx: {thisTile.xPos}\ny: {thisTile.yPos} "); //Console.WriteLine($"empty tile position\nx: {emptyTile.X}\ny: {emptyTile.Y} "); //#endregion } } }
void TextTile_Touch(object sender, View.TouchEventArgs e) { if (e.Event.Action == MotionEventActions.Up) { if (tilesArr.Contains(sender)) { MyTextView thisTile = (MyTextView)sender; Console.WriteLine("Tile Position X:{0} -- Y:{1}", thisTile.xPos, thisTile.yPos); Console.WriteLine("Empty Position X:{0} -- Y:{1}", emptySpot.X, emptySpot.Y); float xDif = (float)Math.Pow(thisTile.xPos - emptySpot.X, 2); float yDif = (float)Math.Pow(thisTile.yPos - emptySpot.Y, 2); float dist = (float)Math.Sqrt(xDif + yDif); if (dist == 1) // si es verdadero, puede mover { // memoriza donde estaba el tile Point curPoint = new Point(thisTile.xPos, thisTile.yPos); // mueve el tile al lugar vacio GridLayout.Spec rowSpec = GridLayout.InvokeSpec(emptySpot.Y); GridLayout.Spec colSpec = GridLayout.InvokeSpec(emptySpot.X); GridLayout.LayoutParams newLocParams = new GridLayout.LayoutParams(rowSpec, colSpec); thisTile.xPos = emptySpot.X; thisTile.yPos = emptySpot.Y; newLocParams.Width = tileWidth - 10; newLocParams.Height = tileWidth - 10; newLocParams.SetMargins(5, 5, 5, 5); thisTile.LayoutParameters = newLocParams; // el vacio se mueve donde estaba el tile que tocamos emptySpot = curPoint; } } } }
// function that executes when the tile is touched void TextTile_Touch(object sender, View.TouchEventArgs e) { if (e.Event.Action == MotionEventActions.Up) { MyTextView thisTile = (MyTextView)sender; // just write the position of the tile in the Xamarin console, to see each tile when moves Console.WriteLine("\r tile is at: \r x={0} \r y={1}", thisTile.xPos, thisTile.yPos); // compute the distace between the tile which was pressed and the empty space float xDif = (float)Math.Pow(thisTile.xPos - emptySpot.X, 2); float yDif = (float)Math.Pow(thisTile.yPos - emptySpot.Y, 2); float dist = (float)Math.Sqrt(xDif + yDif); // if the tile was near the empty space if (dist == 1) { // memorize the current position of the tile Point curPoint = new Point(thisTile.xPos, thisTile.yPos); // we want to put the tile on the place of the empty space GridLayout.Spec rowSpec = GridLayout.InvokeSpec(emptySpot.Y); GridLayout.Spec colSpec = GridLayout.InvokeSpec(emptySpot.X); GridLayout.LayoutParams newLocParams = new GridLayout.LayoutParams(rowSpec, colSpec); // the tile moves in the empty space thisTile.xPos = emptySpot.X; thisTile.yPos = emptySpot.Y; // we set the appearance of the tile newLocParams.Width = tileWidth - 10; newLocParams.Height = tileWidth - 10; newLocParams.SetMargins(5, 5, 5, 5); thisTile.LayoutParameters = newLocParams; // the empty place goes where was the pressed tile before emptySpot = curPoint; } } }
private void MakeTiles() { _tileWidth = _gameViewWidth / 4; int counter = 1; for (int h = 0; h < 4; h++) { for (int v = 0; v < 4; v++) { MyTextView textTile = new MyTextView(this); GridLayout.Spec rowSpec = GridLayout.InvokeSpec(h); GridLayout.Spec colSpec = GridLayout.InvokeSpec(v); GridLayout.LayoutParams tileLayoutParams = new GridLayout.LayoutParams(rowSpec, colSpec); Point thisLoc = new Point(v, h); tileLayoutParams.Width = _tileWidth - 10; tileLayoutParams.Height = _tileWidth - 10; tileLayoutParams.SetMargins(5, 5, 5, 5); textTile.Text = (counter++).ToString(); textTile.TextSize = 40; textTile.SetTextColor(Color.Black); textTile.Gravity = GravityFlags.Center; textTile.LayoutParameters = tileLayoutParams; textTile.SetBackgroundColor(_wrongCoordColor); textTile.Touch += TextTile_Touch; textTile.XPos = thisLoc.X; textTile.YPos = thisLoc.Y; _coords.Add(thisLoc); _tiles.Add(textTile); _mainLayout.AddView(textTile); } } _mainLayout.RemoveView(_tiles[15]); _tiles.RemoveAt(15); }
private void randomizeMethod() { // take a helper to created random number Random myRand = new Random(); // store a copy of the coordinates list ArrayList copyCoordsList = new ArrayList(coordinatesList); // take each tile (in the variable any) and shuffle it to a new position foreach (MyTextView any in tilesList) { // take random coordinates where to put this tile (tile any) int randIndex = myRand.Next(0, copyCoordsList.Count); // and store the coordinates in a variable (which is a point in space) Point thisRandLoc = (Point)copyCoordsList[randIndex]; // create a new tile (which is a 1 x 1 part of the whole grid) GridLayout.Spec rowSpec = GridLayout.InvokeSpec(thisRandLoc.Y); GridLayout.Spec colSpec = GridLayout.InvokeSpec(thisRandLoc.X); GridLayout.LayoutParams randLayoutParam = new GridLayout.LayoutParams(rowSpec, colSpec); // also keep the location of the tile any any.xPos = thisRandLoc.X; any.yPos = thisRandLoc.Y; // set the appearance of the tile to look similar randLayoutParam.Width = tileWidth - 10; randLayoutParam.Height = tileWidth - 10; randLayoutParam.SetMargins(5, 5, 5, 5); // set the tile position to be the new shuffled position any.LayoutParameters = randLayoutParam; // remove the coordinate, so we can't use it anymore for another tile copyCoordsList.RemoveAt(randIndex); } // we have deleted 15 out of 16 tiles, so the remaining one is the one that is empty emptySpot = (Point)copyCoordsList[0]; }
// x=2, y=3 // x=3, y=3 private void TextTile_Touch(object sender, View.TouchEventArgs e) { if (e.Event.Action == MotionEventActions.Up) { MyTextView thisTile = (MyTextView)sender; Console.WriteLine("\r\r\r this tile is at: \r x={0} y={1}", thisTile.xPos, thisTile.yPos); Console.WriteLine("\r\r\r empty tile is at: \r x={0} y={1}", emptySpot.X, emptySpot.Y); float xDiff = (float)Math.Pow(thisTile.xPos - emptySpot.X, 2); float yDiff = (float)Math.Pow(thisTile.yPos - emptySpot.Y, 2); float dist = (float)Math.Sqrt(xDiff + yDiff); if (dist == 1) { // tile can move // memorize where the tile used to be Point currPoint = new Point(thisTile.xPos, thisTile.yPos); // now take the tile to the empty GridLayout.Spec rowSpec = GridLayout.InvokeSpec(emptySpot.Y); GridLayout.Spec colSpec = GridLayout.InvokeSpec(emptySpot.X); GridLayout.LayoutParams newLocalParams = new GridLayout.LayoutParams(rowSpec, colSpec); thisTile.xPos = emptySpot.X; thisTile.yPos = emptySpot.Y; newLocalParams.Width = tileWidth - 10; newLocalParams.Height = tileWidth - 10; newLocalParams.SetMargins(5, 5, 5, 5); thisTile.LayoutParameters = newLocalParams; emptySpot = currPoint; } } }
private void TextTile_Touch(object sender, View.TouchEventArgs e) { if (!_isPuzzleSolved) { if (e.Event.Action == MotionEventActions.Up) { MyTextView thisTile = sender as MyTextView; //determine if the square that was clicked is next to the open space if (Math.Sqrt(Math.Pow((thisTile.XPos - _emptyLocation.X), 2) + Math.Pow(thisTile.YPos - _emptyLocation.Y, 2)) == 1) { //save the location of the clicked button before moving it to the location that the empty square occupies Point currentPoint = new Point(thisTile.XPos, thisTile.YPos); GridLayout.Spec rowSpec = GridLayout.InvokeSpec(_emptyLocation.Y); GridLayout.Spec colSpec = GridLayout.InvokeSpec(_emptyLocation.X); GridLayout.LayoutParams newLocParams = new GridLayout.LayoutParams(rowSpec, colSpec); thisTile.XPos = _emptyLocation.X; thisTile.YPos = _emptyLocation.Y; newLocParams.Width = _tileWidth - 10; newLocParams.Height = _tileWidth - 10; newLocParams.SetMargins(5, 5, 5, 5); thisTile.LayoutParameters = newLocParams; //move the empty square to the location originally occupied by the tile _emptyLocation = currentPoint; TileIsInCorrectPosition(thisTile); } System.Diagnostics.Debug.WriteLine($"\r\r\rThis tile is at ({thisTile.XPos},{thisTile.YPos})"); } } }
public GridLayout AddFriendLayout(int givenID, byte[] pictureCode, string username, string message) { //TODO: Add Binding programmatically this.CreateBindingSet <FriendlistUI, FriendlistFragmentViewModel>(); GridLayout.Spec gridColumn = GridLayout.InvokeSpec(16, GridLayout.BaselineAlighment); GridLayout.Spec gridRow = GridLayout.InvokeSpec(16, GridLayout.BaselineAlighment); GridLayout.LayoutParams gl = new GridLayout.LayoutParams(gridRow, gridColumn); GridLayout gridLayout = new GridLayout(Context); gridLayout.LayoutParameters = gl; /* gridLayout.Orientation = GridOrientation.Horizontal; * gridLayout.Id = givenID;*/ GridLayout.Spec imageColumn = GridLayout.InvokeSpec(0, GridLayout.BaselineAlighment); GridLayout.Spec imageRow = GridLayout.InvokeSpec(0, GridLayout.BaselineAlighment); GridLayout.Spec usernameColumn = GridLayout.InvokeSpec(0, GridLayout.BaselineAlighment); GridLayout.Spec usernameRow = GridLayout.InvokeSpec(0, GridLayout.BaselineAlighment); GridLayout.Spec messageColumn = GridLayout.InvokeSpec(0, GridLayout.BaselineAlighment); GridLayout.Spec messageRow = GridLayout.InvokeSpec(0, GridLayout.BaselineAlighment); GridLayout.LayoutParams usernameLayout = new GridLayout.LayoutParams(usernameRow, usernameColumn); usernameLayout.SetMargins(256, 32, 0, 0); GridLayout.LayoutParams messageLayout = new GridLayout.LayoutParams(messageRow, messageColumn); messageLayout.SetMargins(248, 112, 0, 0); GridLayout.LayoutParams imageLayout = new GridLayout.LayoutParams(imageRow, imageColumn); imageLayout.SetMargins(48, 32, 0, 0); imageLayout.Width = 196; imageLayout.Height = 196; ImageView imageView = new ImageView(Context); Bitmap bmp = BitmapFactory.DecodeByteArray(pictureCode, 0, pictureCode.Length); imageView.SetImageBitmap(bmp); imageView.LayoutParameters = imageLayout; TextView usernameView = new TextView(Context); usernameView.Text = username; usernameView.SetTextColor(Color.White); usernameView.SetTextSize(Android.Util.ComplexUnitType.Px, 64); usernameView.LayoutParameters = usernameLayout; TextView messageView = new TextView(Context); messageView.Text = message; messageView.SetTextColor(Color.White); messageView.SetTextSize(Android.Util.ComplexUnitType.Px, 48); messageView.LayoutParameters = messageLayout; gridLayout.AddView(imageView, imageLayout); gridLayout.AddView(usernameView, usernameLayout); gridLayout.AddView(messageView, messageLayout); return(gridLayout); }
// method to create the grid layout private void makeTilesMethod() { // the grid is 4 x 4 -> the width of a tile is a quarter of the width of the grid tileWidth = gameViewWidth / 4; // create a counter to keep in mind the number of the tile int counter = 1; // initialize the lists for storing the coordinates of the tiles tilesList = new ArrayList(); coordinatesList = new ArrayList(); // we need to create all 16 tiles, they being placed on 4 rows and 4 columns for (int row = 0; row < 4; ++row) { for (int column = 0; column < 4; ++column) { // we want to make a tile and add it in our main layout MyTextView textTile = new MyTextView(this); // we want to put a tile on the row 'row' and column 'column' GridLayout.Spec rowSpec = GridLayout.InvokeSpec(row); GridLayout.Spec colSpec = GridLayout.InvokeSpec(column); GridLayout.LayoutParams tileLayoutParams = new GridLayout.LayoutParams(rowSpec, colSpec); // print the number of the tile on it textTile.Text = counter.ToString(); textTile.SetTextColor(Color.Black); textTile.TextSize = 40; textTile.Gravity = GravityFlags.Center; // put the text in the center of the tile // set the width and height of the tile tileLayoutParams.Width = tileWidth - 10; // (-10) for visibility tileLayoutParams.Height = tileWidth - 10; // (-10) for visibility tileLayoutParams.SetMargins(5, 5, 5, 5); // for making a nice looking contour // make the changes in the actual textTile textTile.LayoutParameters = tileLayoutParams; textTile.SetBackgroundColor(Color.Green); // keep the coordinates of a tile (as a point in space, where the X coordinate is the column and Y coordiante is the row) Point thisLocation = new Point(column, row); // add the coordiante of this point in the coordinatesList coordinatesList.Add(thisLocation); // add the tile in the list, to use it later tilesList.Add(textTile); // remember the position if the tile textTile.xPos = thisLocation.X; textTile.yPos = thisLocation.Y; // assign a method to execute when we toch the button textTile.Touch += TextTile_Touch; // add the tile in the main layout mainLayout.AddView(textTile); // increase the counter to the next number counter = counter + 1; } } // remove the 16th tile -> tilesList start from the position 0 (not 1 as expected), so the 16th element is on the position 15 mainLayout.RemoveView((MyTextView)tilesList[15]); // remove the 16th tile also from our list tilesList.RemoveAt(15); }
public static View Create(Context context) { GridLayout p = new GridLayout(context); p.UseDefaultMargins = true; p.AlignmentMode = GridAlign.Bounds; Configuration configuration = context.Resources.Configuration; if ((configuration.Orientation == Android.Content.Res.Orientation.Portrait)) { p.ColumnOrderPreserved = false; } else { p.RowOrderPreserved = false; } //FIXME https://bugzilla.xamarin.com/show_bug.cgi?id=14210 GridLayout.Spec titleRow = GridLayout.InvokeSpec(0); GridLayout.Spec introRow = GridLayout.InvokeSpec(1); GridLayout.Spec emailRow = GridLayout.InvokeSpec(2, GridLayout.Baseline); GridLayout.Spec passwordRow = GridLayout.InvokeSpec(3, GridLayout.Baseline); GridLayout.Spec button1Row = GridLayout.InvokeSpec(5); GridLayout.Spec button2Row = GridLayout.InvokeSpec(6); GridLayout.Spec centerInAllColumns = GridLayout.InvokeSpec(0, 4, GridLayout.Center); GridLayout.Spec leftAlignInAllColumns = GridLayout.InvokeSpec(0, 4, GridLayout.Left); GridLayout.Spec labelColumn = GridLayout.InvokeSpec(0, GridLayout.Right); GridLayout.Spec fieldColumn = GridLayout.InvokeSpec(1, GridLayout.Left); GridLayout.Spec defineLastColumn = GridLayout.InvokeSpec(3); GridLayout.Spec fillLastColumn = GridLayout.InvokeSpec(3, GridLayout.Fill); { TextView c = new TextView(context); c.TextSize = 32; c.Text = "Email setup"; p.AddView(c, new GridLayout.LayoutParams(titleRow, centerInAllColumns)); } { TextView c = new TextView(context); c.TextSize = 16; c.Text = "You can configure email in a few simple steps:"; p.AddView(c, new GridLayout.LayoutParams(introRow, leftAlignInAllColumns)); } { TextView c = new TextView(context); c.Text = "Email address:"; p.AddView(c, new GridLayout.LayoutParams(emailRow, labelColumn)); } { EditText c = new EditText(context); c.SetEms(10); c.InputType = Android.Text.InputTypes.ClassText | Android.Text.InputTypes.TextVariationEmailAddress; p.AddView(c, new GridLayout.LayoutParams(emailRow, fieldColumn)); } { TextView c = new TextView(context); c.Text = "Password:"******"Manual setup"; p.AddView(c, new GridLayout.LayoutParams(button1Row, defineLastColumn)); } { Button c = new Button(context); c.Text = "Next"; p.AddView(c, new GridLayout.LayoutParams(button2Row, fillLastColumn)); } return(p); }
private void makeTilesMethod() { _tileWidth = _gameViewWidth / 3; int counter = 1; string imgsource = "piece" + counter; string fullsource = "Resource.Drawable." + imgsource; _tilesList = new ArrayList(); _coordinatesList = new ArrayList(); for (int row = 0; row < 3; ++row) { for (int column = 0; column < 3; ++column) { MyImageView ImageTile = new MyImageView(_context); GridLayout.Spec rowSpec = GridLayout.InvokeSpec(row); GridLayout.Spec colSpec = GridLayout.InvokeSpec(column); GridLayout.LayoutParams tileLayoutParams = new GridLayout.LayoutParams(rowSpec, colSpec); //ImageTile.Text = counter.ToString(); //ImageTile.SetTextColor(Color.Black); //ImageTile.TextSize = 40; //ImageTile.Gravity = GravityFlags.Center; if (counter == 1) { ImageTile.SetImageResource(Resource.Drawable.piece1); } else if (counter == 2) { ImageTile.SetImageResource(Resource.Drawable.piece2); } else if (counter == 3) { ImageTile.SetImageResource(Resource.Drawable.piece3); } else if (counter == 4) { ImageTile.SetImageResource(Resource.Drawable.piece4); } else if (counter == 5) { ImageTile.SetImageResource(Resource.Drawable.piece5); } else if (counter == 6) { ImageTile.SetImageResource(Resource.Drawable.piece6); } else if (counter == 7) { ImageTile.SetImageResource(Resource.Drawable.piece7); } else if (counter == 8) { ImageTile.SetImageResource(Resource.Drawable.piece8); } tileLayoutParams.Width = _tileWidth - 10; tileLayoutParams.Height = _tileWidth - 10; tileLayoutParams.SetMargins(5, 5, 5, 5); ImageTile.LayoutParameters = tileLayoutParams; //textTile.SetBackgroundColor(Color.Green); Point thisLocation = new Point(column, row); _coordinatesList.Add(thisLocation); _tilesList.Add(ImageTile); ImageTile.xPos = thisLocation.X; ImageTile.yPos = thisLocation.Y; ImageTile.Touch += TextTile_Touch; _puzzleLayout.AddView(ImageTile); counter += 1; } } _puzzleLayout.RemoveView((MyImageView)_tilesList[8]); _tilesList.RemoveAt(8); }