private void ShuffleBoard() { int rows = gemsBoard.GetLength(0); int columns = gemsBoard.GetLength(1); var randomizedBoard = new Gem[rows, columns]; int[] shuffledRowIndexes = Enumerable.Range(0, rows).ToArray(); int[] shuffledColumnIndexes = Enumerable.Range(0, columns).ToArray(); System.Random rnd = new System.Random(); shuffledRowIndexes = shuffledRowIndexes.OrderBy(x => rnd.Next()).ToArray(); var center = GetBoardCenterPosition(); var posX = center.x - gemSize.x * (boardWidth / 2); for (int i = 0; i < rows; i++) { shuffledColumnIndexes = shuffledColumnIndexes.OrderBy(x => rnd.Next()).ToArray(); var posY = center.y - gemSize.y * (boardHeight / 2); for (int j = 0; j < columns; j++) { randomizedBoard[i, j] = gemsBoard[shuffledRowIndexes.ElementAt(i), shuffledColumnIndexes.ElementAt(j)]; randomizedBoard[i, j].TargetPosition = new Vector2(posX, posY); randomizedBoard[i, j].Column = i; randomizedBoard[i, j].Row = j; posY += gemSize.y; } posX += gemSize.x; } gemsBoard = randomizedBoard; }
/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { swappableGems = new List <Gem>(4); IsMouseVisible = true; // TODO: Add your initialization logic here gemTexture = Content.Load <Texture2D>("Ball"); square = Content.Load <Texture2D>("Sqaure"); for (int i = 0; i < gems.GetLength(0); i++) { for (int j = 0; j < gems.GetLength(1); j++) { gems[i, j] = new Gem(random.Next(4), new Rectangle(j * 48, i * 48, 48, 48)); } } OnLeftClick += GemSelectionHandler; base.Initialize(); }
/// <summary> /// Gem request on the bottom /// </summary> /// <param name="gems">Gem grid on the board</param> /// <param name="gem">Which gem to watch</param> /// <param name="number">How many elements</param> /// <returns>Could he find the gem and the gem itself</returns> public static (bool, Gem) Bottom(this Gem[,] gems, Gem gem, int number) { var lines = gems.GetLength(1); if (gem.YPosition + number >= lines || gem.YPosition + number < 0) { return(false, null); } var nextGem = gems[gem.XPosition, gem.YPosition + number]; return(gem.Equals(nextGem), nextGem); }
/// <summary> /// Gem request on the right /// </summary> /// <param name="gems">Gem grid on the board</param> /// <param name="gem">Which gem to watch</param> /// <param name="number">How many elements</param> /// <returns>Could he find the gem and the gem itself</returns> public static (bool, Gem) Right(this Gem[,] gems, Gem gem, int number) { var columns = gems.GetLength(0); if (gem.XPosition + number >= columns || gem.XPosition + number < 0) { return(false, null); } var nextGem = gems[gem.XPosition + number, gem.YPosition]; return(gem.Equals(nextGem), nextGem); }
void AddGemRandomlyInRows() { Gems = new Gem[Def.Width, Def.Depth + 2]; int spacing = Def.Index * 2; for (int y = 0; y <= Gems.GetLength(1); y += spacing) { spacing++; var x = PRNG.Int(Def.Width); Gems[x, y] = CreateGem(x, y); } Gems.ForEach(item => { if (!item) { return; } DestroyBlock(item.Cell, false); }); }
public static void DoGemMovementLogic(ref bool flipper) { // next step - Put all gems in array with everything else. determine movement by checking array. // Apply changes to array after movement (to prevent sequential weirdness) var columnsBlocked = new List <int>(); var columnsMoved = new List <int>(); var gemsNotMovedInRow = new List <Gem>(); for (var cy = _levelCells.GetLength(1) - 1; cy >= 0; cy--) { // looping through the rows, bottom to top for (var cx = 0; cx < _levelCells.GetLength(0); cx++) { var gem = _levelCells[cx, cy]; if (gem != null) { int tX = gem.X, tY = gem.Y + 1; var blockedBelow = _level[tX, tY] != '_' || _levelCells[tX, tY] != null; if (blockedBelow) { if (!columnsBlocked.Contains(gem.X)) { columnsBlocked.Add(gem.X); } gemsNotMovedInRow.Add(gem); } else { if (!columnsMoved.Contains(gem.X)) { columnsMoved.Add(gem.X); } MoveCell(gem, tX, tY); } } } } //---------------------------------------------------------------------------------------- // check columns? -- still sucks! if (columnsBlocked.Count > 0) { bool movingUp = true; List <int> indexes = new List <int>(); int cx = 0, leftx = 0, rightx = _levelCells.GetLength(0); while (columnsBlocked.Count > 0) { if (columnsBlocked.Contains(cx)) { movingUp = !movingUp; columnsBlocked.Remove(cx); // looping through the columns for (var cy = 0; cy < _levelCells.GetLength(1); cy++) //for (var cy = _levelCells.GetLength(1) - 1; cy >= 0; cy--) { var gem = _levelCells[cx, cy]; if (_level[cx, cy] == '_') { if (!gemsNotMovedInRow.Contains(gem)) { break; } if (gem != null && gemsNotMovedInRow.Contains(gem)) { bool couldMoveLeft = _level[cx - 1, cy] == '_' && _levelCells[cx - 1, cy] == null, couldMoveRight = _level[cx + 1, cy] == '_' && _levelCells[cx + 1, cy] == null, couldMoveDownLeft = couldMoveLeft && _level[cx - 1, cy + 1] == '_' && _levelCells[cx - 1, cy + 1] == null, couldMoveDownRight = couldMoveRight && _level[cx + 1, cy + 1] == '_' && _levelCells[cx + 1, cy + 1] == null; bool move = false; int tX = cx, tY = cy; if (couldMoveDownLeft || couldMoveDownRight) { move = true; if (couldMoveDownLeft && couldMoveDownRight) { couldMoveDownRight = flipper; couldMoveDownLeft = !flipper; flipper = !flipper; } tX = gem.X + (couldMoveDownRight ? 1 : -1); } else if (couldMoveLeft || couldMoveRight) { if (gem.X != gem.BirthColumn) { if ((gem.X < gem.BirthColumn && couldMoveLeft) || (gem.X > gem.BirthColumn && couldMoveRight)) { move = true; tX = gem.X + (couldMoveRight ? 1 : -1); } } else { move = true; if (couldMoveLeft && couldMoveRight) { couldMoveLeft = flipper; couldMoveRight = !flipper; flipper = !flipper; } tX = gem.X + (couldMoveRight ? 1 : -1); } } if (move) { MoveCell(gem, tX, tY); // move down above crap for (var yy = cy - 1; yy >= 0; yy--) { if (_levelCells[cx, yy] != null && _levelCells[cx, yy + 1] == null) { MoveCell(_levelCells[cx, yy], cx, yy + 1); } } break; } } } } } if (movingUp) { leftx++; cx = leftx; } else { rightx--; cx = rightx; } } } // spawn! foreach (var spawner in _spawners) { if (_levelCells[spawner.X, spawner.Y + 1] == null) { var gem = new Gem(spawner.X, spawner.Y + 1); //_gems.Insert(0, gem); _levelCells[gem.X, gem.Y] = gem; //Console.SetCursorPosition(gem.X, gem.Y); //Console.BackgroundColor = ConsoleColor.Black; //Console.ForegroundColor = gem.Color; //Console.Write(gem.Character); } } }
public bool IsInBounds(float x, float y) { return(x >= 0 && y >= 0 && x < gems.GetLength(0) && y < gems.GetLength(1)); }